码迷,mamicode.com
首页 > 其他好文 > 详细

添加到购物车

时间:2015-10-30 20:16:23      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

private void doAddCar() {
// 1.获取商品bid
int bid = getInt("id");
// 2.获取购物车
List<BookInfo> car = (List<BookInfo>) request.getSession()
.getAttribute("car");
if (car == null) {
car = new ArrayList<BookInfo>();
}
// 3.在购物车找此商品
boolean isFound=false;
for(BookInfo bk:car){
if(bk.getBid()==bid){
bk.setCnt(bk.getCnt()+1);
isFound=true;
break;
}
}
if(isFound=false){
BookInfo bk=dao.findOne(bid);
bk.setCnt(1);
car.add(bk);
}

// 4.保存购物车
request.getSession().setAttribute("car", car);
int sum = 0;// 总数量
double total = 0;// 总价
for (BookInfo bk : car) {
sum += bk.getCnt();
total += bk.getB_price() * bk.getCnt();
}
request.getSession().setAttribute("bookcnt", sum);
request.getSession().setAttribute("booktotal", total);
toMsg("添加购物车成功!", "book.do");
}

添加到购物车

标签:

原文地址:http://www.cnblogs.com/tian114527375/p/4924165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!