Inventory management

mail@pastecode.io avatar
unknown
java
a month ago
3.7 kB
1
Indexable
Never
import java.util.HashMap;
import java.util.Map;

// InventoryItem Interface
interface InventoryItem {
    String getId();
    String getName();
    int getQuantity();
    double getPrice();
    void setQuantity(int quantity);
}

// Order Interface
interface Order {
    String getOrderId();
    void addItem(InventoryItem item, int quantity);
    double getTotalPrice();
}

// InventoryItem Implementation
class InventoryItemImpl implements InventoryItem {
    private String id;
    private String name;
    private int quantity;
    private double price;

    public InventoryItemImpl(String id, String name, int quantity, double price) {
        this.id = id;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getQuantity() {
        return quantity;
    }

    @Override
    public double getPrice() {
        return price;
    }

    @Override
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

// Order Implementation
class OrderImpl implements Order {
    private String orderId;
    private Map<InventoryItem, Integer> items = new HashMap<>();

    public OrderImpl(String orderId) {
        this.orderId = orderId;
    }

    @Override
    public String getOrderId() {
        return orderId;
    }

    @Override
    public void addItem(InventoryItem item, int quantity) {
        if (item.getQuantity() < quantity) {
            throw new IllegalArgumentException("Not enough stock available.");
        }
        items.put(item, quantity);
        item.setQuantity(item.getQuantity() - quantity);
    }

    @Override
    public double getTotalPrice() {
        return items.entrySet().stream()
            .mapToDouble(entry -> entry.getKey().getPrice() * entry.getValue())
            .sum();
    }
}

// InventoryManager Class
class InventoryManager {
    private Map<String, InventoryItem> inventory = new HashMap<>();

    public void addInventoryItem(InventoryItem item) {
        inventory.put(item.getId(), item);
    }

    public InventoryItem getItem(String id) {
        return inventory.get(id);
    }

    public void updateInventory(String id, int quantity) {
        InventoryItem item = inventory.get(id);
        if (item != null) {
            item.setQuantity(quantity);
        } else {
            throw new IllegalArgumentException("Item not found.");
        }
    }

    public void printInventory() {
        inventory.values().forEach(item -> 
            System.out.println("ID: " + item.getId() + ", Name: " + item.getName() +
            ", Quantity: " + item.getQuantity() + ", Price: $" + item.getPrice()));
    }
}

// Main Class
public class Main {
    public static void main(String[] args) {
        InventoryManager inventoryManager = new InventoryManager();

        InventoryItem item1 = new InventoryItemImpl("1", "Widget", 100, 19.99);
        InventoryItem item2 = new InventoryItemImpl("2", "Gadget", 50, 29.99);

        inventoryManager.addInventoryItem(item1);
        inventoryManager.addInventoryItem(item2);

        System.out.println("Initial Inventory:");
        inventoryManager.printInventory();

        Order order = new OrderImpl("123");
        order.addItem(item1, 2);
        order.addItem(item2, 1);

        System.out.println("\nOrder Details:");
        System.out.println("Order ID: " + order.getOrderId());
        System.out.println("Total Price: $" + order.getTotalPrice());

        System.out.println("\nUpdated Inventory:");
        inventoryManager.printInventory();
    }
}
Leave a Comment