如圖所示:
Product(綠色框)是每件商品的信息,對應數據庫中的product每一項,不是重點
CartItem(紅色框)是每一個購物項,也就是你點擊加入購物車的所有信息,包括Product,還有購買數量,和購買這個商品的總價格 重點
Cart(藍色框)是購物車,也是你本次購買所有商品的總的信息,包括CartItem,和所有購物項的總金額 重點
CartItem實體類:
public class CartItem {//購物項 private Product product;//這個購物項中的商品信息 private int buyNum;//購買數量 private double subtotal;//總價格 public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public int getBuyNum() { return buyNum; } public void setBuyNum(int buyNum) { this.buyNum = buyNum; } public double getSubtotal() { return subtotal; } public void setSubtotal(double subtotal) { this.subtotal = subtotal; } }
Cart實體類:
import java.util.HashMap; import java.util.Map; public class Cart {//購物車 //該購物車中存儲的n個購物項 用Map集合是為了之后業務操作 //Map中的key是String類型,存放的是購物項中商品的pid(主鍵)為了之后多次加入該商品方便累加 private Map<String,CartItem> cartItems = new HashMap<String,CartItem>(); //n個購物項的總計 private double total; public Map<String, CartItem> getCartItems() { return cartItems; } public void setCartItems(Map<String, CartItem> cartItems) { this.cartItems = cartItems; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } }
商品信息頁面:
<script type="text/javascript"> function addCart(){ //獲得購買的商品的數量 var buyNum = $("#buyNum").val(); location.href="${pageContext.request.contextPath}/product?method=addProductToCart&pid=${product.pid}&buyNum="+buyNum; } </script> <div class="col-md-6"> <div> <strong>${product.pname}</strong> </div> <div style="border-bottom: 1px dotted #dddddd; width: 350px; margin: 10px 0 10px 0;"> <div>編號:${product.pid}</div> </div> <div style="margin: 10px 0 10px 0;"> 億家價: <strong style="color: #ef0101;">¥:${product.shop_price}元/份</strong> 參 考 價: <del>¥${product.market_price}元/份</del> </div> <div style="margin: 10px 0 10px 0;"> 促銷: <a target="_blank" title="限時搶購 (2014-07-30 ~ 2015-01-01)" style="background-color: #f07373;">限時搶購</a> </div> <div style="padding: 10px; border: 1px solid #e7dbb1; width: 330px; margin: 15px 0 10px 0;; background-color: #fffee6;"> <div style="margin: 5px 0 10px 0;">白色</div> <div style="border-bottom: 1px solid #faeac7; margin-top: 20px; padding-left: 10px;"> 購買數量: <input id="buyNum" name="buyNum" value="1" maxlength="4" size="10" type="text"> </div> <div style="margin: 20px 0 10px 0;; text-align: center;"> <a href="javascript:void(0);" onclick="addCart()"> <input style="background: url('./images/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0); height: 36px; width: 127px;" value="加入購物車" type="button"> </a> 收藏商品 </div> </div> <div> <a href="${pageContext.request.contextPath}/product?method=productList&cid=${cid }¤tPage=${currentPage}">返回列表頁面</a> </div> </div> </div>
點擊加入購物車的邏輯:
public void addProductToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ProductService service = new ProductService(); //獲得要放到購物車的商品的pid String pid = request.getParameter("pid"); //獲得該商品的購買數量 int buyNum = Integer.parseInt(request.getParameter("buyNum")); //獲得product對象 Product product = service.findProductByPid(pid); //計算小計 double subtotal = product.getShop_price()*buyNum; //封裝CartItem CartItem item = new CartItem(); item.setProduct(product); item.setBuyNum(buyNum); item.setSubtotal(subtotal); //獲得購物車---判斷是否在session中已經存在購物車 Cart cart = (Cart) session.getAttribute("cart"); if(cart==null){ cart = new Cart(); } //將購物項放到車中---key是pid //先判斷購物車中是否已將包含此購物項了 ----- 判斷key是否已經存在 //如果購物車中已經存在該商品----將現在買的數量與原有的數量進行相加操作 Map<String, CartItem> cartItems = cart.getCartItems(); double newsubtotal = 0.0; if(cartItems.containsKey(pid)){ //取出原有商品的數量 CartItem cartItem = cartItems.get(pid); int oldBuyNum = cartItem.getBuyNum(); oldBuyNum+=buyNum; cartItem.setBuyNum(oldBuyNum); cart.setCartItems(cartItems); //修改小計 //原來該商品的小計 double oldsubtotal = cartItem.getSubtotal(); //新買的商品的小計 newsubtotal = buyNum*product.getShop_price(); cartItem.setSubtotal(oldsubtotal+newsubtotal); }else{ //如果車中沒有該商品 cart.getCartItems().put(product.getPid(), item); newsubtotal = buyNum*product.getShop_price(); } //計算總計 double total = cart.getTotal()+newsubtotal; cart.setTotal(total); //將車再次訪問session session.setAttribute("cart", cart); //直接跳轉到購物車頁面 response.sendRedirect(request.getContextPath()+"/cart.jsp"); }