From bf80933f6a55918fe187f36aed9f804d8d7c6949 Mon Sep 17 00:00:00 2001 From: Dasiame Date: Mon, 28 Oct 2024 13:46:17 +0000 Subject: [PATCH] feat: Adds Exceptions to Product & Order file --- .../lesson14/ecommerce/EcommerceSystem.java | 10 ++++++++-- .../lesson14/ecommerce/OrderNotFoundException.java | 6 +++++- .../lesson14/ecommerce/ProductNotFoundException.java | 6 +++++- 3 files changed, 18 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..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,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..3c20f541 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 {} +public 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..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 @@ -5,4 +5,8 @@ package com.codedifferently.lesson14.ecommerce; -class ProductNotFoundException {} +class ProductNotFoundException extends Exception { + public ProductNotFoundException(String message) { + super(message); + } +}