Reputation: 31
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
Upvotes: 1
Views: 3909
Reputation: 2335
Here is the solution. Looks like you already understand streams and lambdas well. So, I haven't added too much explanation in the code comments. Let me know if you need any explanations.
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class InvoiceProcessor {
//MAIN METHOD - START HERE !!!
public static void main(String[] args) {
List<Invoice> invoices = getInvoices();
System.out.println("\nQuestion A:");
partA(invoices);
System.out.println("\nQuestion B:");
partB(invoices);
System.out.println("\nQuestion C:");
partC(invoices);
System.out.println("\nQuestion D:");
partD(invoices);
System.out.println("\nQuestion E:");
partE(invoices);
System.out.println("\nQuestion F:");
partF(invoices);
}
//Generate some sample invoices to use in our code - using lambdas and streams!
public static List<Invoice> getInvoices(){
List<String> partNames = Arrays.asList("Lawn mower", "Electric sander", "Sledge hammer",
"Power saw", "Jig saw", "Wrench", "Hammer", "Screwdriver");
List<Invoice> invoices = IntStream
.range(0, partNames.size())
//Use each number to generate Invoices,i.e. 1 number is MAP-ped to 1 invoice.
.mapToObj(n -> new Invoice(n, partNames.get(n), (n%3)+1, (n+1) * 50.0))
//Collect all the invoices in a list.
.collect(Collectors.toList());
return invoices;
}
//a. Use streams to sort the Invoice objects by partDescription, then display the results.
public static void partA(List<Invoice> invoices){
invoices.stream()
.sorted(Comparator.comparing(Invoice::getPartDescription))
.forEach(System.out::println);
}
//b. Use streams to sort the Invoice objects by pricePerItem, then display the results.
public static void partB(List<Invoice> invoices){
invoices.stream()
.sorted(Comparator.comparing(Invoice::getPrice))
.forEach(System.out::println);
}
//c. Use streams to map each Invoice to its partDescription and quantity, sort the results by quantity,
// then display the results.
public static void partC(List<Invoice> invoices){
//Do we really need to do any mapping here?
invoices.stream()
.sorted(Comparator.comparing(Invoice::getQuantity))
.forEach(i -> System.out.println("Description: " + i.getPartDescription() + " Quantity: " + i.getQuantity())
);
}
//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.
public static void partD(List<Invoice> invoices){
//Do we really need to do any mapping here?
invoices.stream()
.sorted( Comparator.comparingDouble(i -> i.getQuantity() * i.getPrice() ) )
.forEach(i -> System.out.println("Description: " + i.getPartDescription() + " Invoice value: " + i.getQuantity() * i.getPrice())
);
}
//e. Modify Part (d) to select the Invoice values in the range $200.00 to $500.00.
public static void partE(List<Invoice> invoices){
invoices.stream()
.filter(i -> 200.0 <= i.getQuantity() * i.getPrice() && i.getQuantity() * i.getPrice() <= 500.0)
.forEach(i -> System.out.println("Description: " + i.getPartDescription() + " Invoice value: " + i.getQuantity() * i.getPrice()));
}
//f. Find any one Invoice in which the partDescription contains the word "saw".
public static void partF(List<Invoice> invoices){
Invoice saw = invoices.stream()
.filter(i -> i.getPartDescription().contains("saw"))
.findFirst()
.get();
System.out.println(saw);
}
}
Upvotes: 1
Reputation: 369458
You can map the element to anything you want, including, for example, a String
:
list.
stream().
sorted(Comparator.comparing(Invoice::getQuantity)).
map(invoice ->
String.format(
"Description: %-15s Quantity: %-4d",
invoice.getPartDescription(),
invoice.getQuantity()
)
).
forEach(System.out::println);
Upvotes: 1
Reputation: 31878
The way you have mapped the Stream
, you would only be able to access the partDescription
and not the quantity
at the same time.
.map(Invoice::getPartDescription) // changed to Stream<String> from Stream<Invoice>
Use streams to map each Invoice to its partDescription and quantity, sort the results by quantity, then display the results
Thinking around to keep a tuple of both these attributes at the same time, a Map
could be useful to collect the information for further processing(sorting). This would look like:
list.stream()
.collect(Collectors.toMap(Invoice::getPartDescription,
Invoice::getQuantity)) // map description to its quantity
.entrySet().stream()
.sorted(Map.Entry.comparingByValue()) // sort by quantity (values of map)
.forEach(e -> System.out.println("Description: " + e.getKey() + " Quantity: " + e.getValue()));
.filter
to selectively process the elements.filter
, you need to findAny
for section 'f' while you filter based on the given condition around the description.Upvotes: 0