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 77cf26937..447af73db 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,20 @@ 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); + /*the reason why i put the throw here is due to the fact that the product is being declared and we know in the test cases + * that we are given a product id, what we dont know is if thaat productId is correct so technically speaking we should check + * if the productId is matched up to the hashmap at all using get and if it doesnt exist then product will be null because no such key exists + * instead of trying to put it in every single instance we see an order id or product id our test cases check to see if the given + * input is even available for us to grab not if there is any input at all since that itself in my opinion would be front end and not us + * that would handle that sort of thing before it reaches this point + * + * If you need me to delete comments I can i just wrote this out incase people look at any pr's for their own reasoning or what not + */ + 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 +40,20 @@ public void cancelOrder(String orderId) { orders.remove(orderId); } - public String checkOrderStatus(String orderId) { + public String checkOrderStatus(String orderId) + throws OrderNotFoundException, ProductNotFoundException { + Order order = orders.get(orderId); + // Since order is set at this exact moment all we need to do is check if the given id is in the + // hashmap and if it isnt we know + // that it would return a null value if no such key exists, hence why we would see if order == + // null and if it is throw that error + // we do this instead of having an error at each exact moment because we know in the test cases + // we are given an orderId we just need to make sure + // on our end that it is correct, not if it exists. + 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 0104e0ab8..50607ed33 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 25aa814eb..196ac84ac 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); + } +}