一个小型的购物商店
源代码在线查看: cart.java
/* * Cart.java * * Created on 2006年9月14日, 下午2:11 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package com.shopping.model; import java.util.*; /** * * @author 曹昊 */ public class Cart { private Map items = new HashMap(); private double cost = 0.0;//总价格 public Cart(){} public void addItem(Item item){ items.put(item.getProduct().getId()+"",item); } public void modifyNumberByProductId(String productId, int number){ Item item = (Item)items.get(productId); item.setNumber(number); item.incrementCost(); } public void deleteItemByProductId(String productId){ items.remove(productId); } public void deleteItemsByProductId(String[] productId){ for(int i = 0; i< productId.length; i++){ items.remove(productId[i]); } } public double getCost(){ cost = 0.0; Iterator it = items.values().iterator(); while(it.hasNext()){ Item item = (Item)it.next(); cost += item.getCost(); } return cost; } public void setCost(double cost){ this.cost = cost; } public void clear(){ if(items != null){ items.clear(); } setCost(0.0); } public Map getItems(){ return this.items; } public boolean isEmpty(){ return items.size() == 0; } }