Skip to content

Commit 281f9d7

Browse files
committed
chore: added custom exceptions
1 parent 4462f91 commit 281f9d7

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

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

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

3532
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-
}
33+
orders.remove(orderId);
4934
}
5035

5136
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
52-
Order order = orders.get(orderId);
37+
Order order = orders.get(orderId);
38+
if (orders.get(orderId) == null) {
39+
throw new OrderNotFoundException("Order with ID " + orderId + " not found");
40+
}
5341
return "Order ID: "
5442
+ orderId
5543
+ ", Product: "
5644
+ order.getProduct().getName()
5745
+ ", Quantity: "
5846
+ order.getQuantity();
59-
60-
if (orders.get(orderId) == null) {
61-
throw new OrderNotFoundException("Order with ID " + orderId + "not found");
6247
}
63-
if (orders.remove(orderId)) {
64-
throw new OrderNotFoundException("Order with ID " + orderId + "not found");
65-
}
66-
}
6748
}

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+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55

66
package com.codedifferently.lesson14.ecommerce;
77

8-
class ProductNotFoundException {}
8+
class ProductNotFoundException extends Exception {
9+
10+
public ProductNotFoundException(String message) {
11+
super(message);
12+
}
13+
}

0 commit comments

Comments
 (0)