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