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) {
1717 products .put (productId , new Product (productId , name ));
1818 }
1919
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+
2123 Product product = products .get (productId );
2224 String orderId = UUID .randomUUID ().toString ();
25+
26+ if (product == null ) {
27+ throw new ProductNotFoundException ("Product with ID " + productId + " not found" );
28+ }
29+
2330 orders .put (orderId , new Order (orderId , product , quantity ));
2431 return orderId ;
2532 }
@@ -28,8 +35,16 @@ public void cancelOrder(String orderId) {
2835 orders .remove (orderId );
2936 }
3037
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 {
3240 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+
3348 return "Order ID: "
3449 + orderId
3550 + ", Product: "
You can’t perform that action at this time.
0 commit comments