Skip to content

Commit 651165f

Browse files
committed
Feat: adds Ecommerce Exception Code
1 parent 69fde1b commit 651165f

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ 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+
if (product == null) {
23+
throw new ProductNotFoundException("Product with ID 1 not found");
24+
}
2225
String orderId = UUID.randomUUID().toString();
2326
orders.put(orderId, new Order(orderId, product, quantity));
2427
return orderId;
@@ -28,8 +31,11 @@ public void cancelOrder(String orderId) {
2831
orders.remove(orderId);
2932
}
3033

31-
public String checkOrderStatus(String orderId) {
34+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
3235
Order order = orders.get(orderId);
36+
if (order == null) {
37+
throw new OrderNotFoundException("Order with ID 1 not found");
38+
}
3339
return "Order ID: "
3440
+ orderId
3541
+ ", Product: "

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,7 +9,7 @@ public Product(String productId, String name) {
99
this.name = name;
1010
}
1111

12-
public String getProductId() /*throws ProductNotFoundException*/ {
12+
public String getProductId() {
1313
if (productId == null) {
1414
try {
1515
throw new ProductNotFoundException("The product ID can not be found.");
@@ -20,7 +20,7 @@ public String getProductId() /*throws ProductNotFoundException*/ {
2020
return productId;
2121
}
2222

23-
public String getName() /*throws ProductNotFoundException*/ {
23+
public String getName() {
2424
if (name == null) {
2525
try {
2626
throw new ProductNotFoundException("Nothing matches that name");

0 commit comments

Comments
 (0)