Collections and Generics
Invoice app
Invoice class
The code for the Invoice class
import java.text.NumberFormat; import java.util.ArrayList; public class Invoice { // the instance variable private ArrayListlineItems; // the constructor public Invoice() { lineItems = new ArrayList (); } // a method that adds a line item public void addItem(LineItem lineItem) { this.lineItems.add(lineItem); } // the get accessor for the line item collection public ArrayList getLineItems() { return lineItems; } // a method that gets the invoice total public double getInvoiceTotal() { double invoiceTotal = 0; for (LineItem lineItem : this.lineItems) { invoiceTotal += lineItem.getTotal(); } return invoiceTotal; } // a method that returns the invoice total in currency format public String getFormattedTotal() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return currency.format(this.getInvoiceTotal()); } }