From 158c8a78336f734ebcf8b7af44f3e37f73b580dc Mon Sep 17 00:00:00 2001 From: Cogobonnia <244cogbonnia@gmail.com> Date: Mon, 28 Oct 2024 14:35:15 +0000 Subject: [PATCH] feat: added lesson_14's solutions for Chelsea --- .../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..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 @@ -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..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); + } +}