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.
void addProduct(String productId, int price, int stock)
productId is new, add it with the given price and stock.productId already exists, set its price to price and add stock to its current stock.boolean addToCart(String userId, String productId, int quantity)
quantity units of the product to the user's cart and returns true.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.boolean removeFromCart(String userId, String productId, int quantity)
false if the product is not in the user's cart.quantity and returns true. If the cart quantity becomes 0 or less, the product is removed from the cart.List<String> getCart(String userId)
"productId,quantity", sorted by productId in lexicographical order.long getCartTotal(String userId)
price * quantity for all cart items, using current prices.0 if the cart is empty.boolean placeOrder(String userId, String orderId)
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."PLACED", the cart becomes empty and true is returned.orderId counts as used only after a successful order.boolean cancelOrder(String userId, String orderId)
false if the order does not exist, does not belong to userId, or is already cancelled."CANCELLED", adds the ordered quantities back to stock and returns true.List<String> getOrders(String userId)
"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.1 ≤ productId.length, userId.length, orderId.length ≤ 201 ≤ price ≤ 100,0001 ≤ stock ≤ 100,0001 ≤ quantity ≤ 100,00010,000 method calls will be made in total.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.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.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.