Create an invoice class with four attributes

Assignment Help JAVA Programming
Reference no: EM131587518

Excercise

Create an Invoice class with 4 attributes: PartNumber (type int), PartDescription (type String), Quantity (type int), and Price (type double). Create a constructor that allows you to initialize all 4 attributes from values passed in as parameters. Override the toString() method to display all 4 attributes in a format of your choosing. Create a List of 10 Invoices containing data of your choosing. Use lambdas and streams to perform the following queries on the list of Invoice objects and display the results: Sort the Invoice objects by PartDescription, then display the results. Sort the Invoice objects by Price, then display the results. Map each Invoice to its PartDescription and Quantity, sort the results by Quantity, then display the results. Map each Invoice to its PartDescription and the value of the Invoice (i.e., Quantity * Price). Order the results by Invoice value. Modify Part (d) above to select the Invoice values in the range of $200 to $500 (inclusive) and display the results separately.
The code I have. Need help on this code to demonstrate the use of lambdas and streams.
code
public class Invoice {

private int partNumber ;
private String partDescription;
private int quantity;
private double price;

public Invoice(int partNumber, String partDescription, int quantity, double price) {
this.partNumber = partNumber;
this.partDescription = partDescription;
this.quantity = quantity;
this.price = price;
}

@Override
public String toString() {
return "Invoice{" +
"partNumber=" + partNumber +
", partDescription=" + partDescription +" "+
", quantity=" + quantity +
", price=" + price +
'}';
}

public int getPartNumber() {
return partNumber;
}

public void setPartNumber(int partNumber) {
this.partNumber = partNumber;
}

public String getPartDescription() {
return partDescription;
}

public void setPartDescription(String partDescription) {
this.partDescription = partDescription;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
public String getDescriptionQuantity() {
return String.format("%s %s", partDescription, quantity);
}

public String getDescriptionTotalPrice() {
return String.format("%s %s", partDescription, quantity * price);
}

public double getValue() {
return quantity * price; // getting the invoice total value
}

public boolean isWithinRange(double min, double max) {
return getValue() >= min && getValue() <=max;
}
}
// Invoice Generator
import java.util.ArrayList;

public class InvoiceGenerator {

private ArrayList<Invoice> invoiceList = new ArrayList<Invoice>();

public InvoiceGenerator() {
invoiceList.add(new Invoice(101,"some desc",1,100));
invoiceList.add(new Invoice(102,"some descA",2,101));
invoiceList.add(new Invoice(103,"some descB",3,102));
invoiceList.add(new Invoice(104,"some descC",4,103));
invoiceList.add(new Invoice(105,"some descD",5,104));
invoiceList.add(new Invoice(106,"some descH",6,105));
invoiceList.add(new Invoice(107,"some descE",7,106));
invoiceList.add(new Invoice(108,"some descF",8,107));
invoiceList.add(new Invoice(109,"some descG",9,108));
invoiceList.add(new Invoice(110,"some descI",10,109));
}

public ArrayList<Invoice> getInvoiceList(){
return invoiceList;
}
}
// Invoice Handler
import java.util.ArrayList;

import static java.util.Comparator.comparing;

public class InvoiceHandler {

private final ArrayList<Invoice> invoices;

private InvoiceHandler(){
this.invoices = new InvoiceGenerator().getInvoiceList();
}

private void sortByPrice(){
invoices.stream().sorted(comparing(Invoice::getPrice)).forEach(System.out::println);
}

private void sortByDescription(){
invoices.stream().sorted(comparing(Invoice::getPartDescription)).forEach(System.out::println);
}

private void mappingDescriptionAndPrice(){
invoices.stream().sorted(comparing(Invoice::getQuantity)).map(Invoice::getDescriptionQuantity).forEach(System.out::println);

// sorted method requires a value, so we have sorted on the basis of quantity.
// we can also call it as sorted(invoice -> invoice.getQuantity())
// sorted, map, filter function take in lambda functions which are of the form functionName(input -> {return expression})
}

private void mappingDescriptionAndValue() {
invoices.stream().sorted(comparing(Invoice::getValue)).map(Invoice::getDescriptionTotalPrice).forEach(System.out::println);
}

private void filterByRange(double min, double max) {
invoices.stream().sorted(comparing(Invoice::getValue)).filter(invoice -> invoice.isWithinRange(min, max)).map(Invoice::getDescriptionTotalPrice).forEach(System.out::println);
}

public static void main(String[] args) {
InvoiceHandler invoiceHandler = new InvoiceHandler();
System.out.println("nSorted by description:n");
invoiceHandler.sortByDescription();
System.out.println("nSorted by price:n");
invoiceHandler.sortByPrice();
System.out.println("nMapping each invoice to its description and quantity:n");
invoiceHandler.mappingDescriptionAndPrice();
System.out.println("nMapping each invoice to its description and value:n");
invoiceHandler.mappingDescriptionAndValue();
System.out.println("nFilter value between range and map each Invoice to its description and value:n");
invoiceHandler.filterByRange(200,500);
}

}

Reference no: EM131587518

Questions Cloud

Calculate the amount of cost of goods manufactured : Target Co. had the following beginning and ending inventory balances for the year, Calculate the amount of Cost of Goods Manufactured for the year
Problem related to coinciding os ranks : Let A ? Hn×n. Show that if there exists B ? Hn×n such that BA = I, then A is invertible and B = A-1.
Example of the program writing data : Please include at least one example of a program retrieving data and one example of the program writing data.
What will be company new roe : A company has a profit margin of 10%, an asset turnover ratio of 1.6, and an equity multiplier ratio of 1.65, what will be company’s new ROE?
Create an invoice class with four attributes : Create an Invoice class with 4 attributes: PartNumber (type int), PartDescription (type String), Quantity (type int), and Price (type double)
Analyze and baseline dns traffic : How would you use Wireshark to analyze and baseline DNS traffic? Site an example in detail.
Provides construction equipment-trailers and crutches : Round Table Rental Yards provides construction equipment, trailers, crutches, etc., on short-term rentals.
Research the disclosure of postretirement health care : Using the Internet, go to the FASB Website, and other resources to research the Disclosure of Postretirement Health Care and Life Insurance Benefits
What is the return on equity and equity multiplier : Locker Company has a debt-equity ratio of .7. Return on assets is 7.8 percent. What is the equity multiplier? What is the return on equity?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Derive the class extendedroman from the class roman

For part 'a' the source code for class Roman is attached to this assignment. You do not have to include the method decimalToRoman as you modify this class. Also, class Roman is not used in your test program.

  Write a program to read and add polynomials

Write a program to read and add polynomials - If the exponents are equal, then create a new term with that exponent and the sum of the coefficients, and advance both iterators.

  Leadership position in a hospital emergency room

Identify in a leadership position in a hospital emergency room. Evaluate how  organization handles patient and employee education, and whether that organization participates in any research (if so, find out what types and whether it is mostly qualita..

  Write a program which adds two matrices

Write a program which adds two matrices together and displays the sum - Two arrays will contain user input, and the third array will be used to contain the sum of the two arrays.

  Write an application with three labeled text fields

Write an application with three labeled text fields, one each for the initial amount of a savings account, the annual interest rate, and the number of years

  Illustrate the power of java inheritance and polymorphism

Illustrate the power of Java inheritance and polymorphism. Your response to this question is in multiple parts. You will submit your answers as specified at the end of this question.

  Write a public static method writetokenstolines

Write a public static method called writeTokensToLines that will read an input file one token at a time using Scanner, and write the tokens to a file using a PrintWriter, one token per line, with the tokens numbered starting at 1.

  Using the following data definition classpublic class

using the following data definition classpublic class circle private int radiuspublic circleint r radius rpublic int

  Write a test program that creates 2 shopping carts

Test the program and provide a list of comprehensive test cases used to validate the application and include these test cases in a word document containing all UML diagrams and descriptions. The test data can be shown in a table that includes inpu..

  Write a main method that will simulate playing the game

On the Monty Hall game show, a contestant is given a choice of three doors. Behind one door is a car and behind the other two doors are goats.

  Write a monitor to control the thirsty people and server

The thirsty person who can make a drink from those two items will pick them up and drink a glass of ice water. Write a monitor to control the thirsty people and the server.

  Calculation or a calendar of some activity.

1. Provide Java code for a simple class of your choice. Be sure to include at least one constructor, two methods and two fields. The fields should be private.Create a test class to construct and call the methods of your class. Describe your class and..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd