Problem Statement
Design a Shopping Cart system where users can browse products, add items to a cart, update quantities, and place orders. The system should ensure product stock consistency, calculate total price correctly, and generate an order after checkout.
Functional Requirements
- Users should be able to view products
- Users should be able to add/remove products from cart
- Cart should maintain quantity for each product
- System should calculate total price of cart
- Users should be able to place an order from cart
- Stock should be validated before checkout
- Cart should be cleared after successful order
Objects Required
- User
- Product
- Cart
- CartItem
- Order
- OrderItem
- ShoppingCartService
- InventoryService
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;
}
public String getName() {
return name;
}
}
The constructor initializes a product with its id, name, and price.
The getPrice() method returns the product price for cart calculations.
The getName() method is used for displaying product information to the user.
CartItem Class
public class CartItem {
private Product product;
private int quantity;
public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public double getTotalPrice() {
return product.getPrice() * quantity;
}
public void addQuantity(int qty) {
this.quantity += qty;
}
public Product getProduct() {
return product;
}
}
The constructor creates a cart item by linking a product with its quantity.
The getTotalPrice() method calculates the total cost of that item based on quantity and product price.
The addQuantity() method increases the quantity when the same product is added again instead of creating duplicates.
Cart Class
import java.util.*;
public class Cart {
private List items = new ArrayList<>();
public void addProduct(Product product, int qty) {
for (CartItem item : items) {
if (item.getProduct().getName().equals(product.getName())) {
item.addQuantity(qty);
return;
}
}
items.add(new CartItem(product, qty));
}
public void removeProduct(String productName) {
items.removeIf(item ->
item.getProduct().getName().equals(productName)
);
}
public double getTotalAmount() {
double total = 0;
for (CartItem item : items) {
total += item.getTotalPrice();
}
return total;
}
public List getItems() {
return items;
}
public void clearCart() {
items.clear();
}
}
The cart maintains a list of cart items and handles all cart-related operations.
The addProduct() method adds a product to the cart. If the product already exists, it simply updates the quantity instead of duplicating entries.
The removeProduct() method removes a product from the cart based on its name.
The getTotalAmount() method calculates the final cart value by summing up all cart items.
The clearCart() method empties the cart after a successful order placement.
Order Class
import java.util.*;
public class Order {
private String orderId;
private List items;
private double totalAmount;
public Order(String orderId, List items) {
this.orderId = orderId;
this.items = new ArrayList<>(items);
this.totalAmount = calculateTotal();
}
private double calculateTotal() {
double sum = 0;
for (CartItem item : items) {
sum += item.getTotalPrice();
}
return sum;
}
public double getTotalAmount() {
return totalAmount;
}
}
The constructor creates an order from the final cart items.
The calculateTotal() method computes the final payable amount at the time of order creation to ensure consistency.
The getTotalAmount() method returns the final locked order amount.
InventoryService Class
import java.util.*;
public class InventoryService {
private Map stock = new HashMap<>();
public boolean isAvailable(Product product, int qty) {
return stock.getOrDefault(product.getName(), 0) >= qty;
}
public void reduceStock(Product product, int qty) {
stock.put(product.getName(),
stock.getOrDefault(product.getName(), 0) - qty);
}
public void addStock(Product product, int qty) {
stock.put(product.getName(),
stock.getOrDefault(product.getName(), 0) + qty);
}
}
The inventory service manages product stock levels in the system.
The isAvailable() method checks whether enough stock exists before checkout.
The reduceStock() method updates stock after a successful order placement.
The addStock() method is used when inventory is replenished.
ShoppingCartService Class
import java.util.*;
public class ShoppingCartService {
private InventoryService inventoryService;
public ShoppingCartService(InventoryService inventoryService) {
this.inventoryService = inventoryService;
}
public Order checkout(Cart cart) {
for (CartItem item : cart.getItems()) {
if (!inventoryService.isAvailable(item.getProduct(), item.getProduct().getPrice() == 0 ? 0 : 1)) {
System.out.println("Product out of stock");
return null;
}
}
for (CartItem item : cart.getItems()) {
inventoryService.reduceStock(item.getProduct(), 1);
}
Order order = new Order(UUID.randomUUID().toString(), cart.getItems());
cart.clearCart();
return order;
}
}
The ShoppingCartService controls the checkout flow of the system.
The checkout() method first validates stock for all items. If any product is unavailable, the checkout is stopped.
After validation, it reduces inventory stock and creates a final order.
Finally, it clears the cart to complete the purchase cycle.
Main Class
public class Main {
public static void main(String[] args) {
Product p1 = new Product("P1", "Laptop", 50000);
Product p2 = new Product("P2", "Phone", 20000);
Cart cart = new Cart();
cart.addProduct(p1, 1);
cart.addProduct(p2, 2);
InventoryService inventory = new InventoryService();
inventory.addStock(p1, 10);
inventory.addStock(p2, 10);
ShoppingCartService service = new ShoppingCartService(inventory);
Order order = service.checkout(cart);
if (order != null) {
System.out.println("Order placed successfully. Total: " + order.getTotalAmount());
}
}
}
The main method simulates a full shopping flow from adding products to checkout and order creation.
Comments
Post a Comment