Skip to content

Commit 13a24d6

Browse files
Refactored the OrderService methods to hide the use of OrderImpl via internal downcasting.
1 parent 9042a0a commit 13a24d6

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

api/src/main/java/coreapi/OrderService.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ public OrderService() {}
2929
* @param quantity The quantity of product to add to the order.
3030
* @throws InsufficientStockException If there isn't enough stock of the product.
3131
*/
32-
public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int quantity)
32+
public void addProductToOrder(Cafeteria coffe, Order ord, int productId, int quantity)
3333
throws InsufficientStockException
3434
{
35+
OrderImpl orderDownCast = (OrderImpl) ord;
3536
Product prod = ProductCatalog.Instance().getProduct(productId);
37+
3638
if(coffe.getAvailableProducts().contains(prod))
3739
{
3840
if(quantity > 0 && coffe.getProductQuantity(prod) >= quantity)
3941
{
4042
coffe.removeStock(prod, quantity);
41-
ord.addProduct(productId, quantity);
43+
orderDownCast.addProduct(productId, quantity);
4244
}
4345
else
4446
{
@@ -55,15 +57,16 @@ public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int
5557
* @throws InsufficientStockException If the quantity to remove is bigger than the quantity which is stock in the order
5658
* @throws ProductNotContainedInOrderException If the product isn't in the basket
5759
*/
58-
public void removeProductFromOrder(OrderImpl ord, int productId, int quantity)
60+
public void removeProductFromOrder(Order ord, int productId, int quantity)
5961
throws InsufficientStockException, ProductNotContainedInOrderException
6062
{
61-
int quantbasket = ord.checkProductQuantity(productId);
62-
if(ord.containsProduct(productId))
63+
OrderImpl orderDownCast = (OrderImpl) ord;
64+
int quantbasket = orderDownCast.checkProductQuantity(productId);
65+
if(orderDownCast.containsProduct(productId))
6366
{
6467
if(quantity > 0 && quantity <= quantbasket)
6568
{
66-
ord.removeProduct(productId, quantity);
69+
orderDownCast.removeProduct(productId, quantity);
6770

6871
}
6972
else
@@ -85,13 +88,14 @@ public void removeProductFromOrder(OrderImpl ord, int productId, int quantity)
8588
* @param ord The order to change the status of.
8689
* @throws UnreachableStatusException If the conditions to set the <code>IN_KITCHEN</code> status aren't met.
8790
*/
88-
public void OrderStatus_InKitchen(OrderImpl ord)
91+
public void OrderStatus_InKitchen(Order ord)
8992
throws UnreachableStatusException
9093
{
94+
OrderImpl orderDownCast = (OrderImpl) ord;
9195
//There must be products in the basket and the order be in the open state
9296
if(!ord.getProducts().isEmpty() && ord.getStatus() == OrderStatus.OPEN)
9397
{
94-
ord.setStatus(OrderStatus.IN_KITCHEN);
98+
orderDownCast.setStatus(OrderStatus.IN_KITCHEN);
9599
}
96100
else
97101
{
@@ -105,13 +109,14 @@ public void OrderStatus_InKitchen(OrderImpl ord)
105109
* @param ord The order to change the status of.
106110
* @throws UnreachableStatusException If the conditions to set the <code>DELIVERED</code> status aren't met.
107111
*/
108-
public void OrderStatus_Delivered(OrderImpl ord)
112+
public void OrderStatus_Delivered(Order ord)
109113
throws UnreachableStatusException
110114
{
115+
OrderImpl orderDownCast = (OrderImpl) ord;
111116
//The state must be in the kitchen to be delivered
112-
if(ord.getStatus() == OrderStatus.IN_KITCHEN)
117+
if(orderDownCast.getStatus() == OrderStatus.IN_KITCHEN)
113118
{
114-
ord.setStatus(OrderStatus.DELIVERED);
119+
orderDownCast.setStatus(OrderStatus.DELIVERED);
115120
}
116121
else
117122
{
@@ -124,13 +129,14 @@ public void OrderStatus_Delivered(OrderImpl ord)
124129
* @param ord The order to change the status of.
125130
* @throws UnreachableStatusException If the conditions to set the <code>PAYED</code> status aren't met.
126131
*/
127-
public void OrderStatus_Payed(OrderImpl ord)
132+
public void OrderStatus_Payed(Order ord)
128133
throws UnreachableStatusException
129134
{
135+
OrderImpl orderDownCast = (OrderImpl) ord;
130136
//The state must be delivered or in the kitchen in order to be paid
131-
if(ord.getStatus() == OrderStatus.IN_KITCHEN || ord.getStatus() == OrderStatus.DELIVERED)
137+
if(orderDownCast.getStatus() == OrderStatus.IN_KITCHEN || ord.getStatus() == OrderStatus.DELIVERED)
132138
{
133-
ord.setStatus(OrderStatus.PAYED);
139+
orderDownCast.setStatus(OrderStatus.PAYED);
134140
}
135141
else
136142
{
@@ -144,13 +150,14 @@ public void OrderStatus_Payed(OrderImpl ord)
144150
* @param ord The order to change the status of.
145151
* @throws UnreachableStatusException If the conditions to set the <code>FINISHED</code> status aren't met.
146152
*/
147-
public void OrderStatus_Finished(OrderImpl ord)
153+
public void OrderStatus_Finished(Order ord)
148154
throws UnreachableStatusException
149155
{
156+
OrderImpl orderDownCast = (OrderImpl) ord;
150157
// The state must be charged in order to be finalized
151-
if(ord.getStatus() == OrderStatus.PAYED)
158+
if(orderDownCast.getStatus() == OrderStatus.PAYED)
152159
{
153-
ord.setStatus(OrderStatus.FINISHED);
160+
orderDownCast.setStatus(OrderStatus.FINISHED);
154161
}
155162
else
156163
{

0 commit comments

Comments
 (0)