Skip to content

Commit 4462f91

Browse files
committed
feat: adds Kimberlee's Exception and Error Handling
first Draft
1 parent 6185a38 commit 4462f91

File tree

1 file changed

+33
-6
lines changed
  • lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce

1 file changed

+33
-6
lines changed

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,51 @@ 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) {
21-
Product product = products.get(productId);
20+
public String placeOrder(String productId, int quantity) throws ProductNotFoundException, InvalidOrderQuantityException {
21+
Product product = products.get(productId); {
2222
String orderId = UUID.randomUUID().toString();
2323
orders.put(orderId, new Order(orderId, product, quantity));
2424
return orderId;
25+
26+
if (! products.containsKey(productId));
27+
throw new ProductNotFoundException("Product with ID " + productId + "not found");
2528
}
29+
if (quantity < 1); {
30+
throw new InvalidOrderQuantityException("Quantity must be at least 1");
31+
}
32+
33+
}
2634

27-
public void cancelOrder(String orderId) {
28-
orders.remove(orderId);
35+
public void cancelOrder(String orderId) throws OrderNotFoundException {
36+
orders.remove(orderId); {
37+
38+
Order order = orders.get(orderId);
39+
if (order == null) {
40+
throw new OrderNotFoundException("Order with ID " + orderId + "not found");
41+
}
42+
if (order.getStatus().equals("Canceled")) {
43+
throw new OrderNotFoundException("Order has already been canceled");
44+
}
45+
if (order.getStatus().equals("Processed")) {
46+
throw new OrderNotFoundException("Order has already been processed");
47+
}
48+
}
2949
}
3050

31-
public String checkOrderStatus(String orderId) {
32-
Order order = orders.get(orderId);
51+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
52+
Order order = orders.get(orderId);
3353
return "Order ID: "
3454
+ orderId
3555
+ ", Product: "
3656
+ order.getProduct().getName()
3757
+ ", Quantity: "
3858
+ order.getQuantity();
59+
60+
if (orders.get(orderId) == null) {
61+
throw new OrderNotFoundException("Order with ID " + orderId + "not found");
3962
}
63+
if (orders.remove(orderId)) {
64+
throw new OrderNotFoundException("Order with ID " + orderId + "not found");
65+
}
66+
}
4067
}

0 commit comments

Comments
 (0)