Try Our Course for 1 day @29

Try our course for 1 day and if you like it, you can go for one year or lifetime access!

Design (LLD) Inventory Management System  - Machine Coding

Design (LLD) Inventory Management System - Machine Coding

Table of contents

Low-level design (LLD) for an inventory management system:

  1. Product catalog: The system will need to store information about the products in the inventory, such as the product name, SKU, description, and current quantity. This will likely involve creating a database or data structure to store this information.

  2. Order processing: The system will need to allow users to place orders for products and update the inventory accordingly. This will involve creating forms and handling the associated server-side logic to process the orders and update the inventory.

  3. Stock management: The system will need to track the current stock levels and alert users when the stock is running low or has run out. This will involve implementing logic to monitor the inventory and generate alerts when necessary.

  4. Reporting and analytics: The system will need to provide reports and analytics on the inventory, such as sales data and product popularity. This will involve implementing functions to query and analyze the data, as well as creating views to display the results.

  5. User accounts and authentication: The system will need to allow users to create accounts, log in, and log out. We will also need to implement measures to ensure the security of user accounts, such as password hashing and potentially two-factor authentication.

  6. User interface: Finally, the system will need to design the user interface for the service, including the layout, navigation, and any necessary graphics or styling. This will involve creating wireframes and mockups, as well as implementing the front-end code to bring the design to life.

Code

import java.time.LocalDateTime;
import java.util.*;

// Enum representing Order Status
public enum OrderStatus {
    PENDING,
    PROCESSING,
    SHIPPED,
    DELIVERED,
    CANCELLED
}

// Abstract Factory to create Products and Orders
interface InventoryFactory {
    Product createProduct(String name, String description, double price, int quantity);
    Order createOrder(Customer customer, Map<Product, Integer> products);
}

// Concrete Factory for Inventory Management
public class ConcreteInventoryFactory implements InventoryFactory {
    @Override
    public Product createProduct(String name, String description, double price, int quantity) {
        return new Product(name, description, price, quantity);
    }

    @Override
    public Order createOrder(Customer customer, Map<Product, Integer> products) {
        return new Order(customer, products);
    }
}

// Product Class representing inventory items
public class Product {
    private long id;
    private String name;
    private String description;
    private double price;
    private int quantity;

    // Observers for product (e.g., low inventory notification)
    private List<Observer> observers = new ArrayList<>();

    public Product(String name, String description, double price, int quantity) {
        this.id = IdGenerator.generateId();
        this.name = name;
        this.description = description;
        this.price = price;
        this.quantity = quantity;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public double getPrice() {
        return price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
        notifyObservers();  // Notify when quantity changes (e.g., low stock alert)
    }

    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    private void notifyObservers() {
        for (Observer observer : observers) {
            observer.update("Product stock updated for " + name);
        }
    }
}

// Order Class representing customer orders
public class Order {
    private long id;
    private Customer customer;
    private Map<Product, Integer> products;
    private LocalDateTime createdAt;
    private OrderStatus status;
    private List<Observer> observers = new ArrayList<>();

    public Order(Customer customer, Map<Product, Integer> products) {
        this.id = IdGenerator.generateId();
        this.customer = customer;
        this.products = products;
        this.createdAt = LocalDateTime.now();
        this.status = OrderStatus.PENDING;
    }

    public long getId() {
        return id;
    }

    public Customer getCustomer() {
        return customer;
    }

    public Map<Product, Integer> getProducts() {
        return products;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
        notifyObservers();  // Notify observers when status changes
    }

    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    private void notifyObservers() {
        for (Observer observer : observers) {
            observer.update("Order " + id + " status changed to " + status);
        }
    }
}

// Customer Class representing customers
public class Customer {
    private long id;
    private String name;
    private String email;
    private String phone;
    private String address;

    public Customer(String name, String email, String phone, String address) {
        this.id = IdGenerator.generateId();
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.address = address;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }
}

// Interface for Observer (e.g., Notifiers for product or order changes)
interface Observer {
    void update(String message);
}

// Command Pattern for handling various operations
interface Command {
    void execute();
}

// Command for adding a Product
class AddProductCommand implements Command {
    private InventoryManagementSystem system;
    private Product product;

    public AddProductCommand(InventoryManagementSystem system, Product product) {
        this.system = system;
        this.product = product;
    }

    @Override
    public void execute() {
        system.addProduct(product);
    }
}

// Command for placing an Order
class PlaceOrderCommand implements Command {
    private InventoryManagementSystem system;
    private Order order;

    public PlaceOrderCommand(InventoryManagementSystem system, Order order) {
        this.system = system;
        this.order = order;
    }

    @Override
    public void execute() {
        system.placeOrder(order);
    }
}

// Strategy Pattern for pricing and discount strategies
interface PricingStrategy {
    double calculatePrice(Product product, int quantity);
}

// Regular pricing strategy
class RegularPricingStrategy implements PricingStrategy {
    @Override
    public double calculatePrice(Product product, int quantity) {
        return product.getPrice() * quantity;
    }
}

// Discounted pricing strategy
class DiscountPricingStrategy implements PricingStrategy {
    private double discountRate;

    public DiscountPricingStrategy(double discountRate) {
        this.discountRate = discountRate;
    }

    @Override
    public double calculatePrice(Product product, int quantity) {
        return (product.getPrice() * quantity) * (1 - discountRate);
    }
}

// InventoryManagementSystem with product, customer, and order management
public class InventoryManagementSystem {
    private Map<Long, Product> products = new HashMap<>();
    private Map<Long, Order> orders = new HashMap<>();
    private Map<Long, Customer> customers = new HashMap<>();

    // Add product to the system
    public void addProduct(Product product) {
        products.put(product.getId(), product);
        System.out.println("Product added: " + product.getName());
    }

    // Get product by ID
    public Product getProduct(long id) {
        return products.get(id);
    }

    // Update product quantity
    public void updateProductQuantity(long productId, int quantity) {
        Product product = products.get(productId);
        if (product != null) {
            product.setQuantity(quantity);
            System.out.println("Product quantity updated for: " + product.getName());
        }
    }

    // Place order in the system
    public void placeOrder(Order order) {
        orders.put(order.getId(), order);
        System.out.println("Order placed: " + order.getId());
    }

    // Get order by ID
    public Order getOrder(long id) {
        return orders.get(id);
    }

    // Update order status
    public void updateOrderStatus(long orderId, OrderStatus status) {
        Order order = orders.get(orderId);
        if (order != null) {
            order.setStatus(status);
        }
    }

    // Add customer to the system
    public void addCustomer(Customer customer) {
        customers.put(customer.getId(), customer);
        System.out.println("Customer added: " + customer.getName());
    }

    // Get customer by ID
    public Customer getCustomer(long id) {
        return customers.get(id);
    }
}

// ID Generator class for unique ID creation
class IdGenerator {
    private static long currentId = 0;

    public static long generateId() {
        return ++currentId;
    }
}

// Main class to demonstrate the Inventory Management System
public class Main {
    public static void main(String[] args) {
        // Create the factory
        InventoryFactory factory = new ConcreteInventoryFactory();

        // Create products
        Product product1 = factory.createProduct("Laptop", "High-performance laptop", 1200.0, 10);
        Product product2 = factory.createProduct("Smartphone", "Latest smartphone", 800.0, 20);

        // Create customers
        Customer customer1 = new Customer("Alice", "alice@example.com", "1234567890", "123 Main St");
        Customer customer2 = new Customer("Bob", "bob@example.com", "0987654321", "456 Elm St");

        // Create the inventory system
        InventoryManagementSystem inventorySystem = new InventoryManagementSystem();

        // Add customers to the system
        inventorySystem.addCustomer(customer1);
        inventorySystem.addCustomer(customer2);

        // Add products using command pattern
        Command addProduct1 = new AddProductCommand(inventorySystem, product1);
        Command addProduct2 = new AddProductCommand(inventorySystem, product2);
        addProduct1.execute();
        addProduct2.execute();

        // Create and place an order
        Map<Product, Integer> orderProducts = new HashMap<>();
        orderProducts.put(product1, 2);
        orderProducts.put(product2, 1);

        Order order1 = factory.createOrder(customer1, orderProducts);

        Command placeOrder = new PlaceOrderCommand(inventorySystem, order1);
        placeOrder.execute();

        // Update order status
        inventorySystem.updateOrderStatus(order1.getId(), OrderStatus.PROCESSING);
    }
}