在线交易系统的购物车的实现

在购物车中,我们可以删除购物项,修改产品的购买数量,清空购物车,进入结算中心。

以下是购物车的代码

View Code
  1 /**
2 * 购物车
3 */
4 public class BuyCart {
5 /* 购物项 */
6 private List<BuyItem> items = new ArrayList<BuyItem>();
7 /* 配送信息 */
8 private OrderDeliverInfo deliverInfo;
9 /* 购买者联系信息 */
10 private OrderContactInfo contactInfo;
11 /* 支付方式 */
12 private PaymentWay paymentWay;
13 /* 购买者与收货人是否相同 */
14 private Boolean buyerIsrecipients;
15 /* 配送费 */
16 private float deliveFee = 10f;
17 /* 附言 */
18 private String note;
19
20 public String getNote() {
21 return note;
22 }
23
24 public void setNote(String note) {
25 this.note = note;
26 }
27
28 public float getDeliveFee() {
29 return deliveFee;
30 }
31
32 public void setDeliveFee(float deliveFee) {
33 this.deliveFee = deliveFee;
34 }
35
36 public PaymentWay getPaymentWay() {
37 return paymentWay;
38 }
39
40 public void setPaymentWay(PaymentWay paymentWay) {
41 this.paymentWay = paymentWay;
42 }
43
44 public Boolean getBuyerIsrecipients() {
45 return buyerIsrecipients;
46 }
47
48 public void setBuyerIsrecipients(Boolean buyerIsrecipients) {
49 this.buyerIsrecipients = buyerIsrecipients;
50 }
51
52 public OrderContactInfo getContactInfo() {
53 return contactInfo;
54 }
55
56 public void setContactInfo(OrderContactInfo contactInfo) {
57 this.contactInfo = contactInfo;
58 }
59
60 public OrderDeliverInfo getDeliverInfo() {
61 return deliverInfo;
62 }
63
64 public void setDeliverInfo(OrderDeliverInfo deliverInfo) {
65 this.deliverInfo = deliverInfo;
66 }
67
68 public List<BuyItem> getItems() {
69 return items;
70 }
71
72 /**
73 * 添加购物项
74 * @param item 购物项
75 */
76 public void add(BuyItem item){
77 if(this.items.contains(item)){
78 for(BuyItem it : this.items){
79 if(it.equals(item)){
80 it.setAmount(it.getAmount()+1);
81 break;
82 }
83 }
84 }else{
85 this.items.add(item);
86 }
87 }
88 /**
89 * 删除指定购物项
90 * @param item 购物项
91 */
92 public void delete(BuyItem item){
93 if(this.items.contains(item)) this.items.remove(item);
94 }
95 /**
96 * 清空购物项
97 */
98 public void deleteAll(){
99 this.items.clear();
100 }
101
102 /**
103 * 计算商品总销售价
104 * @return
105 */
106 public float getTotalSellPrice(){
107 float totalprice = 0F;
108 for(BuyItem item : this.items){
109 totalprice += item.getProduct().getSellprice() * item.getAmount();
110 }
111 return totalprice;
112 }
113 /**
114 * 计算商品总市场价
115 * @return
116 */
117 public float getTotalMarketPrice(){
118 float totalprice = 0F;
119 for(BuyItem item : this.items){
120 totalprice += item.getProduct().getMarketprice() * item.getAmount();
121 }
122 return totalprice;
123 }
124 /**
125 * 计算总节省金额
126 * @return
127 */
128 public float getTotalSavedPrice(){
129 return this.getTotalMarketPrice() - this.getTotalSellPrice();
130 }
131 /**
132 * 计算订单的总费用
133 * @return
134 */
135 public float getOrderTotalPrice(){
136 return this.getTotalSellPrice()+ this.deliveFee;
137 }
138 }

 以下是购物项的代码:

View Code
 1 /**
2 * 购物项
3 */
4 public class BuyItem {
5 private ProductInfo product;
6 private Integer amount = 1;
7
8 public BuyItem(){}
9
10 public BuyItem(ProductInfo product) {
11 this.product = product;
12 }
13 public ProductInfo getProduct() {
14 return product;
15 }
16 public void setProduct(ProductInfo product) {
17 this.product = product;
18 }
19 public Integer getAmount() {
20 return amount;
21 }
22 public void setAmount(Integer amount) {
23 this.amount = amount;
24 }
25
26 @Override
27 public int hashCode() {
28 String result = product.getId().toString();
29 if(!product.getStyles().isEmpty()) result +="-"+ product.getStyles().iterator().next().getId();
30 return result.hashCode();
31 }
32 //购物车里的产品,最多只可能存在一个样式
33 @Override
34 public boolean equals(Object obj) {
35 if (this == obj)
36 return true;
37 if (obj == null)
38 return false;
39 if (getClass() != obj.getClass())
40 return false;
41 final BuyItem other = (BuyItem) obj;
42 if (product == null) {
43 if (other.product != null)
44 return false;
45 } else if (!product.equals(other.product))
46 return false;
47
48 if(product.getStyles().size()!=other.product.getStyles().size()){
49 return false;
50 }
51 Integer style = product.getStyles().iterator().next().getId();
52 Integer othersytle = other.product.getStyles().iterator().next().getId();
53 if(!style.equals(othersytle)) return false;
54 return true;
55 }
56
57 }
购物车的Controller:
View Code
 1 @Controller("/shopping/cart/manage")
2 public class BuyCartManageAction extends DispatchAction {
3
4 /**
5 * 删除指定购物项
6 */
7 public ActionForward delete(ActionMapping mapping, ActionForm form,
8 HttpServletRequest request, HttpServletResponse response)
9 throws Exception {
10 BuyCart cart = (BuyCart)WebUtil.getBuyCart(request);
11 BuyCartForm formbean = (BuyCartForm) form;
12 if(formbean.getProductid()!=null && formbean.getProductid()>0
13 && formbean.getStyleid()!=null && formbean.getStyleid()>0){
14 ProductInfo product = new ProductInfo(formbean.getProductid());
15 product.addProductStyle(new ProductStyle(formbean.getStyleid()));
16 BuyItem item = new BuyItem(product);
17 cart.delete(item);
18 }
19 request.setAttribute("directUrl", "/shopping/cart.do");
20 return mapping.findForward("directUrl");
21 }
22 /**
23 * 清空购物车
24 */
25 public ActionForward deleteAll(ActionMapping mapping, ActionForm form,
26 HttpServletRequest request, HttpServletResponse response)
27 throws Exception {
28 BuyCart cart = (BuyCart)WebUtil.getBuyCart(request);
29 cart.deleteAll();
30 request.setAttribute("directUrl", "/shopping/cart.do");
31 return mapping.findForward("directUrl");
32 }
33 /**
34 * 修改购买数量
35 */
36 public ActionForward updateAmount(ActionMapping mapping, ActionForm form,
37 HttpServletRequest request, HttpServletResponse response)
38 throws Exception {
39 modifyBuyAmount(request);
40 request.setAttribute("directUrl", "/shopping/cart.do");
41 return mapping.findForward("directUrl");
42 }
43
44 private void modifyBuyAmount(HttpServletRequest request) {
45 BuyCart cart = (BuyCart)WebUtil.getBuyCart(request);
46 for(BuyItem item : cart.getItems()){
47 StringBuilder sb = new StringBuilder("amount_");
48 sb.append(item.getProduct().getId()).append("_");
49 sb.append(item.getProduct().getStyles().iterator().next().getId());
50 String amount = request.getParameter(sb.toString());
51 if(amount!=null && !"".equals(amount.trim())){
52 item.setAmount(new Integer(amount.trim()));
53 }
54 }
55 }
56 /**
57 * 结算
58 */
59 public ActionForward settleAccounts(ActionMapping mapping, ActionForm form,
60 HttpServletRequest request, HttpServletResponse response)
61 throws Exception {
62 modifyBuyAmount(request);
63 BuyCartForm formbean = (BuyCartForm) form;
64 String url = "/customer/shopping/deliver.do";
65 if(formbean.getDirectUrl()!=null && !"".equals(formbean.getDirectUrl())) url = formbean.getDirectUrl();
66 request.setAttribute("directUrl", url);
67 return mapping.findForward("directUrl");
68 }
69
70
71 }

清空购物车的功能就是调用了BuyCartManageAction的deleteAll()方法。删除指定购物项只需调用BuyCartManageAction的delete()方法就行了。



posted @ 2012-04-07 22:09  jerry_xing8  阅读(3604)  评论(0编辑  收藏  举报