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

📄 order.java

📁 这是我修读美国卡耐基梅隆大学Carnegie Mellon University(CMU)课程ssd3:Object-Oriented Programming and Design时完成的课程设计
💻 JAVA
字号:
import java.util.*;

/**
 * The class Order maintains a list of order items.It contains methods to process vectors of {@link OrderItem} objects.
 *
 * @author  张维
 * @version  1.0.0
 * @see  OrderItem
 * @see  Vector
 */
public class Order  {
  	
	 /* Collection of <code>OrderItem</code> objects. */
	 private Vector items;
		
	 /**
	  * Constructs an empty Order.
	  */
	 public Order( ) {
	     	  
	 	  items = new Vector( );
	 }
	 	 
	 /**
	  * Adds the specified order item to the order.
	  *
	  * @param  orderItem the <code>OrderItem</code> object.
	  */
	 public void addItem(OrderItem orderItem) {
	    	
	  	items.add(orderItem);
	 }
	  	  
	 /**
	  * Removes the specified order item from the order. 
	  *
	  * @param orderItem  the <code>OrderItem</code> object.
	  */
	 public void removeItem(OrderItem orderItem) {
	   	
	    items.remove(orderItem);
	 }
	   		   	
	 /**
	  * Returns an iterator over the instances in the items.
	  *
	  * @return an <code>Iterator</code> object.
	  */
	 public Iterator getItemsIterator( ) {
	   		
	    return items.iterator( );
	 }
	   		   	
	 /**
	  * Returns a reference to the OrderItem instance with the specified product. 
	  * Returns null if there are no items in the order with the specified product. 
	  *
	  * @param product  a <code>Product</code> object.
	  * @return  the <code>OrderItem</code> object with the specified <code>product</code>.
	  */
	 public OrderItem getItem(Product product) {
	   	 	
	   	for (Iterator i = getItemsIterator( );i.hasNext( );) {
	   	 	   	
	   	 		 OrderItem orderItem = (OrderItem)i.next( );
	   	 		      
	   	 		 if(orderItem.getProduct( ).equals(product)) {
	   	 		     	
	   	 			return orderItem;
	   	 		  }
	   	 }
	   	 return null;
	 }
	   	    	 
	 /**
	  * Returns the number of instances in the vector items.
	  *
	  * @return the number of <code>OrderItem</code> objects in the vector items.
	  */
	 public int getNumberOfItems( ) {
	   	  	
	   	return items.size( );
	 }
	   	  	   	  
	 /**
	  * Returns the total cost of the order.
	  *
	  * @return the total cost of the order.
	  */
	 public double getTotalCost( ) {
	   	   	
	   	double totalCost = 0.0D;
	   	   	   
	   	for (Iterator i = getItemsIterator( );i.hasNext( );)  {
	   	   	   	
	   	 		OrderItem orderItem = (OrderItem)i.next( );
	   	 		totalCost = totalCost + orderItem.getValue( );
	   	}
	   	return totalCost;
	 }
	 
	 /**
	  * Returns an array of the OrderItem objects (all the items in the current order),
	  * which is used by GourmetCoffeeGUI to populate the order JList. 
	  *
	  * @return  an array of all the items in the current order
    */
	 public OrderItem[ ] getItems( ) {
	 	
        OrderItem aorderitem[ ] = new OrderItem[getNumberOfItems( )];
        int i = 0;
        
        for(Iterator iterator = getItemsIterator( ); iterator.hasNext( );)
            aorderitem[i++] = (OrderItem)iterator.next();

        return aorderitem;
    }
}

⌨️ 快捷键说明

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