View Javadoc
1   /*
2    *    Copyright 2010-2026 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  public class LineItem implements Serializable {
26  
27    /** The serial version uid. */
28    private static final long serialVersionUID = 6804536240033522156L;
29  
30    /** The order id. */
31    private int orderId;
32    /** The line number. */
33    private int lineNumber;
34    /** The quantity. */
35    private int quantity;
36    /** The item id. */
37    private String itemId;
38    /** The unit price. */
39    private BigDecimal unitPrice;
40    /** The item. */
41    private Item item;
42    /** The total. */
43    private BigDecimal total;
44  
45    /**
46     * Instantiates a new line item.
47     */
48    public LineItem() {
49    }
50  
51    /**
52     * Instantiates a new line item.
53     *
54     * @param lineNumber
55     *          the line number
56     * @param cartItem
57     *          the cart item
58     */
59    public LineItem(int lineNumber, CartItem cartItem) {
60      this.lineNumber = lineNumber;
61      this.quantity = cartItem.getQuantity();
62      this.itemId = cartItem.getItem().getItemId();
63      this.unitPrice = cartItem.getItem().getListPrice();
64      this.item = cartItem.getItem();
65      calculateTotal();
66    }
67  
68    /**
69     * Gets the order id.
70     *
71     * @return the order id
72     */
73    public int getOrderId() {
74      return orderId;
75    }
76  
77    /**
78     * Sets the order id.
79     *
80     * @param orderId
81     *          the order id
82     */
83    public void setOrderId(int orderId) {
84      this.orderId = orderId;
85    }
86  
87    /**
88     * Gets the line number.
89     *
90     * @return the line number
91     */
92    public int getLineNumber() {
93      return lineNumber;
94    }
95  
96    /**
97     * Sets the line number.
98     *
99     * @param lineNumber
100    *          the line number
101    */
102   public void setLineNumber(int lineNumber) {
103     this.lineNumber = lineNumber;
104   }
105 
106   /**
107    * Gets the item id.
108    *
109    * @return the item id
110    */
111   public String getItemId() {
112     return itemId;
113   }
114 
115   /**
116    * Sets the item id.
117    *
118    * @param itemId
119    *          the item id
120    */
121   public void setItemId(String itemId) {
122     this.itemId = itemId;
123   }
124 
125   /**
126    * Gets the unit price.
127    *
128    * @return the unit price
129    */
130   public BigDecimal getUnitPrice() {
131     return unitPrice;
132   }
133 
134   /**
135    * Sets the unit price.
136    *
137    * @param unitprice
138    *          the unitprice
139    */
140   public void setUnitPrice(BigDecimal unitprice) {
141     this.unitPrice = unitprice;
142   }
143 
144   /**
145    * Gets the total.
146    *
147    * @return the total
148    */
149   public BigDecimal getTotal() {
150     return total;
151   }
152 
153   /**
154    * Gets the item.
155    *
156    * @return the item
157    */
158   public Item getItem() {
159     return item;
160   }
161 
162   /**
163    * Sets the item.
164    *
165    * @param item
166    *          the item
167    */
168   public void setItem(Item item) {
169     this.item = item;
170     calculateTotal();
171   }
172 
173   /**
174    * Gets the quantity.
175    *
176    * @return the quantity
177    */
178   public int getQuantity() {
179     return quantity;
180   }
181 
182   /**
183    * Sets the quantity.
184    *
185    * @param quantity
186    *          the quantity
187    */
188   public void setQuantity(int quantity) {
189     this.quantity = quantity;
190     calculateTotal();
191   }
192 
193   /**
194    * Calculate total.
195    */
196   private void calculateTotal() {
197     total = Optional.ofNullable(item).map(Item::getListPrice).map(v -> v.multiply(new BigDecimal(quantity)))
198         .orElse(null);
199   }
200 
201 }