Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Product product = products.get(productId);
if (product == null) {
throw new ProductNotFoundException("Product with ID " + productId + " 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("Order with order ID " + orderId + " does not exist");
} catch (OrderNotFoundException e) {
System.out.println("Order with order ID " + orderId + " 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 " + orderId + " not found");
return "Order ID: "
+ orderId
+ ", Product: "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

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
@@ -1,8 +1,7 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

package com.codedifferently.lesson14.ecommerce;

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