您的位置首页百科知识

购物车的Java代码

购物车的Java代码

的有关信息介绍如下:

购物车的Java代码

import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;public class ShoppingCartManager { HashMap hm=new HashMap(); float totlePrice=0; //添加book到购物车 public void addBook(String bookId,String bookQuantity){ if(hm.containsKey(bookId)){ int value=Integer.parseInt(hm.get(bookId)); value+=Integer.parseInt(bookQuantity); hm.put(bookId, value+""); }else{ hm.put(bookId, bookQuantity); } } //修改数量 public void updateQuantity(String bookId,String bookQuantity){ hm.put(bookId, bookQuantity); } //获取购物车的所有信息 并计算总价 public ArrayList getShoppingCart(){ ArrayList al=new ArrayList(); Iterator i=hm.keySet().iterator(); String ids=""; BookTableManager btm=new BookTableManager(); while(i.hasNext()){ ids=ids+","+i.next(); } al= btm.selectByBookIds(ids); totlePrice=0; //清空总价,防止无限累计 for(int j=0;j