在购物车中,我们可以删除购物项,修改产品的购买数量,清空购物车,进入结算中心。
以下是购物车的代码:
/** * 购物车 */ public class BuyCart { /* 购物项 */ private List<BuyItem> items = new ArrayList<BuyItem>(); /* 配送信息 */ private OrderDeliverInfo deliverInfo; /* 购买者联系信息 */ private OrderContactInfo contactInfo; /* 支付方式 */ private PaymentWay paymentWay; /* 购买者与收货人是否相同 */ private Boolean buyerIsrecipients; /* 配送费 */ private float deliveFee = 10f; /* 附言 */ private String note; public String getNote() { return note; } public void setNote(String note) { this.note = note; } public float getDeliveFee() { return deliveFee; } public void setDeliveFee(float deliveFee) { this.deliveFee = deliveFee; } public PaymentWay getPaymentWay() { return paymentWay; } public void setPaymentWay(PaymentWay paymentWay) { this.paymentWay = paymentWay; } public Boolean getBuyerIsrecipients() { return buyerIsrecipients; } public void setBuyerIsrecipients(Boolean buyerIsrecipients) { this.buyerIsrecipients = buyerIsrecipients; } public OrderContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(OrderContactInfo contactInfo) { this.contactInfo = contactInfo; } public OrderDeliverInfo getDeliverInfo() { return deliverInfo; } public void setDeliverInfo(OrderDeliverInfo deliverInfo) { this.deliverInfo = deliverInfo; } public List<BuyItem> getItems() { return items; } /** * 添加购物项 * @param item 购物项 */ public void add(BuyItem item){ if(this.items.contains(item)){ for(BuyItem it : this.items){ if(it.equals(item)){ it.setAmount(it.getAmount()+1); break; } } }else{ this.items.add(item); } } /** * 删除指定购物项 * @param item 购物项 */ public void delete(BuyItem item){ if(this.items.contains(item)) this.items.remove(item); } /** * 清空购物项 */ public void deleteAll(){ this.items.clear(); } /** * 计算商品总销售价 * @return */ public float getTotalSellPrice(){ float totalprice = 0F; for(BuyItem item : this.items){ totalprice += item.getProduct().getSellprice() * item.getAmount(); } return totalprice; } /** * 计算商品总市场价 * @return */ public float getTotalMarketPrice(){ float totalprice = 0F; for(BuyItem item : this.items){ totalprice += item.getProduct().getMarketprice() * item.getAmount(); } return totalprice; } /** * 计算总节省金额 * @return */ public float getTotalSavedPrice(){ return this.getTotalMarketPrice() - this.getTotalSellPrice(); } /** * 计算订单的总费用 * @return */ public float getOrderTotalPrice(){ return this.getTotalSellPrice()+ this.deliveFee; } }
以下是购物项的代码:
/** * 购物项 */ public class BuyItem { private ProductInfo product; private Integer amount = 1; public BuyItem(){} public BuyItem(ProductInfo product) { this.product = product; } public ProductInfo getProduct() { return product; } public void setProduct(ProductInfo product) { this.product = product; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } @Override public int hashCode() { String result = product.getId().toString(); if(!product.getStyles().isEmpty()) result +="-"+ product.getStyles().iterator().next().getId(); return result.hashCode(); } //购物车里的产品,最多只可能存在一个样式 @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final BuyItem other = (BuyItem) obj; if (product == null) { if (other.product != null) return false; } else if (!product.equals(other.product)) return false; if(product.getStyles().size()!=other.product.getStyles().size()){ return false; } Integer style = product.getStyles().iterator().next().getId(); Integer othersytle = other.product.getStyles().iterator().next().getId(); if(!style.equals(othersytle)) return false; return true; } }
购物车的Controller:
@Controller("/shopping/cart/manage") public class BuyCartManageAction extends DispatchAction { /** * 删除指定购物项 */ public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { BuyCart cart = (BuyCart)WebUtil.getBuyCart(request); BuyCartForm formbean = (BuyCartForm) form; if(formbean.getProductid()!=null && formbean.getProductid()>0 && formbean.getStyleid()!=null && formbean.getStyleid()>0){ ProductInfo product = new ProductInfo(formbean.getProductid()); product.addProductStyle(new ProductStyle(formbean.getStyleid())); BuyItem item = new BuyItem(product); cart.delete(item); } request.setAttribute("directUrl", "/shopping/cart.do"); return mapping.findForward("directUrl"); } /** * 清空购物车 */ public ActionForward deleteAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { BuyCart cart = (BuyCart)WebUtil.getBuyCart(request); cart.deleteAll(); request.setAttribute("directUrl", "/shopping/cart.do"); return mapping.findForward("directUrl"); } /** * 修改购买数量 */ public ActionForward updateAmount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { modifyBuyAmount(request); request.setAttribute("directUrl", "/shopping/cart.do"); return mapping.findForward("directUrl"); } private void modifyBuyAmount(HttpServletRequest request) { BuyCart cart = (BuyCart)WebUtil.getBuyCart(request); for(BuyItem item : cart.getItems()){ StringBuilder sb = new StringBuilder("amount_"); sb.append(item.getProduct().getId()).append("_"); sb.append(item.getProduct().getStyles().iterator().next().getId()); String amount = request.getParameter(sb.toString()); if(amount!=null && !"".equals(amount.trim())){ item.setAmount(new Integer(amount.trim())); } } } /** * 结算 */ public ActionForward settleAccounts(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { modifyBuyAmount(request); BuyCartForm formbean = (BuyCartForm) form; String url = "/customer/shopping/deliver.do"; if(formbean.getDirectUrl()!=null && !"".equals(formbean.getDirectUrl())) url = formbean.getDirectUrl(); request.setAttribute("directUrl", url); return mapping.findForward("directUrl"); } }
清空购物车的功能就是调用了BuyCartManageAction的deleteAll()方法。删除指定购物项只需调用BuyCartManageAction的delete()方法就行了。