View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.jpetstore.domain;
17  
18  import java.io.Serializable;
19  import java.math.BigDecimal;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * The Class Cart.
29   *
30   * @author Eduardo Macarron
31   */
32  public class Cart implements Serializable {
33  
34    private static final long serialVersionUID = 8329559983943337176L;
35  
36    private final Map<String, CartItem> itemMap = Collections.synchronizedMap(new HashMap<>());
37    private final List<CartItem> itemList = new ArrayList<>();
38  
39    public Iterator<CartItem> getCartItems() {
40      return itemList.iterator();
41    }
42  
43    public List<CartItem> getCartItemList() {
44      return itemList;
45    }
46  
47    public int getNumberOfItems() {
48      return itemList.size();
49    }
50  
51    public Iterator<CartItem> getAllCartItems() {
52      return itemList.iterator();
53    }
54  
55    public boolean containsItemId(String itemId) {
56      return itemMap.containsKey(itemId);
57    }
58  
59    /**
60     * Adds the item.
61     *
62     * @param item
63     *          the item
64     * @param isInStock
65     *          the is in stock
66     */
67    public void addItem(Item item, boolean isInStock) {
68      CartItem cartItem = itemMap.get(item.getItemId());
69      if (cartItem == null) {
70        cartItem = new CartItem();
71        cartItem.setItem(item);
72        cartItem.setQuantity(0);
73        cartItem.setInStock(isInStock);
74        itemMap.put(item.getItemId(), cartItem);
75        itemList.add(cartItem);
76      }
77      cartItem.incrementQuantity();
78    }
79  
80    /**
81     * Removes the item by id.
82     *
83     * @param itemId
84     *          the item id
85     *
86     * @return the item
87     */
88    public Item removeItemById(String itemId) {
89      CartItem cartItem = itemMap.remove(itemId);
90      if (cartItem == null) {
91        return null;
92      } else {
93        itemList.remove(cartItem);
94        return cartItem.getItem();
95      }
96    }
97  
98    /**
99     * Increment quantity by item id.
100    *
101    * @param itemId
102    *          the item id
103    */
104   public void incrementQuantityByItemId(String itemId) {
105     CartItem cartItem = itemMap.get(itemId);
106     cartItem.incrementQuantity();
107   }
108 
109   public void setQuantityByItemId(String itemId, int quantity) {
110     CartItem cartItem = itemMap.get(itemId);
111     cartItem.setQuantity(quantity);
112   }
113 
114   /**
115    * Gets the sub total.
116    *
117    * @return the sub total
118    */
119   public BigDecimal getSubTotal() {
120     return itemList.stream()
121         .map(cartItem -> cartItem.getItem().getListPrice().multiply(new BigDecimal(cartItem.getQuantity())))
122         .reduce(BigDecimal.ZERO, BigDecimal::add);
123   }
124 
125 }