代码欣赏

/**
* 购物车
*/
public ActionForward cart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//此处记得保存rootid
// 获取Cookie
String pids="";
Cookie[] cookies = request.getCookies();
for(Cookie cookie:cookies){
String cookiename = cookie.getName();
System.out.println("cookie.getName()->"+cookiename);
if (cookiename.equals("shopcart")) {
pids=cookie.getValue();
System.out.println("cookie.getValue()->"+pids);
break;
}
}
pids=pids.replaceAll("%7C", "|");
pids=pids.replaceAll("%2C", ",");
System.out.println(pids);
//解析得到商品类别数据结构
Hashtable<Integer,Integer> p = new Hashtable<Integer,Integer>();
Hashtable<Integer,Integer> pl = new Hashtable<Integer,Integer>();
String pkey=null;
String pnum=null;
String po = null;
if(pids !=null){
StringTokenizer st = new StringTokenizer(pids,"|");
while(st.hasMoreTokens()){
String s = st.nextToken();
StringTokenizer st2 = new StringTokenizer(s,",");
int idx=0;
while(st2.hasMoreTokens()){
String ss = st2.nextToken();
//商品的ID
if (idx==0) {
pkey=ss;
System.out.println(ss);
}else if (idx==2) {
pnum=ss;
System.out.println(ss);
}else{
po = ss;
}
idx++;
}
//保存到数据结构
p.put(Integer.valueOf(pkey), Integer.valueOf(pnum));
pl.put(Integer.valueOf(pkey), Integer.valueOf(po));
}
//循环完毕得到所有的商品ID与对应的数量
}
//查询商品并结算金额
List<ECartProduct> lstr = new Vector<ECartProduct>();
//ProductDAO daop = new ProductDAO();
Double total = 0d;
Enumeration<Integer> en = p.keys();
while(en.hasMoreElements()){
Integer id = en.nextElement();
//访问DAO得到商品信息
ECartProduct pt = daop.getById(id);
pt.setCount(p.get(pt.getId()));
pt.setTotal(pt.getPrice()*pt.getCount());
System.out.println("pt.getTotal()->"+pt.getTotal());
pt.setCid(pl.get(pt.getId()));
//计算单价,并累加
if (pt!=null) {
lstr.add(pt);
total = total+pt.getTotal();
}
}
ECarts cart = new ECarts(lstr,total);
request.setAttribute("book", cart.getCart());
request.setAttribute("amount", cart.getTotal());
return mapping.findForward("cart");
}

/**
* 订单结算
*/
public ActionForward checkOut(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//此处记得保存rootid
String uid = request.getParameter("uid");
String total = request.getParameter("total");

return mapping.findForward("checkout");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值