Skip to content

Commit 959e183

Browse files
committed
fix: adds exception handling to files under ecommerce folder for lesson_14 homework - Joseph Caballero
1 parent c601e7f commit 959e183

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,33 @@ 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+
throw new ProductNotFoundException("Product with ID " + productId + " 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+
if (orders == null) {
33+
throw new OrderNotFoundException("Product with ID " + orderId + " not found");
34+
}
2835
orders.remove(orderId);
2936
}
3037

31-
public String checkOrderStatus(String orderId) {
38+
public String checkOrderStatus(String orderId)
39+
throws OrderNotFoundException, ProductNotFoundException {
40+
3241
Order order = orders.get(orderId);
42+
43+
if (order == null) {
44+
throw new OrderNotFoundException("Order with ID " + orderId + " not found");
45+
}
46+
3347
return "Order ID: "
3448
+ orderId
3549
+ ", Product: "

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public Order(String orderId, Product product, int quantity) {
1111
this.quantity = quantity;
1212
}
1313

14-
public String getOrderId() {
14+
public String getOrderId() throws OrderNotFoundException {
1515
return orderId;
1616
}
1717

18-
public Product getProduct() {
18+
public Product getProduct() throws OrderNotFoundException {
1919
return product;
2020
}
2121

22-
public int getQuantity() {
22+
public int getQuantity() throws OrderNotFoundException {
2323
return quantity;
2424
}
2525
}

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/Product.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public Product(String productId, String name) {
99
this.name = name;
1010
}
1111

12-
public String getProductId() {
12+
public String getProductId() throws ProductNotFoundException {
1313
return productId;
1414
}
1515

16-
public String getName() {
16+
public String getName() throws ProductNotFoundException {
1717
return name;
1818
}
1919
}

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+
}

0 commit comments

Comments
 (0)