解决方案 »

  1.   

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */package shopcart.dto;
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;/**
     *
     * @author MR-XIE
     */
    public class ShopCart implements Serializable  {
        public ShopCart()
        {
            
        }
        private List cart=null;
        public void addProductToCart(Product product)
        {
            if(cart==null)
                cart=new ArrayList();
            Iterator it=cart.iterator();
            while(it.hasNext())
            {
                Product item=(Product) it.next();
                if(item.getId().equals(product.getId()))
                {
                    return;
                }
            }
            cart.add(product);
        }
            public void removeProductFromCart(String productId)
        {
            if (cart == null)
            {
                return;
            }
            Iterator it = cart.iterator();
            while (it.hasNext())
            {
                Product item = (Product) it.next();
                if (item.getId().equals(productId))
                {
                    it.remove();
                    return;
                }
            }
        }
        public double getAllProductPrice()
        {
            if(cart==null)
                return 0;
            double totalPrice=0;
            Iterator it=cart.iterator();
            while(it.hasNext())
            {
                Product item=(Product) it.next();
                totalPrice +=item.getPrice();
            }
            return totalPrice;
        }
        public List getAllProductsFromCart()
            {
                return cart;
            }
        }
    不知道为什么数据过不来
      

  2.   

    Quote: 引用 1 楼 jintianhen1 的回复:

    代码太长了,用debug调试吧