learning about lambdas and streams in my java class and trying to get this one specific part figured out.
Here is our assignment: Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results:
a. Use streams to sort the Invoice objects by partDescription, then display the results.
b. Use streams to sort the Invoice objects by pricePerItem, then display the results.
c. Use streams to map each Invoice to its partDescription and quantity, sort the results by quantity, then display the results
d. Use streams to map each Invoice to its partDescription and the value of the Invoice (i.e., quantity * pricePerItem). Order the results by Invoice value.
e. Modify Part (d) to select the Invoice values in the range $200.00 to $500.00.
f. Find any one Invoice in which the partDescription contains the word “saw”.
Where I'm at: So I've got a) and b) down but I'm a little confused with part c). I haven't found anything online or in my book that suggests you can map an object to more than one of its own attributes. I've seen one example of this project where someone created a separate function where they created a String of the two elements combined, but I don't think my professor will give points for that because he said no modifications of his Invoice class. I'm wondering if he wants us to use a lambda to modify the toString method of Invoice, but that doesn't quite seem right because then we technically wouldn't be mapping the object of both attributes, just mapping to one and changing its output. Find his code (can't modify) below, and my code that I have so far.
Professor's Code (can't be modified):
public class Invoice {
private final int partNumber;
private final String partDescription;
private int quantity;
private double price;
// constructor
public Invoice(int partNumber, String partDescription, int quantity, double price)
{
if (quantity < 0) { // validate quantity
throw new IllegalArgumentException("Quantity must be>= 0");
}
if (price < 0.0) { // validate price
throw new IllegalArgumentException(
"Price per item must be>= 0");
}
this.partNumber = partNumber;
this.partDescription = partDescription;
this.quantity = quantity;
this.price = price;
}
// get part number
public int getPartNumber() {
return partNumber; // should validate
}
// get description
public String getPartDescription() {
return partDescription;
}
// set quantity
public void setQuantity(int quantity) {
if (quantity <0) { // validate quantity
throw new IllegalArgumentException("Quantity must be>= 0");
}
this.quantity = quantity;
}
// get quantity
public int getQuantity() {
return quantity;
}
// set price per item
public void setPrice(double price) {
if (price <0.0) { // validate price
throw new IllegalArgumentException(
"Price per item must be>= 0");
}
this.price = price;
}
// get price per item
public double getPrice() {
return price;
}
// return String representation of Invoice object
@Override
public String toString() {
return String.format(
"Part #: %-2d Description: %-15s Quantity: %-4d Price: $%,6.2f",
getPartNumber(), getPartDescription(),
getQuantity(), getPrice());
}
}
**Here's my code so far: **
// import statements
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class InvoiceDriver {
//***************************************************************
// Method: developerInfo
// Description: The developer information method of the program
// Parameters: none
// Returns: n/a
//**************************************************************
public static void developerInfo() {
System.out.println("");
System.out.println("*************************************************");
System.out.println ("Name: Allison Crenshaw");
System.out.println ("Course: ITSE 2317 Intermediate Java Programming");
System.out.println ("Program: Five");
System.out.println("*************************************************");
} // End of developerInfo
public static void main(String[] args) {
// variables
Invoice[] invoices = {
new Invoice(83, "Electric sander",
7, 57.98),
new Invoice(24,"Power saw",
18, 99.99),
new Invoice(7, "Sledge hammer",
11, 21.50),
new Invoice(77, "Hammer",
76, 11.99),
new Invoice(39, "Lawn mower",
3, 79.50),
new Invoice(68, "Screwdriver",
106, 6.99),
new Invoice(56, "Jig saw",
21, 11.00),
new Invoice(3, "Wrench",
34, 7.50)};
// display developer info
developerInfo();
// welcome message
System.out.println("Welcome to this Invoice Program.");
System.out.println("This program receives invoice information " +
"and displays");
System.out.println("the info based on various sorts using lambdas " +
"and streams.");
System.out.println();
// get list view of Invoices and use to stream and print
List<Invoice> list = Arrays.asList(invoices);
// use a st
// a) use streams to sort the invoices by descriptions, then display
System.out.println("Invoices sorted by description: ");
Arrays.stream(invoices)
.sorted(Comparator.comparing(Invoice::getPartDescription))
.forEach(System.out::println);
System.out.println();
// b) use streams to sort the invoices by price, then display
System.out.println("Invoices sorted by price: ");
Arrays.stream(invoices)
.sorted(Comparator.comparing(Invoice::getPrice))
.forEach(System.out::println);
System.out.println();
// c) use streams to map each invoice to its description and quantity,
// sort the results by quantity, then display the results
System.out.println("Invoices mapped to description and quantity " +
"and sorted by quantity: ");
list.stream()
.map(Invoice::getPartDescription)
.forEach(System.out::println);
// d) use streams to map each invoice to its description and the
// value of the invoice (quantity * price) then order by value
// e) modify part d) to select the invoice values in range $200-$500
// f) find any one invoice in which description contains the word "saw"
} // main
} // end InvoiceDriver
**Here's what the output is supposed to look like for part c): **
Invoices mapped to description and quantity:
Description: Lawn mower Quantity: 3
Description: Electric sander Quantity: 7
Description: Sledge hammer Quantity: 11
Description: Power saw Quantity: 18
Description: Jig saw Quantity: 21
Description: Wrench Quantity: 34
Description: Hammer Quantity: 76
Description: Screwdriver Quantity: 106