接下来就是代码实例来实现 购物车的功能了:
⾸先我们看下购物车和购物项两个JavaBean的设计:
购物车: buyerCart.java
1public class BuyerCart implements Serializable{
2
3/**
4 * 购物车
5 */
6private static final long serialVersionUID = 1L;
7
8//商品结果集
段奕宏图片
9private List<BuyerItem> items = new ArrayList<BuyerItem>();
10
11//添加购物项到购物车
怎么向银行贷款12public void addItem(BuyerItem item){
13//判断是否包含同款
14if (ains(item)) {
15//追加数量
16for (BuyerItem buyerItem : items) {
17if (buyerItem.equals(item)) {
18 buyerItem.Amount() + Amount());
19 }
20 }
山西面食21 }else {
22 items.add(item);
23 }
24
25 }
26
27public List<BuyerItem> getItems() {
28return items;
29 }
30
31public void setItems(List<BuyerItem> items) {
32this.items = items;
33 }
34
35
36//⼩计
37//商品数量
38@JsonIgnore
39public Integer getProductAmount(){
韩剧鬼怪40 Integer result = 0;
41//计算
蚝油生菜怎么做
42for (BuyerItem buyerItem : items) {
43 result += Amount();
44 }
45return result;
46 }
47
48//商品⾦额
49@JsonIgnore
50public Float getProductPrice(){
51 Float result = 0f;
52//计算
53for (BuyerItem buyerItem : items) {
54 result += Amount()*Sku().getPrice();
55 }
56return result;
57 }
58
59//运费
60@JsonIgnore
61public Float getFee(){
62 Float result = 0f;
63//计算
64if (getProductPrice() < 79) {
65 result = 5f;
66 }
67
68return result;
69 }
70
71//总价
72@JsonIgnore
73public Float getTotalPrice(){
74return getProductPrice() + getFee();
520留言75 }
76
77 }
这⾥使⽤了@JsonIgonre注解是因为下⾯需要将BuyerCart 转换成Json格式, ⽽这⼏个字段只有get ⽅法, 所以不能转换, 需要使⽤忽略Json.
下⾯是购物项: buyerItem.java
1public class BuyerItem implements Serializable{
2
3private static final long serialVersionUID = 1L;
4
5//SKu对象
6private Sku sku;
7
8//是否有货
9private Boolean isHave = true;
10
11//购买的数量
12private Integer amount = 1;
13
14public Sku getSku() {
15return sku;
16 }
17
18public void setSku(Sku sku) {
19this.sku = sku;
20 }
21
22public Boolean getIsHave() {
23return isHave;
24 }
25
26public void setIsHave(Boolean isHave) {
27this.isHave = isHave;
28 }
29
30public Integer getAmount() {
31return amount;
32 }
33
34public void setAmount(Integer amount) {
35this.amount = amount;
36 }
37
38@Override
39public int hashCode() {
40final int prime = 31;
41int result = 1;
42 result = prime * result + ((sku == null) ? 0 : sku.hashCode());
43return result;
44 }
45
46@Override
47public boolean equals(Object obj) {
48if (this == obj) //⽐较地址
49return true;
50if (obj == null)
51return false;
52if (getClass() != Class())
53return false;
54 BuyerItem other = (BuyerItem) obj;
55if (sku == null) {
56if (other.sku != null)
57return false;
58 } else if (!Id().equals(Id()))
59return false;
60return true;
61 }
62 }
1、将商品加⼊购物车中
1//加⼊购物车
2function addCart(){
3// + skuId
4window.location.href="/shopping/buyerCart?skuId="+skuId+"&amount="+$("#buy-num").val();
5 }
这⾥传⼊的参数是skuId(库存表的主键, 库存表保存的商品id,颜⾊,尺码,库存等信息), 购买数量amount.接着我们来看Controller是如何来处理的:
1//加⼊购物车
2@RequestMapping(value="/shopping/buyerCart")
3public <T> String buyerCart(Long skuId, Integer amount, HttpServletRequest request,
4 HttpServletResponse response) throws JsonParseException, JsonMappingException, IOException{
5//将对象转换成json字符串/json字符串转成对象
6 ObjectMapper om = new ObjectMapper();
7 om.setSerializationInclusion(Include.NON_NULL);
8 BuyerCart buyerCart = null;
9//1,获取Cookie中的购物车
10 Cookie[] cookies = Cookies();
11if (null != cookies && cookies.length > 0) {
12for (Cookie cookie : cookies) {
13//
14if (Constants.BUYER_CART.Name())) {
15//购物车对象与json字符串互转
16 buyerCart = om.Value(), BuyerCart.class);
发布评论