⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 order.java

📁 ssd2 exercise4 的题目和答案
💻 JAVA
字号:
import java.util.*;

/**
 * Maintains the information of a list of order items. Contains a
 * collection of {@link OrderItem} objects.
 *
 * @author Libo Du
 * @version  1.0.0
 * @see OrderItem
 */
public class Order implements Iterable<OrderItem>{

	/* Collection of <code>OrderItem</code> objects.*/
	private ArrayList<OrderItem> items;
	
	/**
     * Constructs an collection <code>items</code>, which is initially empty. 
     */
    public Order(){
        
    	items = new ArrayList<OrderItem>();
    }

    /**
     * Adds the specified {@link OrderItem} to the collection <code>items</code>. 
     * 
     * @param orderItem  the specified {@link OrderItem} object.
     */
    public void addItem(OrderItem orderItem){
        
    	items.add(orderItem);
    }

    /**
     * Removes the specified {@link OrderItem} from the collection <code>items</code>. 
     * 
     * @param orderItem  the specified {@link OrderItem} object.
     */
    public void removeItem(OrderItem orderItem){
        
    	items.remove(orderItem);
    }

    /**
     * Returns an iterator over the instances in the collection <code>items</code>.
     * 
     * @return  an {@link Iterator} of {@link OderItem}
     */
    public Iterator<OrderItem> iterator(){
        
    	return items.iterator();
    }

    /**
     * Returns a reference to the {@link OrderItem} instance with the 
     * specified <code>product</code>.
     * Returns <code>null</code> if there are no items 
     * in the order with the specified <code>product</code>. 
     * 
     * @param product  an intance of {@link Product}.
     * @return  The {@link OrderItem} object with the specifed
     *          <code>product</code>. Returns <code>null</code> if the object with
     *          the <code>product</code> is not found.
     */
    public OrderItem getItem(Product product){
        
    	for(Iterator<OrderItem> itr = items.iterator(); itr.hasNext();){
            
    		OrderItem orderitem = itr.next();
            
            if(orderitem.getProduct().equals(product))
                
            	return orderitem;
        }

        return null;
    }

    /**
     * Returns the number of instances in the collection <code>items</code>.
     *
     * @return the number of {@link OrderItem} objects in this order.
     */
    public int getNumberOfItems(){
        
    	return items.size();
    }

    /**
     * Returns the total cost of the order.
     *
     * @return the total cost of the {@link Order}.
     */
    public double getTotalCost(){
        
    	double total = 0.0D;
        
        for(Iterator<OrderItem> itr = items.iterator(); itr.hasNext();){
            
            total += itr.next().getValue();
        }

        return total;
    }
}

⌨️ 快捷键说明

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