1 |
/** |
2 |
Describes a quantity an article to purchase and its price. |
3 |
*/ |
4 |
class Item |
5 |
{ |
6 |
/** |
7 |
Constructs an item from the product and quantity |
8 |
@param aProduct the product |
9 |
@param aQuantity the item quantity |
10 |
*/ |
11 |
public Item(Product aProduct, int aQuantity) |
12 |
{ |
13 |
theProduct = aProduct; |
14 |
quantity = aQuantity; |
15 |
} |
16 |
|
17 |
/** |
18 |
Computes the total cost of this item. |
19 |
@return the total price |
20 |
*/ |
21 |
public double getTotalPrice() |
22 |
{ |
23 |
return theProduct.getPrice() * quantity; |
24 |
} |
25 |
|
26 |
/** |
27 |
Formats this item. |
28 |
@return a formatted string of this item |
29 |
*/ |
30 |
public String format() |
31 |
{ |
32 |
final int COLUMN_WIDTH = 30; |
33 |
String description = theProduct.getDescription(); |
34 |
|
35 |
String r = description; |
36 |
|
37 |
// pad with spaces to fill column |
38 |
|
39 |
int pad = COLUMN_WIDTH - description.length(); |
40 |
for (int i = 1; i <= pad; i++) |
41 |
r = r + " "; |
42 |
|
43 |
r = r + theProduct.getPrice() |
44 |
+ " " + quantity |
45 |
+ " " + getTotalPrice(); |
46 |
|
47 |
return r; |
48 |
} |
49 |
|
50 |
private int quantity; |
51 |
private Product theProduct; |
52 |
} |