Excercise Create an Invoice class with 4 attributes: PartNumber (type int), Part
ID: 3811624 • Letter: E
Question
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
// Invoice Generator
// Invoice Handler
Explanation / Answer
Consider the following example to understand the use of stream:
List list = Arrays.asList("one","two","three","four");
Stream stream = list.stream().sorted();
List listSorted = stream.collect(Collectors.toList());
for (String s : listSorted) {
System.out.println(s);
}
Here it sortes the listt and display the result. But, we can even reduce the operation using consumers.
So, the result of it is:
List listStr = Arrays.asList("one","two","three","four");
listStr.stream().sorted().foreach(str -> System.out.println(str));
Here, the output of sorted is passed to foreach where consumer is output of list stream
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.