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 @@ -5,12 +5,15 @@
import java.util.UUID;

public class EcommerceSystem {

private Map<String, Product> products;
private Map<String, Order> orders;
private Map<String, String> productToOrderId;

public EcommerceSystem() {
products = new HashMap<>();
orders = new HashMap<>();
productToOrderId = new HashMap<>();
}

public void addProduct(String productId, String name) {
Expand All @@ -19,22 +22,38 @@ public void addProduct(String productId, String name) {

public String placeOrder(String productId, int quantity) {
Product product = products.get(productId);
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));
Order order = new Order(orderId, product, quantity);
orders.put(orderId, order);
productToOrderId.put(productId, orderId); // so we can find the order by productId
return orderId;
}

public void cancelOrder(String orderId) {
orders.remove(orderId);
}

public String checkOrderStatus(String orderId) {
Order order = orders.get(orderId);
return "Order ID: "
+ orderId
+ ", Product: "
+ order.getProduct().getName()
+ ", Quantity: "
+ order.getQuantity();
public String checkOrderStatus(String id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Took a long time to understand what you were doing here. Could've essentially solved this in three lines of code (just check if the order is null).

// Check if this is a real order ID
Order order = orders.get(id);
if (order != null) {
return "Order ID: "
+ id
+ ", Product: "
+ order.getProduct().getName()
+ ", Quantity: "
+ order.getQuantity();
}

// If a product ID was passed instead of an order ID, and it had a cancelled order
if (productToOrderId.containsKey(id) && !orders.containsKey(productToOrderId.get(id))) {
throw new OrderNotFoundException("Order with ID " + id + " not found");
}

// If not found at all
throw new OrderNotFoundException("Order with ID " + id + " not found");
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.codedifferently.lesson14.ecommerce;

public class Order {
private String orderId;
private Product product;
private int quantity;
private final String id;
private final Product product;
private final int quantity;

public Order(String orderId, Product product, int quantity) {
this.orderId = orderId;
public Order(String id, Product product, int quantity) {
this.id = id;
this.product = product;
this.quantity = quantity;
}

public String getOrderId() {
return orderId;
public String getId() {
return id;
}

public Product getProduct() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

package com.codedifferently.lesson14.ecommerce;

class OrderNotFoundException {}
public class OrderNotFoundException extends RuntimeException {
public OrderNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

package com.codedifferently.lesson14.ecommerce;

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