1+ /**
2+ * Implementation of the <code>Order</code> interface.
3+ * Users of the API shouldn't use this class directly.
4+ * @author Borja
5+ * @version 0.2
6+ */
17package coreapi ;
28
9+ import java .math .BigDecimal ;
310import java .util .ArrayList ;
411import java .util .Date ;
512import java .util .List ;
@@ -15,7 +22,11 @@ public class OrderImpl implements Order {
1522 private LinkedHashMap <Product , Integer > basket ;
1623 private OrderStatus orderStatus ;
1724
18-
25+ /**
26+ * Creates a new <code>OrderImpl</code> instance.
27+ * @param assignedId the identifier assigned to this order.
28+ * @param creationDate the timestamp representing the moment this order was created.
29+ */
1930 OrderImpl (int assignedId , Date creationDate )
2031 {
2132 id = assignedId ;
@@ -24,51 +35,90 @@ public class OrderImpl implements Order {
2435 orderStatus = OrderStatus .OPEN ;
2536 }
2637
38+ /**
39+ * Returns the unique id assigned by the order factory to this specific order.
40+ * @return returns the id of this order.
41+ * @see OrderFactory
42+ */
2743 public int getId () { return id ; }
44+ /**
45+ * Returns the timestamp representing the moment this order was created.
46+ * @return the date on which the order was created.
47+ * @see Date
48+ */
2849 public Date getDate () { return date ; }
50+ /**
51+ * Returns a read-only value of the current state of this order.
52+ * @return returns the state of this order.
53+ * @see OrderStatus
54+ */
2955 public OrderStatus getStatus () { return OrderStatus .valueOf (orderStatus .name ()); }
56+ /**
57+ * Sets the current status of this order to the one passed by parameter.
58+ * @param newStatus the new status to set this order to.
59+ * @see OrderStatus
60+ */
3061 public void setStatus (OrderStatus newStatus ) { orderStatus = newStatus ; }
3162
3263 /**
33- * @return Returns the total cost of all the products contained in the Order.
64+ * Returns the total cost of all the products contained in the order.
65+ * Takes into account the individual price of each product, as well as how many
66+ * of them are currently in this order.
67+ * @return the combined cost of this order.
68+ * @see BigDecimal
3469 */
35- public float totalCost ()
70+ public BigDecimal totalCost ()
3671 {
37- float sumCost = 0 ;
72+ BigDecimal sumCost = BigDecimal .ZERO ;
73+ BigDecimal productCost = BigDecimal .ZERO ;
74+ BigDecimal productQuantity = BigDecimal .ZERO ;
75+
3876 for (Map .Entry <Product , Integer > entry : basket .entrySet ())
3977 {
40- sumCost += entry .getKey ().getPrice () * entry .getValue ().intValue ();
78+ // We create a copy of the product's price as to not modify its value via this reference.
79+ productCost = entry .getKey ().getPrice ();
80+ productQuantity = new BigDecimal (entry .getValue ().intValue ());
81+ productCost = productCost .multiply (productQuantity );
82+
83+ sumCost = sumCost .add (productCost );
4184 }
4285 return sumCost ;
4386 }
4487
4588 /**
46- * @return Returns a read-only map containing what products are in this Order.
89+ * Returns a read-only list containing what products are in this Order.
90+ * @return the list of products contained in this order.
4791 */
4892 public List <Product > getProducts ()
4993 {
5094 return Collections .unmodifiableList (new ArrayList <Product >(basket .keySet ()));
5195 }
5296
5397 /**
54- * @return Returns a read-only map containing what products and how many
55- * of them are in this Order.
98+ * Returns a read-only map containing what products and how many
99+ * of each of them are in this order.
100+ * @return the map of products and their quantities in this order.
56101 */
57102 public Map <Product ,Integer > getBasket ()
58103 {
59104 return Collections .unmodifiableMap (basket );
60105 }
61106
62107 /**
63- * @return Returns whether or not the Product determined by the id is contained in this Order.
108+ * Returns whether or not the product determined by the id is contained in this order.
109+ * Returns <code>true</code> if there is at least one unit of the product in this order, and
110+ * <code>false</code> if there is none.
111+ * @return if the product represented by the id exists in this order.
64112 */
65113 public boolean containsProduct (int id )
66114 {
67115 return basket .containsKey (ProductCatalog .Instance ().getProduct (id ));
68116 }
69117
70118 /**
71- * @return Returns how many existences of a Product are there in this Order.
119+ * Returns how many existences of the product determined by the id are there in this order.
120+ * Returns zero if the product in question isn't in this order.
121+ * @return the quantity of a product in this order.
72122 */
73123 public int checkProductQuantity (int id )
74124 {
@@ -84,12 +134,19 @@ public int checkProductQuantity(int id)
84134 }
85135 return quantity ;
86136 }
87-
137+ /**
138+ * Adds a single unit of the product determined by the id to this order.
139+ * @param newProductId the id of the product to add.
140+ */
88141 public void addProduct (int newProductId )
89142 {
90143 addProduct (newProductId , 1 );
91144 }
92-
145+ /**
146+ * Adds a certain amount of units of the product determined by the id to this order.
147+ * @param newProductId the id of the product to add.
148+ * @param quantity the amount of the product to add.
149+ */
93150 public void addProduct (int newProductId , int quantity )
94151 {
95152 if (quantity > 0 )
@@ -106,12 +163,22 @@ public void addProduct(int newProductId, int quantity)
106163 }
107164 }
108165 }
109-
166+ /**
167+ * Removes all amounts of the product determined by the id from this order.
168+ * The product in question will no longer be contained within this order.
169+ * @param productId the id of the product to remove.
170+ */
110171 public void removeProduct (int productId )
111172 {
112173 basket .remove (ProductCatalog .Instance ().getProduct (productId ));
113174 }
114-
175+ /**
176+ * Removes a certain amount of units of the product determined by the id from this order.
177+ * If the amount to remove is equal to or greater that the amount of that product currently
178+ * in the order, the product in question is removed from the order entirely.
179+ * @param productId the id of the product to remove.
180+ * @param quantity the amount of the product to remove.
181+ */
115182 public void removeProduct (int productId , int quantity )
116183 {
117184 if (quantity > 0 )
0 commit comments