View Javadoc
1   /*
2    *    Copyright 2010-2026 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.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  /**
27   * The Class Cart.
28   *
29   * @author Eduardo Macarron
30   */
31  public class Cart implements Serializable {
32  
33    /** The serial version uid. */
34    private static final long serialVersionUID = 8329559983943337176L;
35  
36    /** The item map. */
37    private final Map<String, CartItem> itemMap = new ConcurrentHashMap<>();
38  
39    /** The item list. */
40    private final List<CartItem> itemList = new ArrayList<>();
41  
42    /**
43     * Gets the cart items.
44     *
45     * @return the cart items
46     */
47    public Iterator<CartItem> getCartItems() {
48      return itemList.iterator();
49    }
50  
51    /**
52     * Gets the cart item list.
53     *
54     * @return the cart item list
55     */
56    public List<CartItem> getCartItemList() {
57      return itemList;
58    }
59  
60    /**
61     * Gets the number of items.
62     *
63     * @return the number of items
64     */
65    public int getNumberOfItems() {
66      return itemList.size();
67    }
68  
69    /**
70     * Gets the all cart items.
71     *
72     * @return the all cart items
73     */
74    public Iterator<CartItem> getAllCartItems() {
75      return itemList.iterator();
76    }
77  
78    /**
79     * Contains item id.
80     *
81     * @param itemId
82     *          the item id
83     *
84     * @return true, if successful
85     */
86    public boolean containsItemId(String itemId) {
87      return itemMap.containsKey(itemId);
88    }
89  
90    /**
91     * Adds the item.
92     *
93     * @param item
94     *          the item
95     * @param isInStock
96     *          the is in stock
97     */
98    public void addItem(Item item, boolean isInStock) {
99      CartItem cartItem = itemMap.get(item.getItemId());
100     if (cartItem == null) {
101       cartItem = new CartItem();
102       cartItem.setItem(item);
103       cartItem.setQuantity(0);
104       cartItem.setInStock(isInStock);
105       itemMap.put(item.getItemId(), cartItem);
106       itemList.add(cartItem);
107     }
108     cartItem.incrementQuantity();
109   }
110 
111   /**
112    * Removes the item by id.
113    *
114    * @param itemId
115    *          the item id
116    *
117    * @return the item
118    */
119   public Item removeItemById(String itemId) {
120     CartItem cartItem = itemMap.remove(itemId);
121     if (cartItem == null) {
122       return null;
123     } else {
124       itemList.remove(cartItem);
125       return cartItem.getItem();
126     }
127   }
128 
129   /**
130    * Increment quantity by item id.
131    *
132    * @param itemId
133    *          the item id
134    */
135   public void incrementQuantityByItemId(String itemId) {
136     CartItem cartItem = itemMap.get(itemId);
137     cartItem.incrementQuantity();
138   }
139 
140   /**
141    * Set quantity by item id.
142    *
143    * @param itemId
144    *          the item id
145    * @param quantity
146    *          the quantity
147    */
148   public void setQuantityByItemId(String itemId, int quantity) {
149     CartItem cartItem = itemMap.get(itemId);
150     cartItem.setQuantity(quantity);
151   }
152 
153   /**
154    * Gets the sub total.
155    *
156    * @return the sub total
157    */
158   public BigDecimal getSubTotal() {
159     return itemList.stream()
160         .map(cartItem -> cartItem.getItem().getListPrice().multiply(new BigDecimal(cartItem.getQuantity())))
161         .reduce(BigDecimal.ZERO, BigDecimal::add);
162   }
163 
164 }