添加到购物车

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");
}

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