497. Design E-commerce System with Cart, Stock and Orders
Asked in
Design E-commerce System with Cart, Stock and Orders

Design a simple eKart e-commerce system. Products are added with a price and stock. Users add products to their cart and then place orders for the whole cart. A placed order can be cancelled later.

All ids (productId, userId, orderId) are given as input. A user starts with an empty cart and no orders.

Methods

Add Product

void addProduct(String productId, int price, int stock)
  • If productId is new, add it with the given price and stock.
  • If productId already exists, set its price to price and add stock to its current stock.

Add to Cart

boolean addToCart(String userId, String productId, int quantity)
  • Adds quantity units of the product to the user's cart and returns true.
  • Returns false and changes nothing if the product does not exist, or if the cart quantity of this product after adding would be more than its current stock.
  • Adding to a cart does not reduce stock.

Remove from Cart

boolean removeFromCart(String userId, String productId, int quantity)
  • Returns false if the product is not in the user's cart.
  • Otherwise reduces its cart quantity by quantity and returns true. If the cart quantity becomes 0 or less, the product is removed from the cart.

Get Cart

List<String> getCart(String userId)
  • Returns each cart item as "productId,quantity", sorted by productId in lexicographical order.
  • Returns an empty list if the cart is empty.

Get Cart Total

long getCartTotal(String userId)
  • Returns the sum of price * quantity for all cart items, using current prices.
  • Returns 0 if the cart is empty.

Place Order

boolean placeOrder(String userId, String orderId)
  • Places one order for the whole cart of the user.
  • Returns false and changes nothing if orderId is already used by any user, the cart is empty, or any cart product has less stock than its cart quantity.
  • On success, stock of each cart product is reduced by its cart quantity, the order total is saved using current prices, the order status becomes "PLACED", the cart becomes empty and true is returned.
  • An orderId counts as used only after a successful order.

Cancel Order

boolean cancelOrder(String userId, String orderId)
  • Returns false if the order does not exist, does not belong to userId, or is already cancelled.
  • Otherwise sets the order status to "CANCELLED", adds the ordered quantities back to stock and returns true.

Get Orders

List<String> getOrders(String userId)
  • Returns each order of the user as "orderId,status,total", in the order they were placed.
  • status is "PLACED" or "CANCELLED". total is the amount saved when the order was placed. Later price changes do not change it.
  • Returns an empty list if the user has no orders.

Constraints

  • 1 ≤ productId.length, userId.length, orderId.length ≤ 20
  • All ids contain only lowercase English letters and digits.
  • 1 ≤ price ≤ 100,000
  • 1 ≤ stock ≤ 100,000
  • 1 ≤ quantity ≤ 100,000
  • At most 10,000 method calls will be made in total.

Examples

Example 1

  • addProduct(productId = "pen", price = 20, stock = 10)
  • addProduct(productId = "book", price = 150, stock = 2)
  • addToCart(userId = "asha", productId = "eraser", quantity = 1) returns false. Product "eraser" does not exist.
  • addToCart(userId = "asha", productId = "book", quantity = 3) returns false. Only 2 books are in stock.
  • addToCart(userId = "asha", productId = "book", quantity = 2) returns true.
  • addToCart(userId = "asha", productId = "pen", quantity = 5) returns true.
  • getCart(userId = "asha") returns ["book,2", "pen,5"].
  • getCartTotal(userId = "asha") returns 400. 2 * 150 + 5 * 20 = 400.
  • addProduct(productId = "pen", price = 30, stock = 5). Pen price becomes 30 and its stock becomes 15.
  • getCartTotal(userId = "asha") returns 450. 2 * 150 + 5 * 30 = 450.
  • removeFromCart(userId = "asha", productId = "pen", quantity = 9) returns true. 5 - 9 is less than 0, so pen is removed from the cart.
  • getCart(userId = "asha") returns ["book,2"].
  • removeFromCart(userId = "asha", productId = "pen", quantity = 1) returns false. Pen is not in the cart.

Example 2

  • addProduct(productId = "lamp", price = 900, stock = 3)
  • addProduct(productId = "bulb", price = 50, stock = 10)
  • addToCart(userId = "ravi", productId = "lamp", quantity = 2) returns true.
  • addToCart(userId = "ravi", productId = "bulb", quantity = 4) returns true.
  • addToCart(userId = "meera", productId = "lamp", quantity = 3) returns true. Carts do not reduce stock, so 3 lamps are still in stock.
  • placeOrder(userId = "meera", orderId = "o100") returns true. Total is 2,700 and lamp stock becomes 0.
  • placeOrder(userId = "ravi", orderId = "o200") returns false. Ravi needs 2 lamps but 0 are in stock. Nothing changes.
  • getCart(userId = "ravi") returns ["bulb,4", "lamp,2"].
  • cancelOrder(userId = "ravi", orderId = "o100") returns false. Order "o100" belongs to meera.
  • cancelOrder(userId = "meera", orderId = "o100") returns true. Lamp stock becomes 3 again.
  • placeOrder(userId = "ravi", orderId = "o100") returns false. Order id "o100" is already used.
  • placeOrder(userId = "ravi", orderId = "o200") returns true. Total is 2 * 900 + 4 * 50 = 2,000. Lamp stock becomes 1 and bulb stock becomes 6.
  • getCart(userId = "ravi") returns [].
  • getOrders(userId = "meera") returns ["o100,CANCELLED,2700"].
  • getOrders(userId = "ravi") returns ["o200,PLACED,2000"].
  • cancelOrder(userId = "meera", orderId = "o100") returns false. The order is already cancelled.

Example 3

  • addProduct(productId = "mug", price = 120, stock = 5)
  • placeOrder(userId = "kiran", orderId = "k1") returns false. The cart is empty, so "k1" is not used.
  • addToCart(userId = "kiran", productId = "mug", quantity = 2) returns true.
  • placeOrder(userId = "kiran", orderId = "k1") returns true. Total is 240 and mug stock becomes 3.
  • addProduct(productId = "mug", price = 200, stock = 1). Mug price becomes 200 and its stock becomes 4.
  • addToCart(userId = "kiran", productId = "mug", quantity = 4) returns true.
  • addToCart(userId = "kiran", productId = "mug", quantity = 1) returns false. Cart quantity would be 5 but stock is 4.
  • getCartTotal(userId = "kiran") returns 800.
  • placeOrder(userId = "kiran", orderId = "k2") returns true. Total is 800 and mug stock becomes 0.
  • cancelOrder(userId = "kiran", orderId = "k1") returns true. Mug stock becomes 2.
  • getOrders(userId = "kiran") returns ["k1,CANCELLED,240", "k2,PLACED,800"]. Order "k1" keeps its old total of 240 even though the mug price is now 200.


Please use Laptop/Desktop or any other large screen to add/edit code.