@@ -17,24 +17,51 @@ public void addProduct(String productId, String name) {
17
17
products .put (productId , new Product (productId , name ));
18
18
}
19
19
20
- public String placeOrder (String productId , int quantity ) {
21
- Product product = products .get (productId );
20
+ public String placeOrder (String productId , int quantity ) throws ProductNotFoundException , InvalidOrderQuantityException {
21
+ Product product = products .get (productId ); {
22
22
String orderId = UUID .randomUUID ().toString ();
23
23
orders .put (orderId , new Order (orderId , product , quantity ));
24
24
return orderId ;
25
+
26
+ if (! products .containsKey (productId ));
27
+ throw new ProductNotFoundException ("Product with ID " + productId + "not found" );
25
28
}
29
+ if (quantity < 1 ); {
30
+ throw new InvalidOrderQuantityException ("Quantity must be at least 1" );
31
+ }
32
+
33
+ }
26
34
27
- public void cancelOrder (String orderId ) {
28
- orders .remove (orderId );
35
+ public void cancelOrder (String orderId ) throws OrderNotFoundException {
36
+ orders .remove (orderId ); {
37
+
38
+ Order order = orders .get (orderId );
39
+ if (order == null ) {
40
+ throw new OrderNotFoundException ("Order with ID " + orderId + "not found" );
41
+ }
42
+ if (order .getStatus ().equals ("Canceled" )) {
43
+ throw new OrderNotFoundException ("Order has already been canceled" );
44
+ }
45
+ if (order .getStatus ().equals ("Processed" )) {
46
+ throw new OrderNotFoundException ("Order has already been processed" );
47
+ }
48
+ }
29
49
}
30
50
31
- public String checkOrderStatus (String orderId ) {
32
- Order order = orders .get (orderId );
51
+ public String checkOrderStatus (String orderId ) throws OrderNotFoundException {
52
+ Order order = orders .get (orderId );
33
53
return "Order ID: "
34
54
+ orderId
35
55
+ ", Product: "
36
56
+ order .getProduct ().getName ()
37
57
+ ", Quantity: "
38
58
+ order .getQuantity ();
59
+
60
+ if (orders .get (orderId ) == null ) {
61
+ throw new OrderNotFoundException ("Order with ID " + orderId + "not found" );
39
62
}
63
+ if (orders .remove (orderId )) {
64
+ throw new OrderNotFoundException ("Order with ID " + orderId + "not found" );
65
+ }
66
+ }
40
67
}
0 commit comments