Skip to content

Commit

Permalink
feat: adding exception in lesson14 homework by Yemi
Browse files Browse the repository at this point in the history
  • Loading branch information
jimoye244 committed Oct 25, 2024
1 parent c601e7f commit be83c8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@ public void addProduct(String productId, String name) {
products.put(productId, new Product(productId, name));
}

public String placeOrder(String productId, int quantity) {
public String placeOrder(String productId, int quantity)
throws ProductNotFoundException, Exception {
Product product = products.get(productId);
if (product == null) {
throw new ProductNotFoundException("Product with ID 1 not found");
}
String orderId = UUID.randomUUID().toString();
orders.put(orderId, new Order(orderId, product, quantity));
return orderId;
}

public void cancelOrder(String orderId) {
public void cancelOrder(String orderId) throws OrderNotFoundException {
Order order = orders.get(orderId);
try {
if (order == null) throw new OrderNotFoundException("Such Order does not exist");
} catch (OrderNotFoundException e) {
System.out.println(" Order does not exist");
}
orders.remove(orderId);
}

public String checkOrderStatus(String orderId) {
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
Order order = orders.get(orderId);
if (order == null) throw new OrderNotFoundException("Order with ID 1 not found");
return "Order ID: "
+ orderId
+ ", Product: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

package com.codedifferently.lesson14.ecommerce;

class OrderNotFoundException {}
class OrderNotFoundException extends RuntimeException {
public OrderNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

package com.codedifferently.lesson14.ecommerce;

class ProductNotFoundException {}
class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(String message) {
super(message);
}
}

0 comments on commit be83c8d

Please sign in to comment.