View Javadoc
1   /*
2    *    Copyright 2009-2023 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.apache.ibatis.domain.jpetstore;
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  public class Cart implements Serializable {
28  
29    private static final long serialVersionUID = 1L;
30  
31    private final Map<String, CartItem> itemMap = Collections.synchronizedMap(new HashMap<>());
32    private final List<CartItem> itemList = new ArrayList<>();
33  
34    public Iterator<CartItem> getCartItems() {
35      return itemList.iterator();
36    }
37  
38    public List<CartItem> getCartItemList() {
39      return itemList;
40    }
41  
42    public int getNumberOfItems() {
43      return itemList.size();
44    }
45  
46    public boolean containsItemId(String itemId) {
47      return itemMap.containsKey(itemId);
48    }
49  
50    public void addItem(Item item, boolean isInStock) {
51      CartItem cartItem = itemMap.get(item.getItemId());
52      if (cartItem == null) {
53        cartItem = new CartItem();
54        cartItem.setItem(item);
55        cartItem.setQuantity(0);
56        cartItem.setInStock(isInStock);
57        itemMap.put(item.getItemId(), cartItem);
58        itemList.add(cartItem);
59      }
60      cartItem.incrementQuantity();
61    }
62  
63    public Item removeItemById(String itemId) {
64      CartItem cartItem = itemMap.remove(itemId);
65      if (cartItem == null) {
66        return null;
67      }
68      itemList.remove(cartItem);
69      return cartItem.getItem();
70    }
71  
72    public void incrementQuantityByItemId(String itemId) {
73      CartItem cartItem = itemMap.get(itemId);
74      cartItem.incrementQuantity();
75    }
76  
77    public void setQuantityByItemId(String itemId, int quantity) {
78      CartItem cartItem = itemMap.get(itemId);
79      cartItem.setQuantity(quantity);
80    }
81  
82    public BigDecimal getSubTotal() {
83      BigDecimal subTotal = new BigDecimal("0");
84      Iterator<CartItem> items = getCartItems();
85      while (items.hasNext()) {
86        CartItem cartItem = items.next();
87        Item item = cartItem.getItem();
88        BigDecimal listPrice = item.getListPrice();
89        BigDecimal quantity = new BigDecimal(String.valueOf(cartItem.getQuantity()));
90        subTotal = subTotal.add(listPrice.multiply(quantity));
91      }
92      return subTotal;
93    }
94  
95  }