-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from all commits
959e183
38daaf5
373fadc
f97cdeb
a029d67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: " | ||
|
There was a problem hiding this comment.
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.