View Javadoc
1   /*
2    *    Copyright 2009-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.apache.ibatis.domain.jpetstore;
17  
18  import java.io.Serializable;
19  import java.math.BigDecimal;
20  
21  public class CartItem implements Serializable {
22  
23    private static final long serialVersionUID = 1L;
24  
25    private Item item;
26    private int quantity;
27    private boolean inStock;
28    private BigDecimal total;
29  
30    public boolean isInStock() {
31      return inStock;
32    }
33  
34    public void setInStock(boolean inStock) {
35      this.inStock = inStock;
36    }
37  
38    public BigDecimal getTotal() {
39      return total;
40    }
41  
42    public Item getItem() {
43      return item;
44    }
45  
46    public void setItem(Item item) {
47      this.item = item;
48      calculateTotal();
49    }
50  
51    public int getQuantity() {
52      return quantity;
53    }
54  
55    public void setQuantity(int quantity) {
56      this.quantity = quantity;
57      calculateTotal();
58    }
59  
60    public void incrementQuantity() {
61      quantity++;
62      calculateTotal();
63    }
64  
65    private void calculateTotal() {
66      if (item != null && item.getListPrice() != null) {
67        total = item.getListPrice().multiply(new BigDecimal(quantity));
68      } else {
69        total = null;
70      }
71    }
72  
73  }