LineItem.java.html

  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        Formats this item.
 31        @return a formatted string of this item
 32     */
 33     public String format()
 34     {  
 35        return String.format("%-30s%8.2f%5d%8.2f", 
 36           theProduct.getDescription(), theProduct.getPrice(), 
 37           quantity, getTotalPrice());
 38     }
 39  }