📄 shopcart.java
字号:
package com.jc.taobao.gjj.logic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import com.jc.taobao.gjj.logic.CartShopCount;
import com.jc.taobao.gjj.entity.ShopInfo;
public class ShopCart {
private HashMap hs=new HashMap();
public ShopCart()
{
}
public void addShopping(String id,Object obj)//把商品添加到购物车中
{
if(hs.containsKey(id))//如果此映射包含对于指定的键的映射关系,则返回 true
{
CartShopCount item=(CartShopCount)hs.get(id);//返回指定键在此标识哈希映射中所映射的值
item.addShopcount();
}
else
{
CartShopCount newcartcount=new CartShopCount(obj);
hs.put(id, newcartcount);// 在此映射中关联指定值与指定键
}
}
public void remove(String id)//删除购物车中的商品
{
if(hs.containsKey(id))
{
CartShopCount cart=(CartShopCount)hs.get(id);
cart.cancelShopcount();
hs.remove(id);//如果此映射中存在该键的映射关系,则将其删除
}
}
public List getShopItems()//获得购物车中的商品
{
List results=new ArrayList();
Iterator iter=this.hs.values().iterator();//将哈西表中的元素跌代
while(iter.hasNext())//如果仍有元素可以迭代,则返回 true
{
results.add(iter.next());//将跌代的元素添加到List集合中去
}
return results;
}
public double getTotal()//计算购物车中商品的金额
{
double total=0.0;
for(Iterator i=getShopItems().iterator();i.hasNext();)
{
CartShopCount item=(CartShopCount)i.next();//next返回迭代的下一个元素
ShopInfo shop=(ShopInfo)item.getObj();
total+=(item.getShopcount())*(shop.getPrice());//求出商品的金额
}
return roundOff(total);
}
private double roundOff(double x)//总和的结果为浮点型
{
long number=Math.round(x*100);
return number/100.0;
}
public void clearShopCart()//清空购物车中的商品
{
hs.clear();
}
public void modAmount(String id,int count)//更改购物车中商品的数量
{
CartShopCount itemcart=(CartShopCount)hs.get(id);
itemcart.setShopcount(count);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -