`
seesai
  • 浏览: 7098 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

代码欣赏

阅读更多
/**
* 购物车
*/
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");
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics