From 3ed51f74d199e624e003e30da4fb34af6f02f854 Mon Sep 17 00:00:00 2001 From: cdbluejr Date: Mon, 28 Oct 2024 15:31:04 +0000 Subject: [PATCH 1/2] Feat: Lesson 14 Ecommerce System Exceptions by Dwight Blue --- .../lesson14/ecommerce/EcommerceSystem.java | 12 +++++++++--- .../lesson14/ecommerce/OrderNotFoundException.java | 6 +++++- .../lesson14/ecommerce/ProductNotFoundException.java | 6 +++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java index 77cf2693..dc1913e2 100644 --- a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java +++ b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java @@ -17,19 +17,25 @@ 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) { 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: " diff --git a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java index 0104e0ab..135bdcbc 100644 --- a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java +++ b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java @@ -5,4 +5,8 @@ package com.codedifferently.lesson14.ecommerce; -class OrderNotFoundException {} +class OrderNotFoundException extends Exception { + public OrderNotFoundException(String message) { + super(message); + } +} diff --git a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java index 25aa814e..650ab2e9 100644 --- a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java +++ b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java @@ -5,4 +5,8 @@ package com.codedifferently.lesson14.ecommerce; -class ProductNotFoundException {} +class ProductNotFoundException extends Exception { + public ProductNotFoundException(String message) { + super(message); + } +} From d82280cae1394cbda4ea97e01caebc7772bf80a9 Mon Sep 17 00:00:00 2001 From: cdbluejr Date: Mon, 28 Oct 2024 15:34:53 +0000 Subject: [PATCH 2/2] Spot: Lesson 14 Ecommerce System Exceptions by Dwight Blue --- .../codedifferently/lesson14/ecommerce/EcommerceSystem.java | 2 +- .../lesson14/ecommerce/OrderNotFoundException.java | 6 +++--- .../lesson14/ecommerce/ProductNotFoundException.java | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java index dc1913e2..b0ebe115 100644 --- a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java +++ b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/EcommerceSystem.java @@ -26,7 +26,7 @@ public String placeOrder(String productId, int quantity) throws ProductNotFoundE orders.put(orderId, new Order(orderId, product, quantity)); return orderId; } - + public void cancelOrder(String orderId) { orders.remove(orderId); } diff --git a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java index 135bdcbc..50607ed3 100644 --- a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java +++ b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java @@ -6,7 +6,7 @@ package com.codedifferently.lesson14.ecommerce; class OrderNotFoundException extends Exception { - public OrderNotFoundException(String message) { - super(message); - } + public OrderNotFoundException(String message) { + super(message); + } } diff --git a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java index 650ab2e9..196ac84a 100644 --- a/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java +++ b/lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java @@ -6,7 +6,7 @@ package com.codedifferently.lesson14.ecommerce; class ProductNotFoundException extends Exception { - public ProductNotFoundException(String message) { - super(message); - } + public ProductNotFoundException(String message) { + super(message); + } }