Skip to content

fix: adds exception handling to files under the ecommerce folder for lesson_14 homework - Joseph Caballero #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think defensively. Don't assume that your consumers are always going to do the right thing. Throwing this exception will help consumers understand why the operation didn't work in case they did the wrong thing.

*
* 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;
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use proper casing, grammar, and punctuation for your comments. Also, use shorter sentences for clarity. More importantly, I'd suggest that the code is clear enough on its own, so adding it just makes your reader have to do more work.

In general, only add comments to explain something that isn't immediately obvious in the code and that can't be made any simpler by rewriting the code itself.

// 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: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

package com.codedifferently.lesson14.ecommerce;

class OrderNotFoundException {}
class OrderNotFoundException extends Exception {
public OrderNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

package com.codedifferently.lesson14.ecommerce;

class ProductNotFoundException {}
class ProductNotFoundException extends Exception {
public ProductNotFoundException(String message) {
super(message);
}
}