From bd775064c6a90080b848c2a390d1e58a0cb91029 Mon Sep 17 00:00:00 2001 From: Yafiaha Date: Mon, 28 Oct 2024 09:56:39 -0400 Subject: [PATCH 1/2] Feat: Yafiah Lesson-14 Custom Exceptions in Ecommerce System --- .../lesson14/ecommerce/EcommerceSystem.java | 10 ++++++++-- .../lesson14/ecommerce/OrderNotFoundException.java | 7 ++++++- .../lesson14/ecommerce/ProductNotFoundException.java | 6 +++++- 3 files changed, 19 insertions(+), 4 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..fa4fa82b 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,8 +17,11 @@ 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; @@ -28,8 +31,11 @@ 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..55d31eac 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,9 @@ package com.codedifferently.lesson14.ecommerce; -class OrderNotFoundException {} +class OrderNotFoundException extends RuntimeException{ + 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..00a9cccc 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 RuntimeException { + public ProductNotFoundException(String message) { + super(message); + } +} From 6a652e5a6decf46edda25786ac6b7c8493f3066c Mon Sep 17 00:00:00 2001 From: Yafiaha Date: Mon, 28 Oct 2024 10:01:44 -0400 Subject: [PATCH 2/2] fix-up --- .../codedifferently/lesson14/ecommerce/EcommerceSystem.java | 4 ++-- .../lesson14/ecommerce/OrderNotFoundException.java | 5 ++--- .../lesson14/ecommerce/ProductNotFoundException.java | 6 +++--- 3 files changed, 7 insertions(+), 8 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 fa4fa82b..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 @@ -17,7 +17,7 @@ public void addProduct(String productId, String name) { products.put(productId, new Product(productId, name)); } - public String placeOrder(String productId, int quantity) throws ProductNotFoundException{ + 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"); @@ -31,7 +31,7 @@ public void cancelOrder(String orderId) { orders.remove(orderId); } - public String checkOrderStatus(String orderId) throws OrderNotFoundException{ + public String checkOrderStatus(String orderId) throws OrderNotFoundException { Order order = orders.get(orderId); if (order == null) { throw new OrderNotFoundException("Order with ID " + orderId + " not found"); 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 55d31eac..e4eb0a66 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,9 +5,8 @@ package com.codedifferently.lesson14.ecommerce; -class OrderNotFoundException extends RuntimeException{ +class OrderNotFoundException extends RuntimeException { public OrderNotFoundException(String message) { - super(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 00a9cccc..9b118021 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 RuntimeException { - public ProductNotFoundException(String message) { - super(message); - } + public ProductNotFoundException(String message) { + super(message); + } }