1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
24
25
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 }