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