File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -17,9 +17,16 @@ 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 ) {
20
+ // Tells the method that it should expect to throw an exception
21
+ public String placeOrder (String productId , int quantity ) throws ProductNotFoundException {
22
+
21
23
Product product = products .get (productId );
22
24
String orderId = UUID .randomUUID ().toString ();
25
+
26
+ if (product == null ) {
27
+ throw new ProductNotFoundException ("Product with ID " + productId + " not found" );
28
+ }
29
+
23
30
orders .put (orderId , new Order (orderId , product , quantity ));
24
31
return orderId ;
25
32
}
@@ -28,8 +35,16 @@ public void cancelOrder(String orderId) {
28
35
orders .remove (orderId );
29
36
}
30
37
31
- public String checkOrderStatus (String orderId ) {
38
+ // Modified the method declaration to tell this method to expect and Exception.
39
+ public String checkOrderStatus (String orderId ) throws OrderNotFoundException {
32
40
Order order = orders .get (orderId );
41
+
42
+ // Checks if the user has entered nothing
43
+ if (order == null ) {
44
+ // Throws and error and sends back a message
45
+ throw new OrderNotFoundException ("Order with ID " + orderId + " not found" );
46
+ }
47
+
33
48
return "Order ID: "
34
49
+ orderId
35
50
+ ", Product: "
You can’t perform that action at this time.
0 commit comments