View Javadoc
1   package org.example.customer;
2   
3   /*
4    * This is free and unencumbered software released into the public domain.
5    * Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, 
6    * either in source code form or as a compiled binary, for any purpose, commercial or 
7    * non-commercial, and by any means.
8    * 
9    * In jurisdictions that recognize copyright laws, the author or authors of this 
10   * software dedicate any and all copyright interest in the software to the public domain. 
11   * We make this dedication for the benefit of the public at large and to the detriment of 
12   * our heirs and successors. We intend this dedication to be an overt act of relinquishment in 
13   * perpetuity of all present and future rights to this software under copyright law.
14   * 
15   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
16   * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
17   * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES 
18   * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20   * 
21   * For more information, please refer to: https://unlicense.org/
22  */
23  
24  import java.io.Serializable;
25  
26  import org.apache.commons.lang3.builder.EqualsBuilder;
27  import org.apache.commons.lang3.builder.HashCodeBuilder;
28  import org.apache.logging.log4j.LogManager;
29  import org.apache.logging.log4j.Logger;
30  import org.example.customer.utility.CustomerEntity;
31  
32  /**
33   * The OrderItem Entity for the Customer application.
34   * <p>
35   * This class represents the following DB Table:
36   * 
37   * <pre>
38   * CREATE TABLE ORDER_ITEM (
39   *      ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
40   *      ORDER_ID INTEGER NOT NULL,
41   *      PRODUCT_ID INTEGER NOT NULL,
42   *      UNIT_PRICE DECIMAL(12,2) NOT NULL,
43   *      QUANTITY INTEGER NOT NULL,
44   *      CONSTRAINT PK_ORDERITEM PRIMARY KEY (ID)
45   *   );
46   * </pre>
47   * 
48   * @author Jonathan Earl
49   * @version 1.0
50   *
51   */
52  public final class OrderItem extends CustomerEntity
53      implements Serializable
54  {
55      private static final long serialVersionUID = 1L;
56  
57      private static final Logger LOG = LogManager.getLogger();
58  
59      private int myOrderId;
60      private int myProductId;
61      private double myUnitPrice;
62      private int myQuantity;
63      private double mySubTotal;
64  
65      /**
66       * The default constructor for the OrderItem class.
67       * <p>
68       * The initial values are:
69       * <ul>
70       * <li>id: 1</li>
71       * <li>orderId: 1</li>
72       * <li>productId: 1</li>
73       * <li>unitPrice: 0.01</li>
74       * <li>quantity: 1</li>
75       * <li>subTotal: unitPrice * quantity</li>
76       * </ul>
77       */
78      public OrderItem()
79      {
80          LOG.debug("Starting the default Constructor");
81          final int initialId = 1;
82          final int initialOrderId = 1;
83          final int initialProductId = 1;
84          final double initialUnitPrice = 0.01;
85          final int initialQuantity = 1;
86  
87          setId(initialId);
88          setOrderId(initialOrderId);
89          setProductId(initialProductId);
90          setUnitPrice(initialUnitPrice);
91          setQuantity(initialQuantity);
92          computeSubTotal();
93      }
94  
95      /**
96       * Computes the OrderItem subTotal as quantity * unitPrice.
97       */
98      private void computeSubTotal()
99      {
100         LOG.debug("Computing the subTotal");
101         mySubTotal = myQuantity * myUnitPrice;
102 
103     }
104 
105     /**
106      * Returns the orderId value for the Order.
107      *  
108      * @return the orderId value for the order
109      */
110     public int getOrderId()
111     {
112         LOG.debug("returning the OrderId: " + myOrderId);
113         return myOrderId;
114     }
115 
116     /**
117      * Sets the orderId value for the OrderItem.
118      * <p>
119      * The business rules are:
120      * <ul>
121      *   <li>the orderId must be 1 or greater</li>
122      * </ul>
123      * 
124      * @param orderId the value to set into the orderId field
125      * @throws IllegalArgumentException if the orderId is invalid
126      */
127     public void setOrderId(final int orderId)
128     {
129         LOG.debug("setting the OrderId: " + orderId);
130         final int min = 1;
131         
132         if (orderId < min)
133         {
134             LOG.error("OrderId must be greater then zero");
135             throw new IllegalArgumentException("OrderId must be greater then zero");
136         }
137         this.myOrderId = orderId;
138     }
139 
140     /**
141      * Returns the productId value for the OrderItem.
142      *  
143      * @return the productId value for the OrderItem
144      */
145     public int getProductId()
146     {
147         LOG.debug("returning the ProductId: " + myProductId);
148         return myProductId;
149     }
150 
151     /**
152      * Sets the productId value for the Order.
153      * <p>
154      * The business rules are:
155      * <ul>
156      *   <li>the productId must be 1 or greater</li>
157      * </ul>
158      * 
159      * @param productId the value to set into the productId field
160      * @throws IllegalArgumentException if the productId is invalid
161      */
162     public void setProductId(final int productId)
163     {
164         LOG.debug("setting the productId: " + productId);
165         final int min = 1;
166         
167         if (productId < min)
168         {
169             LOG.error("productId must be greater then zero");
170             throw new IllegalArgumentException("productId must be greater then zero");
171         }
172         this.myProductId = productId;
173     }
174 
175     /**
176      * Returns the UnitPrice value for the OrderItem.
177      *  
178      * @return the UnitPrice value for the OrderItem
179      */
180     public double getUnitPrice()
181     {
182         LOG.debug("returning the UnitPrice: " + myUnitPrice);
183         return myUnitPrice;
184     }
185 
186     /**
187      * Sets the UnitPrice value for the OrderItem.
188      * <p>
189      * The business rules are:
190      * <ul>
191      *   <li>the UnitPrice must be 0.01 or greater</li>
192      * </ul>
193      * This will also recompute the subTotal value.
194      * 
195      * @param unitPrice the value to set into the unitPrice field
196      * @throws IllegalArgumentException if the UnitPrice is invalid
197      */
198     public void setUnitPrice(final double unitPrice)
199     {
200         LOG.debug("setting the unitPrice: " + unitPrice);
201         final double min = 0.01;
202         
203         if (unitPrice < min)
204         {
205             LOG.error("unitPrice must be greater then zero");
206             throw new IllegalArgumentException("unitPrice must be greater then zero");
207         }
208         this.myUnitPrice = unitPrice;
209         computeSubTotal();
210     }
211 
212     /**
213      * Returns the Quantity value for the OrderItem.
214      *  
215      * @return the Quantity value for the OrderItem
216      */
217     public int getQuantity()
218     {
219         LOG.debug("returning the Quantity: " + myQuantity);
220         return myQuantity;
221     }
222 
223     /**
224      * Sets the Quantity value for the OrderItem.
225      * <p>
226      * The business rules are:
227      * <ul>
228      *   <li>the Quantity must be 1 or greater</li>
229      * </ul>
230      * This will also recompute the subTotal value.
231      * 
232      * @param quantity the value to set into the Quantity field
233      * @throws IllegalArgumentException if the Quantity is invalid
234      */
235     public void setQuantity(final int quantity)
236     {
237         LOG.debug("setting the Quantity: " + quantity);
238         final int min = 1;
239         
240         if (quantity < min)
241         {
242             LOG.error("Quantity must be greater then zero");
243             throw new IllegalArgumentException("Quantity must be greater then zero");
244         }
245         this.myQuantity = quantity;
246         computeSubTotal();
247     }
248 
249     /**
250      * Returns the SubTotal value for the OrderItem.
251      * 
252      * The SunTotal will be recomputed any time the UnitPrice or Quantity changes.
253      *  
254      * @return the SubTotal value for the OrderItem
255      */
256     public double getSubTotal()
257     {
258         LOG.debug("returning the SubTotal: " + mySubTotal);
259         return mySubTotal;
260     }
261     
262     /**
263      * The hashCode() method of the OrderItem class.
264      * <p>
265      * <strong>This method uses:</strong>
266      * <ul>
267      *  <li>id</li>
268      *  <li>order id</li>
269      *  <li>product id</li>
270      *  <li>unit price</li>
271      *  <li>quantity</li>
272      * </ul>
273      * 
274      * @see java.lang.Object#hashCode()
275      * @return the hashCode value for this Order object
276      */
277     @Override
278     public int hashCode()
279     {
280         LOG.debug("building HashCode");
281         return new HashCodeBuilder()
282                 .append(getId())
283                 .append(myOrderId)
284                 .append(myProductId)
285                 .append(myUnitPrice)
286                 .append(myQuantity)
287                 .toHashCode();
288     }
289 
290     /**
291      * The equals() method of the OrderItem class.
292      * <p>
293      * <strong>This method uses:</strong>
294      * <ul>
295      *  <li>id</li>
296      *  <li>order id</li>
297      *  <li>product id</li>
298      *  <li>unit price</li>
299      *  <li>quantity</li>
300      * </ul>
301      * 
302      * @see java.lang.Object#equals(Object obj)
303      * @param obj the incoming object to compare against
304      * @return true if the fields being compared are equal
305      */
306     @Override
307     public boolean equals(final Object obj)
308     {
309         LOG.debug("checking equals");
310         if (obj instanceof OrderItem)
311         {
312             final OrderItem other = (OrderItem) obj;
313             return new EqualsBuilder()
314                     .append(getId(), other.getId())
315                     .append(myOrderId, other.myOrderId)
316                     .append(myProductId, other.myProductId)
317                     .append(myUnitPrice, other.myUnitPrice)
318                     .append(myQuantity, other.myQuantity)
319                     .isEquals();
320         }
321         else
322         {
323             return false;
324         }
325     }
326 
327 
328     /**
329      * The toString method for the OrderItem class.
330      * 
331      * this method will return:<br>
332      * OrderItem [Id=xxx", OrderId=xxx, ProductId=xxx,
333                UnitPrice=xxx, Quantity=xxx, SubTotal=xxx]
334      */
335     @Override
336     public String toString()
337     {
338         return "OrderItem [Id=" + getId() + ", OrderId=" + myOrderId 
339                 + ", ProductId=" + myProductId
340                 + ", UnitPrice=" + myUnitPrice + ", Quantity=" 
341                 + myQuantity + ", SubTotal=" + mySubTotal + "]";
342     }
343 
344 }