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 LineItem implements Serializable {
28
29 private static final long serialVersionUID = 6804536240033522156L;
30
31 private int orderId;
32 private int lineNumber;
33 private int quantity;
34 private String itemId;
35 private BigDecimal unitPrice;
36 private Item item;
37 private BigDecimal total;
38
39 public LineItem() {
40 }
41
42
43
44
45
46
47
48
49
50 public LineItem(int lineNumber, CartItem cartItem) {
51 this.lineNumber = lineNumber;
52 this.quantity = cartItem.getQuantity();
53 this.itemId = cartItem.getItem().getItemId();
54 this.unitPrice = cartItem.getItem().getListPrice();
55 this.item = cartItem.getItem();
56 calculateTotal();
57 }
58
59 public int getOrderId() {
60 return orderId;
61 }
62
63 public void setOrderId(int orderId) {
64 this.orderId = orderId;
65 }
66
67 public int getLineNumber() {
68 return lineNumber;
69 }
70
71 public void setLineNumber(int lineNumber) {
72 this.lineNumber = lineNumber;
73 }
74
75 public String getItemId() {
76 return itemId;
77 }
78
79 public void setItemId(String itemId) {
80 this.itemId = itemId;
81 }
82
83 public BigDecimal getUnitPrice() {
84 return unitPrice;
85 }
86
87 public void setUnitPrice(BigDecimal unitprice) {
88 this.unitPrice = unitprice;
89 }
90
91 public BigDecimal getTotal() {
92 return total;
93 }
94
95 public Item getItem() {
96 return item;
97 }
98
99 public void setItem(Item item) {
100 this.item = item;
101 calculateTotal();
102 }
103
104 public int getQuantity() {
105 return quantity;
106 }
107
108 public void setQuantity(int quantity) {
109 this.quantity = quantity;
110 calculateTotal();
111 }
112
113 private void calculateTotal() {
114 total = Optional.ofNullable(item).map(Item::getListPrice).map(v -> v.multiply(new BigDecimal(quantity)))
115 .orElse(null);
116 }
117
118 }