[java]代码库
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class ShoppingCart {//购物车
//key:商品编号 value:商品条目
private Map<Integer,BuyerItem> map=new LinkedHashMap<Integer,BuyerItem>();
public void addSku(Sku p){//添加商品
int SkuId=p.getSkuId();
if(map.containsKey(SkuId)){
BuyerItem BuyerItem=map.get(SkuId);
BuyerItem.setCount(BuyerItem.getCount()+1);
}else{
map.put(SkuId, new BuyerItem(p,1));
}
}
public void showAll(){//查看订单信息
Collection<BuyerItem> BuyerItems = map.values();
Iterator<BuyerItem> iterator = BuyerItems.iterator();
while(iterator.hasNext()){
BuyerItem BuyerItem = iterator.next();
Sku Sku = BuyerItem.getSku();
System.out.println("商品序号:"+Sku.getSkuId()+",商品名称:"
+Sku.getSkuName()+",单价:"+Sku.getPrice()+",数量:"+BuyerItem.getCount()
+",小计:"+BuyerItem.totalMoney());
}
}
public boolean deleteSku(int SkuId){//删除商品
if(map.containsKey(SkuId)){
map.remove(SkuId);
return true;
}
return false;
}
public boolean modifySku(int SkuId,int count){//修改
if(map.containsKey(SkuId)){
if(count>=1){
BuyerItem BuyerItem = map.get(SkuId);
BuyerItem.setCount(count);
return true;
}else if(count==0){//删除该商品
deleteSku(SkuId);
return true;
}
}
return false;
}
public void clearCart(){//清空购物车
map.clear();
}
public void Submit() {
System.out.println("订单已提交");
}
public double totalAllMoney(){//商品总钱数
double total=0;
Collection<BuyerItem> BuyerItems = map.values();
Iterator<BuyerItem> iterator = BuyerItems.iterator();
while(iterator.hasNext()){
BuyerItem BuyerItem = iterator.next();
double money=BuyerItem.totalMoney();
total+=money;
}
return total;
}
}
[源代码打包下载]
初级程序员
by: Sube 发表于:2018-10-01 17:46:43 顶(0) | 踩(0) 回复
这个代码好棒呦
回复评论