Skip to content

Commit c7bc227

Browse files
updated existing code to add exceptions
1 parent bf96654 commit c7bc227

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
import java.util.UUID;
66

77
public class EcommerceSystem {
8+
89
private Map<String, Product> products;
910
private Map<String, Order> orders;
11+
private Map<String, String> productToOrderId;
1012

1113
public EcommerceSystem() {
1214
products = new HashMap<>();
1315
orders = new HashMap<>();
16+
productToOrderId = new HashMap<>();
1417
}
1518

1619
public void addProduct(String productId, String name) {
@@ -19,22 +22,38 @@ public void addProduct(String productId, String name) {
1922

2023
public String placeOrder(String productId, int quantity) {
2124
Product product = products.get(productId);
25+
if (product == null) {
26+
throw new ProductNotFoundException("Product with ID " + productId + " not found");
27+
}
2228
String orderId = UUID.randomUUID().toString();
23-
orders.put(orderId, new Order(orderId, product, quantity));
29+
Order order = new Order(orderId, product, quantity);
30+
orders.put(orderId, order);
31+
productToOrderId.put(productId, orderId); // so we can find the order by productId
2432
return orderId;
2533
}
2634

2735
public void cancelOrder(String orderId) {
2836
orders.remove(orderId);
2937
}
3038

31-
public String checkOrderStatus(String orderId) {
32-
Order order = orders.get(orderId);
33-
return "Order ID: "
34-
+ orderId
35-
+ ", Product: "
36-
+ order.getProduct().getName()
37-
+ ", Quantity: "
38-
+ order.getQuantity();
39+
public String checkOrderStatus(String id) {
40+
// Check if this is a real order ID
41+
Order order = orders.get(id);
42+
if (order != null) {
43+
return "Order ID: "
44+
+ id
45+
+ ", Product: "
46+
+ order.getProduct().getName()
47+
+ ", Quantity: "
48+
+ order.getQuantity();
49+
}
50+
51+
// If a product ID was passed instead of an order ID, and it had a cancelled order
52+
if (productToOrderId.containsKey(id) && !orders.containsKey(productToOrderId.get(id))) {
53+
throw new OrderNotFoundException("Order with ID " + id + " not found");
54+
}
55+
56+
// If not found at all
57+
throw new OrderNotFoundException("Order with ID " + id + " not found");
3958
}
4059
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.codedifferently.lesson14.ecommerce;
22

33
public class Order {
4-
private String orderId;
5-
private Product product;
6-
private int quantity;
4+
private final String id;
5+
private final Product product;
6+
private final int quantity;
77

8-
public Order(String orderId, Product product, int quantity) {
9-
this.orderId = orderId;
8+
public Order(String id, Product product, int quantity) {
9+
this.id = id;
1010
this.product = product;
1111
this.quantity = quantity;
1212
}
1313

14-
public String getOrderId() {
15-
return orderId;
14+
public String getId() {
15+
return id;
1616
}
1717

1818
public Product getProduct() {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/*
2-
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3-
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4-
*/
5-
61
package com.codedifferently.lesson14.ecommerce;
72

8-
class OrderNotFoundException {}
3+
public class OrderNotFoundException extends RuntimeException {
4+
public OrderNotFoundException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/*
2-
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3-
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4-
*/
5-
61
package com.codedifferently.lesson14.ecommerce;
72

8-
class ProductNotFoundException {}
3+
public class ProductNotFoundException extends RuntimeException {
4+
public ProductNotFoundException(String message) {
5+
super(message);
6+
}
7+
}

0 commit comments

Comments
 (0)