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