Skip to content

Commit cb28537

Browse files
Own exceptions
1 parent f37d77f commit cb28537

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

src/main/java/coreapi/OrderService.java

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,36 @@
33
import java.math.BigDecimal;
44
import java.util.Date;
55

6+
/*_________________________________________EXCEPTION_CLASSES______________________________ */
67

8+
//Class that throws an exception when there is not enough stock of the product.
9+
class InsufficientStock extends Exception
10+
{
11+
public InsufficientStock(String msg)
12+
{
13+
super(msg);
14+
}
15+
}
16+
17+
//Class that throws exceptions related to the products in the basket.
18+
class ExistenceInTheBasket extends Exception
19+
{
20+
public ExistenceInTheBasket(String msg)
21+
{
22+
super(msg);
23+
}
24+
}
25+
26+
//Class that throws exceptions related to order status change.
27+
class StatusException extends Exception
28+
{
29+
public StatusException(String msg)
30+
{
31+
super(msg);
32+
}
33+
}
34+
35+
/*_________________________________________CLASS_ORDERSERVICE______________________________ */
736
public class OrderService
837
{
938
//Create a new object of OrderService
@@ -16,12 +45,12 @@ public OrderService() {}
1645
* PRECONDITION:Receive an order and an id of a existing product, plus a positive quantity
1746
* POSTCONDITION: Add the product with the indicated quantity to the order
1847
*/
19-
public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int q)
48+
public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int q) throws InsufficientStock, ExistenceInTheBasket
2049
{
2150
Product prod = ProductCatalog.Instance().getProduct(productId);
2251
if(ord.containsProduct(productId))
2352
{
24-
throw new RuntimeException("This product is already in your basket, you can modify the quantity of it if you wish.");
53+
throw new ExistenceInTheBasket("This product is already in your basket, you can modify the quantity of it if you wish.");
2554
}
2655
else
2756
{
@@ -33,7 +62,7 @@ public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int
3362
}
3463
else
3564
{
36-
throw new RuntimeException("There is not enough stock of the product.");
65+
throw new InsufficientStock("There is not enough stock of the product.");
3766
}
3867
}
3968
}
@@ -44,7 +73,7 @@ public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int
4473
* PRECONDITION:Receive an order and an id of a existing product, plus a positive quantity
4574
* POSTCONDITION: Modify the quantity of the product indicated in the order
4675
*/
47-
public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId, int q)
76+
public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId, int q) throws InsufficientStock, ExistenceInTheBasket
4877
{
4978
Product prod = ProductCatalog.Instance().getProduct(productId);
5079

@@ -56,20 +85,20 @@ public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId,
5685
}
5786
else
5887
{
59-
throw new RuntimeException("There is not enough stock of the product.");
88+
throw new InsufficientStock("There is not enough stock of the product.");
6089
}
6190
}
6291
else
6392
{
64-
throw new RuntimeException("The product is not in your basket.");
93+
throw new ExistenceInTheBasket("The product is not in your basket.");
6594
}
6695

6796
}
6897
/*
6998
* PRECONDITION:Receive an order and an id of a existing product, plus a positive quantity
7099
* POSTCONDITION:Eliminate the indicated amount of the product
71100
*/
72-
public void removeProductFromOrder(OrderImpl ord, int productId, int q)
101+
public void removeProductFromOrder(OrderImpl ord, int productId, int q) throws ExistenceInTheBasket, InsufficientStock
73102
{
74103
int quantbasket = ord.checkProductQuantity(productId);
75104
if(ord.containsProduct(productId))
@@ -80,12 +109,12 @@ public void removeProductFromOrder(OrderImpl ord, int productId, int q)
80109
}
81110
else
82111
{
83-
throw new RuntimeException("Can't remove that amount of product.");
112+
throw new InsufficientStock("Can't remove that amount of product.");
84113
}
85114
}
86115
else
87116
{
88-
throw new RuntimeException("This object is not in your basket.");
117+
throw new ExistenceInTheBasket("This object is not in your basket.");
89118
}
90119

91120
}
@@ -95,7 +124,7 @@ public void removeProductFromOrder(OrderImpl ord, int productId, int q)
95124
* PRECONDITION:Receive an order
96125
* POSTCONDITION: Assign the status to the order
97126
*/
98-
public void OrderStatus_InKitchen(OrderImpl ord)
127+
public void OrderStatus_InKitchen(OrderImpl ord) throws StatusException
99128
{
100129
//There must be products in the basket and the order be in the open state
101130
if(!ord.getProducts().isEmpty() && ord.getStatus() == OrderStatus.OPEN)
@@ -104,15 +133,15 @@ public void OrderStatus_InKitchen(OrderImpl ord)
104133
}
105134
else
106135
{
107-
throw new RuntimeException("The order has no products to send to the kitchen.");
136+
throw new StatusException("The order has no products to send to the kitchen.");
108137
}
109138
}
110139

111140
/*
112141
* PRECONDITION:Receive an order
113142
* POSTCONDITION: Assign the status to the order
114143
*/
115-
public void OrderStatus_Delivered(OrderImpl ord)
144+
public void OrderStatus_Delivered(OrderImpl ord) throws StatusException
116145
{
117146
//The state must be in the kitchen to be delivered
118147
if(ord.getStatus() == OrderStatus.IN_KITCHEN)
@@ -121,14 +150,14 @@ public void OrderStatus_Delivered(OrderImpl ord)
121150
}
122151
else
123152
{
124-
throw new RuntimeException("The order cannot be entered if it has not been in the kitchen.");
153+
throw new StatusException("The order cannot be entered if it has not been in the kitchen.");
125154
}
126155
}
127156
/*
128157
* PRECONDITION:Receive an order
129158
* POSTCONDITION: Assign the status to the order
130159
*/
131-
public void OrderStatus_Payed(OrderImpl ord)
160+
public void OrderStatus_Payed(OrderImpl ord) throws StatusException
132161
{
133162
//The state must be delivered or in the kitchen in order to be paid
134163
if(ord.getStatus() == OrderStatus.IN_KITCHEN || ord.getStatus() == OrderStatus.DELIVERED)
@@ -137,7 +166,7 @@ public void OrderStatus_Payed(OrderImpl ord)
137166
}
138167
else
139168
{
140-
throw new RuntimeException("The order cannot be charged because it is not yet in the kitchen or delivered.");
169+
throw new StatusException("The order cannot be charged because it is not yet in the kitchen or delivered.");
141170
}
142171

143172
}
@@ -146,7 +175,7 @@ public void OrderStatus_Payed(OrderImpl ord)
146175
* PRECONDITION:Receive an order
147176
* POSTCONDITION: Assign the status to the order
148177
*/
149-
public void OrderStatus_Finished(OrderImpl ord)
178+
public void OrderStatus_Finished(OrderImpl ord) throws StatusException
150179
{
151180
// The state must be charged in order to be finalized
152181
if(ord.getStatus() == OrderStatus.PAYED)
@@ -155,7 +184,7 @@ public void OrderStatus_Finished(OrderImpl ord)
155184
}
156185
else
157186
{
158-
throw new RuntimeException("The order cannot be finalized because it has not been charged.");
187+
throw new StatusException("The order cannot be finalized because it has not been charged.");
159188
}
160189

161190
}

0 commit comments

Comments
 (0)