Design an E-commerce/Online Shopping system
On this page
Design an E-commerce/Online Shopping system
Problem Statement
Design an e-commerce system where users can browse products, add items to cart, and place orders. The system should support sellers, inventory management, order processing, and payment handling. Each order goes through multiple states like created, paid, shipped, delivered, and cancelled.
Asked In Companies
Amazon Flipkart Walmart Google Microsoft Meta eBay Salesforce Oracle PayPal Adobe Atlassian Target
Functional Requirements
- Users should be able to add products to cart
- Users can place an order from cart
- Each product belongs to a seller
- Inventory should track stock per product
- Orders should support multiple items
- Order should go through different statuses
- Payments should be processed and tracked
Objects Required
- User
- Cart
- CartItem
- Product
- Seller
- Order
- OrderItem
- Payment
- Inventory
- EcommerceService
- PaymentService
- InventoryService
- OrderStatus
- PaymentStatus
OrderStatus Enum
public enum OrderStatus {
CREATED,
PAID,
SHIPPED,
DELIVERED,
CANCELLED
}
This enum tracks the lifecycle of an order. Every order starts in CREATED state and moves forward as it is processed, shipped, and delivered. If something goes wrong, it can also be cancelled.
PaymentStatus Enum
public enum PaymentStatus {
SUCCESS,
FAILED,
PENDING
}
This enum represents the status of a payment transaction. It helps the system decide whether an order can move forward or should be rolled back.
User Class
public class User {
private String userId;
private String name;
public User(String userId, String name) {
this.userId = userId;
this.name = name;
}
public String getName() {
return name;
}
}
The User class holds basic user identity information. It acts as the primary actor who places orders in the system.
Cart Class
import java.util.*;
public class Cart {
private String cartId;
private List<CartItem> items = new ArrayList<>();
public Cart(String cartId) {
this.cartId = cartId;
}
public void addItem(CartItem item) {
items.add(item);
}
public void removeItem(CartItem item) {
items.remove(item);
}
public List<CartItem> getItems() {
return items;
}
}
The cart temporarily stores products selected by the user before placing an order.
The addItem() method adds a product to the cart. The removeItem() method removes unwanted items. The getItems() method is used during checkout to convert cart items into an order.
CartItem Class
public class CartItem {
private int quantity;
private Product product;
public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
}
Each CartItem represents a product along with the quantity selected by the user.
The getter methods are used during order creation and inventory validation.
Product Class
public class Product {
private String productId;
private String name;
private double price;
public Product(String productId, String name, double price) {
this.productId = productId;
this.name = name;
this.price = price;
}
public double getPrice() {
return price;
}
}
Product represents an item available in the marketplace. It contains basic details like price and name.
Seller Class
public class Seller {
private String sellerId;
private String name;
public Seller(String sellerId, String name) {
this.sellerId = sellerId;
this.name = name;
}
}
Seller represents the owner of products. A single seller can list multiple products in the system.
OrderItem Class
public class OrderItem {
private Product product;
private int quantity;
private double price;
public OrderItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
this.price = product.getPrice() * quantity;
}
public double getPrice() {
return price;
}
}
OrderItem is created during checkout. It freezes product price at that moment so later changes in product price do not affect past orders.
Inventory Class
public class Inventory {
private Product product;
private int stock;
public Inventory(Product product, int stock) {
this.product = product;
this.stock = stock;
}
public boolean isAvailable(int qty) {
return stock >= qty;
}
public void reduceStock(int qty) {
stock -= qty;
}
}
Inventory tracks stock for each product. It ensures we never sell more items than available.
The isAvailable() method checks stock before order placement. The reduceStock() method updates inventory after successful payment.
Payment Class
public class Payment {
private String paymentId;
private double amount;
private PaymentStatus status;
public Payment(String paymentId, double amount) {
this.paymentId = paymentId;
this.amount = amount;
this.status = PaymentStatus.PENDING;
}
public void markSuccess() {
this.status = PaymentStatus.SUCCESS;
}
public void markFailed() {
this.status = PaymentStatus.FAILED;
}
public PaymentStatus getStatus() {
return status;
}
}
Payment stores transaction details for an order. It starts as PENDING and later moves to SUCCESS or FAILED depending on gateway response.
PaymentService Class
public enum PaymentStatus {
SUCCESS,
FAILED,
PENDING
}
0
This service simulates payment gateway behavior. It creates a payment object and randomly decides success or failure (like a real external API response).
InventoryService Class
public enum PaymentStatus {
SUCCESS,
FAILED,
PENDING
}
1
InventoryService acts as a controller over inventory operations. It validates stock before order and updates stock after payment.
Order Class
public enum PaymentStatus {
SUCCESS,
FAILED,
PENDING
}
2
Order tracks full lifecycle of a purchase. It starts as CREATED and moves through multiple states.
The methods clearly represent real-world transitions: payment confirmation, shipping, delivery, and cancellation.
EcommerceService Class
public enum PaymentStatus {
SUCCESS,
FAILED,
PENDING
}
3
This is the core orchestration layer. It converts cart into order, validates stock, processes payment, and updates inventory.
If payment succeeds, order is marked PAID and stock is reduced. If it fails, the order is cancelled immediately.
Main Class
public enum PaymentStatus {
SUCCESS,
FAILED,
PENDING
}
4
Main method simulates full e-commerce flow from cart creation to order placement and final status update.