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..e2f3cd5a 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,30 @@ 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, Exception { Product product = products.get(productId); + if (product == null) { + throw new ProductNotFoundException("Product with ID 1 not found"); + } String orderId = UUID.randomUUID().toString(); orders.put(orderId, new Order(orderId, product, quantity)); return orderId; } - public void cancelOrder(String orderId) { + public void cancelOrder(String orderId) throws OrderNotFoundException { + Order order = orders.get(orderId); + try { + if (order == null) throw new OrderNotFoundException("Such Order does not exist"); + } catch (OrderNotFoundException e) { + System.out.println(" Order does not exist"); + } 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 1 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..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,4 +5,8 @@ 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..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 @@ -5,4 +5,8 @@ package com.codedifferently.lesson14.ecommerce; -class ProductNotFoundException {} +class ProductNotFoundException extends RuntimeException { + public ProductNotFoundException(String message) { + super(message); + } +}