1 |
import java.util.ArrayList; |
2 |
|
3 |
/** |
4 |
Describes an invoice for a set of purchased products. |
5 |
*/ |
6 |
class Invoice |
7 |
{ |
8 |
/** |
9 |
Constructs an invoice. |
10 |
@param anAddress the billing address |
11 |
*/ |
12 |
public Invoice(Address anAddress) |
13 |
{ |
14 |
items = new ArrayList(); |
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 |
Item anItem = new Item(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 |
+ "\n\nDescription Price Qty Total\n"; |
38 |
for (int i = 0; i < items.size(); i++) |
39 |
{ |
40 |
Item nextItem = (Item)items.get(i); |
41 |
r = r + nextItem.format() + "\n"; |
42 |
} |
43 |
|
44 |
r = r + "\nAMOUNT DUE: $" + getAmountDue(); |
45 |
|
46 |
return r; |
47 |
} |
48 |
|
49 |
/** |
50 |
Computes the total amount due. |
51 |
@return the amount due |
52 |
*/ |
53 |
public double getAmountDue() |
54 |
{ |
55 |
double amountDue = 0; |
56 |
for (int i = 0; i < items.size(); i++) |
57 |
{ |
58 |
Item nextItem = (Item)items.get(i); |
59 |
amountDue = amountDue + nextItem.getTotalPrice(); |
60 |
} |
61 |
return amountDue; |
62 |
} |
63 |
|
64 |
private Address billingAddress; |
65 |
private ArrayList items; |
66 |
} |