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.Optional;
21  
22  /**
23   * The Class CartItem.
24   *
25   * @author Eduardo Macarron
26   */
27  public class CartItem implements Serializable {
28  
29    private static final long serialVersionUID = 6620528781626504362L;
30  
31    private Item item;
32    private int quantity;
33    private boolean inStock;
34    private BigDecimal total;
35  
36    public boolean isInStock() {
37      return inStock;
38    }
39  
40    public void setInStock(boolean inStock) {
41      this.inStock = inStock;
42    }
43  
44    public BigDecimal getTotal() {
45      return total;
46    }
47  
48    public Item getItem() {
49      return item;
50    }
51  
52    public void setItem(Item item) {
53      this.item = item;
54      calculateTotal();
55    }
56  
57    public int getQuantity() {
58      return quantity;
59    }
60  
61    public void setQuantity(int quantity) {
62      this.quantity = quantity;
63      calculateTotal();
64    }
65  
66    public void incrementQuantity() {
67      quantity++;
68      calculateTotal();
69    }
70  
71    private void calculateTotal() {
72      total = Optional.ofNullable(item).map(Item::getListPrice).map(v -> v.multiply(new BigDecimal(quantity)))
73          .orElse(null);
74    }
75  
76  }