Skip to content

Commit b786150

Browse files
committed
Feat: Adds-Changes to Lesson14 .Java files for Nile Jackson
1 parent c601e7f commit b786150

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55
import java.util.UUID;
66

7+
78
public class EcommerceSystem {
89
private Map<String, Product> products;
910
private Map<String, Order> orders;
@@ -17,19 +18,31 @@ public void addProduct(String productId, String name) {
1718
products.put(productId, new Product(productId, name));
1819
}
1920

20-
public String placeOrder(String productId, int quantity) {
21+
public String placeOrder(String productId, int quantity) throws ProductNotFoundException{
2122
Product product = products.get(productId);
23+
if (product == null) {
24+
throw new ProductNotFoundException("Product with Id 1 Not found ");
25+
}
2226
String orderId = UUID.randomUUID().toString();
2327
orders.put(orderId, new Order(orderId, product, quantity));
2428
return orderId;
2529
}
2630

27-
public void cancelOrder(String orderId) {
31+
public void cancelOrder(String orderId) throws
32+
OrderNotFoundException{
33+
Order order = orders.get(orderId);
34+
try {
35+
if (order == null ) throw new
36+
OrderNotFoundException("No Such order exists");
37+
} catch (OrderNotFoundException e) {
38+
System.out.println(" order does not exist");
39+
}
2840
orders.remove(orderId);
2941
}
3042

31-
public String checkOrderStatus(String orderId) {
43+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
3244
Order order = orders.get(orderId);
45+
if(order == null) throw new OrderNotFoundException("order with ID 1 not found");
3346
return "Order ID: "
3447
+ orderId
3548
+ ", Product: "

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@
55

66
package com.codedifferently.lesson14.ecommerce;
77

8-
class OrderNotFoundException {}
8+
class OrderNotFoundException extends RuntimeException{
9+
10+
public OrderNotFoundException(String message) {
11+
super(message);
12+
}
13+
}
14+
15+
16+

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)