在线交易系统的购物车的实现
在购物车中,我们可以删除购物项,修改产品的购买数量,清空购物车,进入结算中心。
以下是购物车的代码
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 }
以下是购物项的代码: