Skip to content

Commit be83c8d

Browse files
committed
feat: adding exception in lesson14 homework by Yemi
1 parent c601e7f commit be83c8d

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,30 @@ 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+
public String placeOrder(String productId, int quantity)
21+
throws ProductNotFoundException, Exception {
2122
Product product = products.get(productId);
23+
if (product == null) {
24+
throw new ProductNotFoundException("Product with ID 1 not found");
25+
}
2226
String orderId = UUID.randomUUID().toString();
2327
orders.put(orderId, new Order(orderId, product, quantity));
2428
return orderId;
2529
}
2630

27-
public void cancelOrder(String orderId) {
31+
public void cancelOrder(String orderId) throws OrderNotFoundException {
32+
Order order = orders.get(orderId);
33+
try {
34+
if (order == null) throw new OrderNotFoundException("Such Order does not exist");
35+
} catch (OrderNotFoundException e) {
36+
System.out.println(" Order does not exist");
37+
}
2838
orders.remove(orderId);
2939
}
3040

31-
public String checkOrderStatus(String orderId) {
41+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
3242
Order order = orders.get(orderId);
43+
if (order == null) throw new OrderNotFoundException("Order with ID 1 not found");
3344
return "Order ID: "
3445
+ orderId
3546
+ ", Product: "

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55

66
package com.codedifferently.lesson14.ecommerce;
77

8-
class OrderNotFoundException {}
8+
class OrderNotFoundException extends RuntimeException {
9+
public OrderNotFoundException(String message) {
10+
super(message);
11+
}
12+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55

66
package com.codedifferently.lesson14.ecommerce;
77

8-
class ProductNotFoundException {}
8+
class ProductNotFoundException extends RuntimeException {
9+
public ProductNotFoundException(String message) {
10+
super(message);
11+
}
12+
}

0 commit comments

Comments
 (0)