01: /**
02:    Describes a quantity of an article to purchase.
03: */
04: public class LineItem
05: {  
06:    /**
07:       Constructs an item from the product and quantity.
08:       @param aProduct the product
09:       @param aQuantity the item quantity
10:    */
11:    public LineItem(Product aProduct, int aQuantity)
12:    {  
13:       theProduct = aProduct;
14:       quantity = aQuantity;
15:    }
16:    
17:    /**
18:       Computes the total cost of this line item.
19:       @return the total price
20:    */
21:    public double getTotalPrice()
22:    {  
23:       return theProduct.getPrice() * quantity;
24:    }
25:    
26:    /**
27:       Gets the product that this item describes.
28:       @return the product
29:    */
30:    public Product getProduct()
31:    {  
32:       return theProduct;
33:    }
34: 
35:    /**
36:       Gets the quantity of the product that this item describes.
37:       @return the quantity
38:    */
39:    public int getQuantity()
40:    {  
41:       return quantity;
42:    }
43: 
44:    /**
45:       Formats this item.
46:       @return a formatted string of this item
47:    */
48:    public String format()
49:    {  
50:       return String.format("%-30s%8.2f%5d%8.2f", 
51:             theProduct.getDescription(), theProduct.getPrice(), 
52:             quantity, getTotalPrice());
53:    }
54: 
55:    private int quantity;
56:    private Product theProduct;
57: }