Skip to content

Commit 8f6142f

Browse files
author
AmiyahJo
committed
feat: adds new exceptoons in ecommerance , adds more code in both exceptions to extend
1 parent ec2ce62 commit 8f6142f

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/Lesson14.java

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

3-
import com.codedifferently.lesson14.ecommerce.EcommerceSystem;
4-
import com.codedifferently.lesson14.ecommerce.OrderNotFoundException;
5-
import com.codedifferently.lesson14.ecommerce.ProductNotFoundException;
3+
// import com.codedifferently.lesson14.ecommerce.EcommerceSystem;
4+
// import com.codedifferently.lesson14.ecommerce.OrderNotFoundException;
5+
// import com.codedifferently.lesson14.ecommerce.ProductNotFoundException;
66

77
public class Lesson14 {
88

99
public static void main(String[] args) {
10-
String orderId = null;
11-
try {
12-
orderId = ecommerceSystem.placeOrder("1", 1);
13-
} catch (ProductNotFoundException e) {
14-
System.out.println("Product with ID " + "1" + " not found.");
15-
} catch (OrderNotFoundException e){
16-
System.out.println("Order with ID " + orderId + " not found.");
17-
}
10+
System.out.println("Hello world");
11+
// String orderId = null;
12+
// try {
13+
// orderId = ecommerceSystem.placeOrder("1", 1);
14+
// } catch (ProductNotFoundException e) {
15+
// System.out.println("Product with ID " + "1" + " not found.");
16+
// } catch (OrderNotFoundException e){
17+
// System.out.println("Order with ID " + orderId + " not found.");
18+
// }
1819
}
1920
}
2021

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 " + productId + " 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 " + orderId + " not found");
38+
}
3339
return "Order ID: "
3440
+ orderId
3541
+ ", Product: "

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/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)