Now we need the
Create a new class named
Add the four fields for this class to the class. If you are wondering where the fourth field is coming from, you have forgotten that association relationships are also fields --- there is a field for the
items association that is a collection.
Add getters and setters.
removeItem methods --- they should add/remove the passed
InvoiceItem parameter to/from the
Don't forget to initialise the
items field unless you enjoy debugging null-pointer exceptions. You should remember from INFO201 that
HashSet should be your default collection rather than
getTotal method --- it should return the sum of all of the item totals for the items in the
You can do this quickly using the following code:
return items.stream().mapToDouble(item -> item.getItemTotal()).sum();
This is using a combination of lambda expressions and sequence streams (both of which have been in Java since Java 8) to iterate through all of the items, calling the
getItemTotal on each, and summing the result to get the invoice total. We could have done this with a loop, but learning new techniques is how we gain experience. The old adage that Java is a verbose language hasn't been true for close to a decade.