Skip to content

Commit 0c86c2a

Browse files
committed
Feat: Adds Shawn Dunsmore Jr Lesson14 Handling Exceptions.
1 parent 43b0c16 commit 0c86c2a

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ 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) throws ProductNotFoundException {
2121
Product product = products.get(productId);
22+
if (product == null){
23+
throw new ProductNotFoundException("Product with ID "+ productId +" not found");
24+
}
2225
String orderId = UUID.randomUUID().toString();
2326
orders.put(orderId, new Order(orderId, product, quantity));
2427
return orderId;
2528
}
2629

27-
public void cancelOrder(String orderId) {
30+
public void cancelOrder(String orderId){
2831
orders.remove(orderId);
2932
}
3033

31-
public String checkOrderStatus(String orderId) {
34+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
3235
Order order = orders.get(orderId);
36+
if (order == null){
37+
throw new OrderNotFoundException("Order with ID "+ orderId+ " not found");
38+
}
3339
return "Order ID: "
3440
+ orderId
3541
+ ", 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 Exception {
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 Exception {
9+
public ProductNotFoundException(String message) {
10+
super(message);
11+
}
12+
}

lesson_14/exceptions/exceptions_app/src/test/java/com/codedifferently/lesson14/ecommerce/EcommerceSystemTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5-
65
import org.junit.jupiter.api.BeforeEach;
76
import org.junit.jupiter.api.Test;
87

0 commit comments

Comments
 (0)