Skip to content

Commit 46862a3

Browse files
committed
CHORE: Added the neeeded exceptions.
1 parent 4fb3601 commit 46862a3

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/Lesson14.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class Lesson14 {
44

55
public static void main(String[] args) {
6+
67
System.out.println("Hello World");
78
}
89
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ 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+
23+
if (product == null) {
24+
// This will throw a custom exception when order is not found, or null.
25+
throw new ProductNotFoundException("Product with ID " + productId + " not found");
26+
}
27+
2228
String orderId = UUID.randomUUID().toString();
2329
orders.put(orderId, new Order(orderId, product, quantity));
2430
return orderId;
@@ -28,8 +34,14 @@ public void cancelOrder(String orderId) {
2834
orders.remove(orderId);
2935
}
3036

31-
public String checkOrderStatus(String orderId) {
37+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
3238
Order order = orders.get(orderId);
39+
40+
if (order == null) {
41+
// This will throw a custom exception when order is not found, or null.
42+
throw new OrderNotFoundException("Order with ID " + orderId + " not found");
43+
}
44+
3345
return "Order ID: "
3446
+ orderId
3547
+ ", 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+
public 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+
public 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void testCancelOrder() throws Exception {
5858
}
5959

6060
@Test
61-
void testCheckOrderStatus_orderDoesNotExist() {
61+
void testCheckOrderStatus_orderDoesNotExist() throws Exception {
6262
// Act
6363
assertThatThrownBy(() -> ecommerceSystem.checkOrderStatus("1"))
6464
.isInstanceOf(OrderNotFoundException.class)

0 commit comments

Comments
 (0)