Skip to content
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,9 +17,16 @@ public void addProduct(String productId, String name) {
products.put(productId, new Product(productId, name));
}

public String placeOrder(String productId, int quantity) {
// Tells the method that it should expect to throw an exception
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're going to add a comment for a method, you must use JavaDoc:

https://stackoverflow.com/questions/11763803/how-to-document-my-method-in-java-like-java-docs

public String placeOrder(String productId, int quantity) throws ProductNotFoundException {

Product product = products.get(productId);
String orderId = UUID.randomUUID().toString();

if (product == null) {
throw new ProductNotFoundException("Product with ID " + productId + " not found");
}

orders.put(orderId, new Order(orderId, product, quantity));
return orderId;
}
Expand All @@ -28,8 +35,16 @@ public void cancelOrder(String orderId) {
orders.remove(orderId);
}

public String checkOrderStatus(String orderId) {
// Modified the method declaration to tell this method to expect and Exception.
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
Order order = orders.get(orderId);

// Checks if the user has entered nothing
if (order == null) {
// Throws and error and sends back a message
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,9 @@

package com.codedifferently.lesson14.ecommerce;

class OrderNotFoundException {}
public 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 {}
public class ProductNotFoundException extends Exception {
public ProductNotFoundException(String message) {
super(message);
}
}