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