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 LineItem.
24   *
25   * @author Eduardo Macarron
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     * Instantiates a new line item.
44     *
45     * @param lineNumber
46     *          the line number
47     * @param cartItem
48     *          the cart item
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 }