Skip to content

Commit 4180769

Browse files
committed
chore: fix formattitng
1 parent 1d4c1b1 commit 4180769

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.UUID;
66

77
public class EcommerceSystem {
8+
89
private Map<String, Product> products;
910
private Map<String, Order> orders;
1011

@@ -17,8 +18,11 @@ 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 " + productId + " not found");
25+
}
2226
String orderId = UUID.randomUUID().toString();
2327
orders.put(orderId, new Order(orderId, product, quantity));
2428
return orderId;
@@ -28,8 +32,11 @@ public void cancelOrder(String orderId) {
2832
orders.remove(orderId);
2933
}
3034

31-
public String checkOrderStatus(String orderId) {
35+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
3236
Order order = orders.get(orderId);
37+
if (order == null) {
38+
throw new OrderNotFoundException("Order with ID " + orderId + " not found");
39+
}
3340
return "Order ID: "
3441
+ orderId
3542
+ ", Product: "

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
33
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
44
*/
5-
65
package com.codedifferently.lesson14.ecommerce;
76

8-
class ProductNotFoundException {}
7+
class ProductNotFoundException extends RuntimeException {
8+
9+
ProductNotFoundException(String message) {
10+
super(message);
11+
}
12+
}

0 commit comments

Comments
 (0)