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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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