购物车模块
Posted menbo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车模块相关的知识,希望对你有一定的参考价值。
1.购物车中存放每条记录(不同类商品)及其购买数量的类
public class BuyItem { private Sku sku; private Integer amount = 1; public Sku getSku() { return sku; } public void setSku(Sku sku) { this.sku = sku; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((amount == null) ? 0 : amount.hashCode()); result = prime * result + ((sku == null) ? 0 : sku.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; BuyItem other = (BuyItem) obj; if (sku == null) { if (other.sku != null) return false; } else if (!sku.getId().equals(other.sku.getId())) return false; return true; } }
2.存放BuyItem的类
public class BuyCart { List<BuyItem> items = new ArrayList<BuyItem>(); private Integer productId; //添加方法 public void addItem(BuyItem item){ if(items.contains(item)){ for (BuyItem buyItem : items) { if(buyItem.equals(item)){ int result = 0; if(item.getAmount()!=null && buyItem.getAmount()!=null){ result = item.getAmount()+buyItem.getAmount(); } if(buyItem.getSku().getSkuUpperLimit()>=result ){ buyItem.setAmount(result); }else{ buyItem.setAmount(buyItem.getSku().getSkuUpperLimit()); } } } }else{ items.add(item); } } /** * 小计 直接写在购物车中 * @return */ //获得物品个数 @JsonIgnore //因为有返回参数在将其转为json格式时会出现异常 public int getProductAmount(){ int result = 0; for (BuyItem buyItem : items) { result += buyItem.getAmount(); } return result; } //商品金额 @JsonIgnore public double getProductPrice(){ double result = 0.0; for (BuyItem buyItem:items) { result+=buyItem.getSku().getSkuPrice()*buyItem.getAmount(); } return result; } //运费 @JsonIgnore public Double getFee(){ double result = 0.0; if(getProductPrice()<=39){ result = 10; } return result; } //应付金额 @JsonIgnore public Double getTotalPrice(){ return getFee()+getProductPrice(); } //清空购物车 public void clearCart(){ items.clear(); } public void deleteItem(BuyItem buyItem){ items.remove(buyItem); } public Integer getProductId() { return productId; } public void setProductId(Integer productId) { this.productId = productId; } public List<BuyItem> getItems() { return items; } }
3.controller层 商品添加购物车,购买,清除购物车等
@Controller public class CardController { //1.获得所有cookie,检测cookie中是否存有购物车 //2.如果没有则new一个,有则获取到cookie中的购物车(ObjectMapper) //3.给buyItem中的sku和amout赋值,将buyItem加入到购物车buyCart中 //4.将购物车存放到cookie中 //5.装载购物车 @Resource private SkuService SkuService; @RequestMapping(value="/shopping/buyCart.shtml") //将商品加入购物车 public String buyCart(Integer skuId,Integer amount,Integer buyLimit,Integer productId,HttpServletRequest request,HttpServletResponse response,ModelMap model){ ObjectMapper om = new ObjectMapper(); om.setSerializationInclusion(Inclusion.NON_NULL);//去掉空值 BuyCart buyCart = null; //获取cookie中的购物车 Cookie []cookies = request.getCookies(); if(cookies!=null&&cookies.length>0){ for (Cookie cookie : cookies) { if(Constants.BUYCART_COOKIE.equals(cookie.getName())){//判断是否为购物车 String value = cookie.getValue(); //把json转化成对象 try { buyCart = om.readValue(value,BuyCart.class); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } } if(null==buyCart){ buyCart = new BuyCart(); } if(null!=skuId){ Sku sku = new Sku(); sku.setId(skuId); if(buyLimit!=null){ sku.setSkuUpperLimit(buyLimit); } BuyItem buyItem = new BuyItem(); buyItem.setSku(sku); buyItem.setAmount(amount); buyCart.addItem(buyItem); if(null!=productId){ buyCart.setProductId(productId); } StringWriter stringWriter = new StringWriter(); try { om.writeValue(stringWriter, buyCart); //将对象转化为json } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, stringWriter.toString());//购物车加入到cookie中 //所有只能存一个购物车 cookie.setPath("/"); cookie.setMaxAge(60*60*24); response.addCookie(cookie); } //装载购物车 装满 List<BuyItem> items = buyCart.getItems(); for (BuyItem buyItem : items) { Sku sku = SkuService.getSkuByKey(buyItem.getSku().getId()); buyItem.setSku(sku); //获取其他属性 } model.addAttribute("buyCart",buyCart); return "/product/cart"; } @RequestMapping(value="/shopping/clearCart.shtml") public String clearCart(HttpServletResponse response){ Cookie cookie = new Cookie(Constants.BUYCART_COOKIE,null); cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); return "redirect:/shopping/buyCart.shtml"; } /** * 删除一个商品 */ @RequestMapping(value="/shopping/deleteItem.shtml") public String deleteItem(HttpServletRequest request,HttpServletResponse response,Integer skuId){ ObjectMapper om = new ObjectMapper(); om.setSerializationInclusion(Inclusion.NON_NULL);//去掉空值 BuyCart buyCart = null; //获取cookie中的购物车 Cookie []cookies = request.getCookies(); if(cookies!=null&&cookies.length>0){ for (Cookie cookie : cookies) { if(Constants.BUYCART_COOKIE.equals(cookie.getName())){ String value = cookie.getValue(); //把json转化成对象 try { buyCart = om.readValue(value,BuyCart.class); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } } if(null!=buyCart){ Sku sku = new Sku(); sku.setId(skuId); BuyItem buyItem = new BuyItem(); buyItem.setSku(sku); buyCart.deleteItem(buyItem); StringWriter stringWriter = new StringWriter(); try { om.writeValue(stringWriter, buyCart); //将对象转化为json } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, stringWriter.toString());//购物车加入到cookie中 //所有只能存一个购物车 cookie.setPath("/"); cookie.setMaxAge(60*60*24); response.addCookie(cookie); } return "redirect:/shopping/buyCart.shtml"; } @RequestMapping(value="/buyer/trueBuy.shtml") public String trueBuy(HttpServletRequest request,HttpServletResponse response,ModelMap model){ //获取cookie中的购物车 ObjectMapper om = new ObjectMapper(); om.setSerializationInclusion(Inclusion.NON_NULL);//去掉空值 BuyCart buyCart = null; //获取cookie中的购物车 Cookie []cookies = request.getCookies(); if(cookies!=null&&cookies.length>0){ for (Cookie cookie : cookies) { if(Constants.BUYCART_COOKIE.equals(cookie.getName())){ String value = cookie.getValue(); //把json转化成对象 try { buyCart = om.readValue(value,BuyCart.class); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } } //判断购物车是否为空 if(buyCart != null){ //判断库存 List<BuyItem> items = buyCart.getItems(); if(items.size()>0&&items!=null){ Integer i = items.size(); //判断库存 for(BuyItem item:items){ Sku sku = skuService.getSkuByKey(item.getSku().getId()); //判断 if (sku.getStockInventory()<item.getAmount()) { //删除该商品 buyCart.deleteItem(item); } Integer l = items.size(); if(i>l){ //判断是否删除过商品 //删除过 购物车写回cookie StringWriter stringWriter = new StringWriter(); try { om.writeValue(stringWriter, buyCart); //将对象转化为json } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, stringWriter.toString());//购物车加入到cookie中 //所有只能存一个购物车 cookie.setPath("/"); cookie.setMaxAge(60*60*24); response.addCookie(cookie); return "redirect:/shopping/buyCart.shtml"; }else{ //条件都满足 //收货地址加载 Buyer buyer = (Buyer) sessionProvider.getAttribute(request,Constants.BUYER_SESSION); AddrQuery addrQuery = new AddrQuery(); addrQuery.setBuyerId(buyer.getUsername()); addrQuery.setIsDef(1); List<Addr>addrs= addrService.getAddrList(addrQuery); /* if(addrs!=null){ model.addAttribute("addr",addrs.get(0)); }*/ model.addAttribute("addr",addrs.get(0)); //装载购物车 List<BuyItem> it = buyCart.getItems(); for (BuyItem buyItem : it) { Sku sku1 = skuService.getSkuByKey(buyItem.getSku().getId()); buyItem.setSku(sku1); } model.addAttribute("buyCart",buyCart); return "product/productOrder"; } } }else{ return "redirect:/shopping/buyCart.shtml"; } }else{ return "redirect:/shopping/buyCart.shtml"; } return null; } @Resource private SkuService skuService; @Autowired private AddrService addrService; @Autowired private SessionProvider sessionProvider; }
以上是关于购物车模块的主要内容,如果未能解决你的问题,请参考以下文章
如何从 recyclerview 片段传递到另一个 recyclerview 片段
CTS测试CtsWindowManagerDeviceTestCases模块的testShowWhenLockedImeActivityAndShowSoftInput测试fail项解决方法(代码片段