添加到购物车
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");
}
posted on 2015-10-30 18:54 kebizhanmushi 阅读(169) 评论(0) 编辑 收藏 举报