简易购物车实体类的设计

一 购物车类(Cart)的设计


 
 
二 代码
1、Cart类
package entity;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//购物车类
public class Cart {
    //购买商品的集合
    private HashMap<Items,Integer> goods;
    
    //购物车的总金额
    private double totalPrice;
    //构造方法
    public Cart()
    {
        goods = new HashMap<Items,Integer>();
        totalPrice = 0.0;
    }
    
    
    public HashMap<Items, Integer> getGoods() {
        return goods;
    }
    public void setGoods(HashMap<Items, Integer> goods) {
        this.goods = goods;
    }
    public double getTotalPrice() {
        return totalPrice;
    }
    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }
    
    //添加商品进购物车的方法
    public boolean addGoodsInCart(Items item ,int number)
    {
        if(goods.containsKey(item))
        {
            goods.put(item, goods.get(item)+number);
        }
        else
        {
            goods.put(item, number);    
        }
        calTotalPrice(); //重新计算购物车的总金额
        return true;
    }
    
    //删除商品的方法
    public boolean removeGoodsFromCart(Items item)
    {
        goods.remove(item);
        calTotalPrice(); //重新计算购物车的总金额
        return true;
    }
    
    //统计购物车的总金额
    public double calTotalPrice()
    {
        double sum = 0.0;
        Set<Items> keys = goods.keySet(); //获得键的集合
        Iterator<Items> it = keys.iterator(); //获得迭代器对象
        while(it.hasNext())
        {
            Items i = it.next();
            sum += i.getPrice()* goods.get(i);
        }
        this.setTotalPrice(sum); //设置购物车的总金额
        return this.getTotalPrice();
    }
    
    public static void main(String[] args) {
        
        //先创建两个商品对象
        Items i1 = new Items(1,"沃特篮球鞋","温州",200,500,"001.jpg");
        Items i2 = new Items(2,"李宁运动鞋","广州",300,500,"002.jpg");
        Items i3 = new Items(1,"沃特篮球鞋","温州",200,500,"001.jpg");
        
        Cart c = new Cart();
        c.addGoodsInCart(i1, 1);
        c.addGoodsInCart(i2, 2);
        //再次购买沃特篮球鞋,购买3双
        c.addGoodsInCart(i3, 3);
        
        
        //变量购物商品的集合
        Set<Map.Entry<Items, Integer>> items= c.getGoods().entrySet();
        for(Map.Entry<Items, Integer> obj:items)
        {
            System.out.println(obj);
        }
        
        
        System.out.println("购物车的总金额:"+c.getTotalPrice());
        
    }
    
}
2、Items类
package  entity;
 
//商品类
public  class  Items {
 
         private  int  id ;  // 商品编号
         private  String  name ;  // 商品名称
         private  String  city ;  // 产地
         private  int  price ;  // 价格
         private  int  number ;  // 库存
         private  String  picture ;  // 商品图片
 
         //保留此不带参数的构造方法
         public  Items()
        {
                
        }
        
         public  Items( int  id,String name,String city, int  price, int  number,String picture)
        {
                 this . id  = id;
                 this . name  = name;
                 this . city  = city;
                 this . picture  = picture;
                 this . price  = price;
                 this . number  = number;
                
        }
         public  int  getId() {
                 return  id ;
        }
 
         public  void  setId( int  id) {
                 this . id  = id;
        }
 
         public  String getName() {
                 return  name ;
        }
 
         public  void  setName(String name) {
                 this . name  = name;
        }
 
         public  String getCity() {
                 return  city ;
        }
 
         public  void  setCity(String city) {
                 this . city  = city;
        }
 
         public  int  getPrice() {
                 return  price ;
        }
 
         public  void  setPrice( int  price) {
                 this . price  = price;
        }
 
         public  int  getNumber() {
                 return  number ;
        }
 
         public  void  setNumber( int  number) {
                 this . number  = number;
        }
 
         public  String getPicture() {
                 return  picture ;
        }
 
         public  void  setPicture(String picture) {
                 this . picture  = picture;
        }
        
        
        
         @Override
         public  int  hashCode() {
                 //  TODO  Auto-generated method stub
                 return  this .getId()+ this .getName().hashCode();
        }
 
         @Override
         public  boolean  equals(Object obj) {
                 //  TODO  Auto-generated method stub
                 if ( this ==obj)
                {
                         return  true ;
                }
                 if (obj  instanceof  Items)
                {
                        Items i = (Items)obj;
                         if ( this .getId()==i.getId()&& this .getName().equals(i.getName()))
                        {
                                 return  true ;
                        }
                         else
                        {
                                 return  false ;
                        }
                }
                 else
                {
                         return  false ;
                }
        }
 
         public  String toString()
        {
                 return  "商品编号:" + this .getId()+ ",商品名称:" + this .getName();
        }
 
}
 
三 测试结果
商品编号:1,商品名称:沃特篮球鞋=4
商品编号:2,商品名称:李宁运动鞋=2
购物车的总金额:1400.0

 

猜你喜欢

转载自cakin24.iteye.com/blog/2396521