Skip to content

Commit 0856d5d

Browse files
committed
feat: adds exception handling for product and order not found in EcommerceSystem
1 parent b003749 commit 0856d5d

File tree

1 file changed

+17
-2
lines changed
  • lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce

1 file changed

+17
-2
lines changed

lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff 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: "

0 commit comments

Comments
 (0)