Skip to content

Commit ac1dfb6

Browse files
Created the exception classes as separate files.
1 parent bf04018 commit ac1dfb6

File tree

5 files changed

+104
-47
lines changed

5 files changed

+104
-47
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
*/
4+
package coreapi;
5+
6+
/**
7+
* @author Maria
8+
* @author Borja
9+
* @version 0.1
10+
*
11+
*/
12+
public class InsufficientStockException extends Exception {
13+
14+
/**
15+
* @param message
16+
*/
17+
public InsufficientStockException(String message) {
18+
super(message);
19+
}
20+
21+
}

src/main/java/coreapi/OrderService.java

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* @author María
2+
* @author Maria
3+
* @author Borja
34
* @version 0.2
45
*/
56

@@ -8,36 +9,7 @@
89
import java.math.BigDecimal;
910
import java.util.Date;
1011

11-
/*_________________________________________EXCEPTION_CLASSES______________________________ */
1212

13-
//Class that throws an exception when there is not enough stock of the product.
14-
class InsufficientStock extends Exception
15-
{
16-
public InsufficientStock(String msg)
17-
{
18-
super(msg);
19-
}
20-
}
21-
22-
//Class that throws exceptions related to the products in the basket.
23-
class ExistenceInTheBasket extends Exception
24-
{
25-
public ExistenceInTheBasket(String msg)
26-
{
27-
super(msg);
28-
}
29-
}
30-
31-
//Class that throws exceptions related to order status change.
32-
class StatusException extends Exception
33-
{
34-
public StatusException(String msg)
35-
{
36-
super(msg);
37-
}
38-
}
39-
40-
/*_________________________________________CLASS_ORDERSERVICE______________________________ */
4113
public class OrderService
4214
{
4315
//Create a new object of OrderService
@@ -56,12 +28,12 @@ public OrderService() {}
5628
* @throws If the product is already in the basket
5729
* @throws If there isn't enough stock of the product
5830
*/
59-
public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int q)throws InsufficientStock, ExistenceInTheBasket
31+
public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int q)throws InsufficientStockException, ProductAlreadyInOrderException
6032
{
6133
Product prod = ProductCatalog.Instance().getProduct(productId);
6234
if(ord.containsProduct(productId))
6335
{
64-
throw new ExistenceInTheBasket("This product is already in your basket, you can modify the quantity of it if you wish.");
36+
throw new ProductAlreadyInOrderException("This product is already in your basket, you can modify the quantity of it if you wish.");
6537
}
6638
else
6739
{
@@ -73,7 +45,7 @@ public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int
7345
}
7446
else
7547
{
76-
throw new InsufficientStock("There is not enough stock of the product.");
48+
throw new InsufficientStockException("There is not enough stock of the product.");
7749
}
7850
}
7951
}
@@ -90,7 +62,7 @@ public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int
9062
* @throws If there isn't enough stock of the product
9163
* @throws If the product isn't in the basket
9264
*/
93-
public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId, int q)throws InsufficientStock, ExistenceInTheBasket
65+
public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId, int q)throws InsufficientStockException, ProductNotContainedInOrderException
9466
{
9567
Product prod = ProductCatalog.Instance().getProduct(productId);
9668

@@ -102,12 +74,12 @@ public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId,
10274
}
10375
else
10476
{
105-
throw new InsufficientStock("There is not enough stock of the product.");
77+
throw new InsufficientStockException("There is not enough stock of the product.");
10678
}
10779
}
10880
else
10981
{
110-
throw new ExistenceInTheBasket("The product is not in your basket.");
82+
throw new ProductNotContainedInOrderException("The product is not in your basket.");
11183
}
11284

11385
}
@@ -121,7 +93,7 @@ public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId,
12193
* @throws If the quantity to remove is bigger than the quantity which is stock in the order
12294
* @throws If the product isn't in the basket
12395
*/
124-
public void removeProductFromOrder(OrderImpl ord, int productId, int q)throws ExistenceInTheBasket, InsufficientStock
96+
public void removeProductFromOrder(OrderImpl ord, int productId, int q)throws InsufficientStockException, ProductNotContainedInOrderException
12597
{
12698
int quantbasket = ord.checkProductQuantity(productId);
12799
if(ord.containsProduct(productId))
@@ -132,12 +104,12 @@ public void removeProductFromOrder(OrderImpl ord, int productId, int q)throws Ex
132104
}
133105
else
134106
{
135-
throw new InsufficientStock("Can't remove that amount of product.");
107+
throw new InsufficientStockException("Can't remove that amount of product.");
136108
}
137109
}
138110
else
139111
{
140-
throw new ExistenceInTheBasket("This object is not in your basket.");
112+
throw new ProductNotContainedInOrderException("This product is not in your basket.");
141113
}
142114

143115
}
@@ -148,7 +120,7 @@ public void removeProductFromOrder(OrderImpl ord, int productId, int q)throws Ex
148120
* Assign the status to the order
149121
* @param ord the order which status change
150122
*/
151-
public void OrderStatus_InKitchen(OrderImpl ord)throws StatusException
123+
public void OrderStatus_InKitchen(OrderImpl ord)throws UnreachableStatusException
152124
{
153125
//There must be products in the basket and the order be in the open state
154126
if(!ord.getProducts().isEmpty() && ord.getStatus() == OrderStatus.OPEN)
@@ -157,7 +129,7 @@ public void OrderStatus_InKitchen(OrderImpl ord)throws StatusException
157129
}
158130
else
159131
{
160-
throw new StatusException("The order has no products to send to the kitchen.");
132+
throw new UnreachableStatusException("The order has no products to send to the kitchen.");
161133
}
162134
}
163135

@@ -166,7 +138,7 @@ public void OrderStatus_InKitchen(OrderImpl ord)throws StatusException
166138
* Assign the status to the order
167139
* @param ord the order which status change
168140
*/
169-
public void OrderStatus_Delivered(OrderImpl ord)throws StatusException
141+
public void OrderStatus_Delivered(OrderImpl ord)throws UnreachableStatusException
170142
{
171143
//The state must be in the kitchen to be delivered
172144
if(ord.getStatus() == OrderStatus.IN_KITCHEN)
@@ -175,15 +147,15 @@ public void OrderStatus_Delivered(OrderImpl ord)throws StatusException
175147
}
176148
else
177149
{
178-
throw new StatusException("The order cannot be entered if it has not been in the kitchen.");
150+
throw new UnreachableStatusException("The order cannot be entered if it has not been in the kitchen.");
179151
}
180152
}
181153
/**
182154
* Receive an order
183155
* Assign the status to the order
184156
* @param ord the order which status change
185157
*/
186-
public void OrderStatus_Payed(OrderImpl ord)throws StatusException
158+
public void OrderStatus_Payed(OrderImpl ord)throws UnreachableStatusException
187159
{
188160
//The state must be delivered or in the kitchen in order to be paid
189161
if(ord.getStatus() == OrderStatus.IN_KITCHEN || ord.getStatus() == OrderStatus.DELIVERED)
@@ -192,7 +164,7 @@ public void OrderStatus_Payed(OrderImpl ord)throws StatusException
192164
}
193165
else
194166
{
195-
throw new StatusException("The order cannot be charged because it is not yet in the kitchen or delivered.");
167+
throw new UnreachableStatusException("The order cannot be charged because it is not yet in the kitchen or delivered.");
196168
}
197169

198170
}
@@ -201,7 +173,7 @@ public void OrderStatus_Payed(OrderImpl ord)throws StatusException
201173
* Assign the status to the order
202174
* @param ord the order which status change
203175
*/
204-
public void OrderStatus_Finished(OrderImpl ord)throws StatusException
176+
public void OrderStatus_Finished(OrderImpl ord)throws UnreachableStatusException
205177
{
206178
// The state must be charged in order to be finalized
207179
if(ord.getStatus() == OrderStatus.PAYED)
@@ -210,7 +182,7 @@ public void OrderStatus_Finished(OrderImpl ord)throws StatusException
210182
}
211183
else
212184
{
213-
throw new StatusException("The order cannot be finalized because it has not been charged.");
185+
throw new UnreachableStatusException("The order cannot be finalized because it has not been charged.");
214186
}
215187

216188
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
*/
4+
package coreapi;
5+
6+
/**
7+
* @author Maria
8+
* @author Borja
9+
* @version 0.1
10+
*
11+
*/
12+
public class ProductAlreadyInOrderException extends Exception
13+
{
14+
15+
/**
16+
* @param message
17+
*/
18+
public ProductAlreadyInOrderException(String message) {
19+
super(message);
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*
3+
*/
4+
package coreapi;
5+
6+
/**
7+
* @author Maria
8+
* @author Borja
9+
* @version 0.1
10+
*
11+
*/
12+
public class ProductNotContainedInOrderException extends Exception
13+
{
14+
15+
/**
16+
* @param message
17+
*/
18+
public ProductNotContainedInOrderException(String message) {
19+
super(message);
20+
}
21+
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
*/
4+
package coreapi;
5+
6+
/**
7+
* @author Maria
8+
* @author Borja
9+
* @version 0.1
10+
*
11+
*/
12+
public class UnreachableStatusException extends Exception
13+
{
14+
15+
/**
16+
* @param message
17+
*/
18+
public UnreachableStatusException(String message) {
19+
super(message);
20+
}
21+
}

0 commit comments

Comments
 (0)