orderitem.java

来自「这是我修读美国卡耐基梅隆大学Carnegie Mellon University」· Java 代码 · 共 85 行

JAVA
85
字号
/**
 * This class models an item in an order.It contains the following information:
 * </p>
 * <ol>
 * <li>the reference to a {@link Product} object</li>
 * <li>the quantity of the product in the order,a <code>int</code></li>
 * </ol>
 *
 * @author   张维
 * @version  1.0.0
 */
public class OrderItem {

   /* Reference to a Product object */
   private Product product;

   /* Quantity of the product in the order */
   private int quantity; 

   /**
    * Constructs a <code>OrderItem</code> object.
    *
    * @param  initialProduct  the reference to a Product object.
    * @param  initialQuantity  the quantity of the product in the order.
    */
   public OrderItem(Product initialProduct,int initialQuantity) {
        
          product = initialProduct;
          quantity = initialQuantity;
   }

   /**
    * Returns the value of the instance variable product, a reference to a Product object. 
    *
    * @return  the value of the instance variable product, a reference to a Product object.
    */
    public Product getProduct( ) {

       return  product;
    }

    /**
     * Returns the value of the instance variable quantity. 
     *
     * @return  the value of the instance variable quantity.
     */
    public int getQuantity( ) {

       return  quantity;
    }

   
    /**
     * Sets the instance variable <code>quantity</code> to the value of parameter newQuantity. 
     *
     * @param newQuantity  the new quantity.
     */
    public void setQuantity(int newQuantity) {

       quantity = newQuantity;
    }

    /**
     * Returns the product of quantity and price. 
     *
     * @return  the product of quantity and price. 
     */
    public double getValue( ) {
        
       double value = getProduct( ).getPrice( ) * (double)getQuantity( );
       return value;
    }

    /**
     * Returns the string representation of an OrderItem object.
     *
     * @return  the string representation of an OrderItem object.
     */
    public String toString( ) {

        return  getQuantity( ) + " " + getProduct( ).getCode( ) + " " + getProduct( ).getPrice( );
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?