01: import java.util.ArrayList;
02: 
03: /**
04:    Describes an invoice for a set of purchased products.
05: */
06: public class Invoice
07: {  
08:    /**
09:       Constructs an invoice.
10:       @param anAddress the billing address
11:    */
12:    public Invoice(Address anAddress)
13:    {  
14:       items = new ArrayList<LineItem>();
15:       billingAddress = anAddress;
16:    }
17:   
18:    /**
19:       Adds a charge for a product to this invoice.
20:       @param aProduct the product that the customer ordered
21:       @param quantity the quantity of the product
22:    */
23:    public void add(Product aProduct, int quantity)
24:    {  
25:       LineItem anItem = new LineItem(aProduct, quantity);
26:       items.add(anItem);
27:    }
28: 
29:    /**
30:       Formats the invoice.
31:       @return the formatted invoice
32:    */
33:    public String format()
34:    {  
35:       String r =  "                     I N V O I C E\n\n"
36:             + billingAddress.format()
37:             + String.format("\n\n%-30s%8s%5s%8s\n",
38:                "Description", "Price", "Qty", "Total");
39: 
40:       for (LineItem i : items)
41:       {  
42:          r = r + i.format() + "\n";
43:       }
44: 
45:       r = r + String.format("\nAMOUNT DUE: $%8.2f", getAmountDue());
46: 
47:       return r;
48:    }
49: 
50:    /**
51:       Computes the total amount due.
52:       @return the amount due
53:    */
54:    public double getAmountDue()
55:    {  
56:       double amountDue = 0;
57:       for (LineItem i : items)
58:       {  
59:          amountDue = amountDue + i.getTotalPrice();
60:       }
61:       return amountDue;
62:    }
63:    
64:    private Address billingAddress;
65:    private ArrayList<LineItem> items;
66: }