思路

实现

放入购物车

购物车取出

清空购物车

代码

商品类

package Shopping;
import java.math.BigDecimal;
public class Product {
//定义商品编号,名称,价格
private int id;
private String name;
private BigDecimal price;
//无参构造方法
public Product() {
		
}
//带参构造方法
public Product(int id, String name, BigDecimal price) {
	super();
	this.id = id;
	this.name = name;
	this.price = price;
}
 //get和set方法
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 BigDecimal getPrice() {
	return price;
}
public void setPrice(BigDecimal price) {
	this.price = price;
}
//重写toString方法
@Override
public String toString() {
	return "商品:  [商品编号:" + id + ",   商品名称:" + name + ",   商品价格:" + price + "]";
}
//重写hashCode方法
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + id;
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	result = prime * result + ((price == null) ? 0 : price.hashCode());
	return result;
}
//重写equals方法
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Product other = (Product) obj;
	if (id != other.id)
		return false;
	if (name == null) {
		if (other.name != null)
			return false;
	} else if (!name.equals(other.name))
		return false;
	if (price == null) {
		if (other.price != null)
			return false;
	} else if (!price.equals(other.price))
		return false;
	return true;
}	
}

购物车

package Shopping;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class shopCart {
	//定义购物车与总价格
	Map<Product,Integer> shopCart;
	BigDecimal totalPrice=BigDecimal.valueOf(0.0);
	//构造方法
public shopCart() {
	//购物车初始化
	shopCart=new HashMap<>();
}
//购物车放入商品
public void add(Product product,int num) {
	if (!shopCart.containsKey(product)) {
		shopCart.put(product, num);
	}else {
		int before=shopCart.get(product);
		int after=before+num;
		shopCart.put(product, after);
	}
	totalPrice = totalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(num)));
}
//购物车拿出商品
public void remove(Product product,int num) {
	int before=shopCart.get(product);
	if (before<=num) {
		shopCart.remove(product);
		totalPrice= totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(before)));
	}else {
		int after=before-num;
		shopCart.put(product, after);
		totalPrice= totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(num)));
		}		
}
//清空购物车
public void clear() {
	shopCart.clear();
	totalPrice= BigDecimal.valueOf(0.0);
}
//查看购物车详单
public void CheckList() {
	System.out.println("商品清单:");
	Set<Product> key=shopCart.keySet();
	
	Iterator<Product> iter=key.iterator();
	while(iter.hasNext()) {
		Product p=iter.next();
		Integer i=shopCart.get(p);
		System.out.println("【数量:"+i+"斤】   "+p+"   单项总价:"+p.getPrice().multiply(BigDecimal.valueOf(i)));
	}
	System.out.println("总价:"+totalPrice);
	}
}

逛超市

package Shopping;
import java.math.BigDecimal;
public class Shopping {
	public static void main(String[] args) {
		//超市的商品
		Product apple=new Product(1001,"苹果",BigDecimal.valueOf(10.2));
		Product orange=new Product(1002,"橘子",BigDecimal.valueOf(10.3));
		Product grape=new Product(1003,"葡萄",BigDecimal.valueOf(10.4));
		Product banana=new Product(1004,"香蕉",BigDecimal.valueOf(10.5));
		//去找了一辆购物车
		shopCart sc=new shopCart();
		//往购物车商品
		sc.add(apple, 10);
		sc.add(orange, 11);
		sc.add(grape, 12);
		sc.add(banana, 10);
		//发现提不起,从购物车拿出商品
		sc.remove(banana, 7);
		sc.remove(apple, 7);
		//发现自己只有1毛钱,把所有物品放回货架,清空购物车
		sc.clear();	
		sc.CheckList();
	}
}