The system will consist of the following classes:
package com.lld; import java.util.HashMap; import java.util.Map; class Coin { private int value; public Coin(int value) { this.value = value; } public int getValue() { return value; } }
class Note { private int value; public Note(int value) { this.value = value; } public int getValue() { return value; } }
class Product { private String name; private int price; private int quantity; public Product(String name, int price, int quantity) { this.name = name; this.price = price; this.quantity = quantity; } public String getName() { return name; } public int getPrice() { return price; } public int getQuantity() { return quantity; } public void decreaseQuantity() { if (quantity > 0) { quantity--; } } public void increaseQuantity(int amount) { this.quantity += amount; } public boolean isAvailable() { return quantity > 0; } }
class CashManager { private int balance = 0; public void insertCoin(Coin coin) { balance += coin.getValue(); } public void insertNote(Note note) { balance += note.getValue(); } public int getBalance() { return balance; } public void deductAmount(int amount) { balance -= amount; } public int returnChange(int price) { int change = balance - price; deductAmount(price); return change; } }
class InventoryManager { private Map<String, Product> productMap = new HashMap<>(); public void addProduct(Product product) { productMap.put(product.getName(), product); } public Product getProduct(String productName) { return productMap.get(productName); } public boolean isProductAvailable(String productName) { return productMap.containsKey(productName) && productMap.get(productName).isAvailable(); } public void restockProduct(String productName, int quantity) { Product product = productMap.get(productName); if (product != null) { product.increaseQuantity(quantity); // Fix: Increase quantity instead of decreasing } else { System.out.println("Product not found in inventory."); } } }
class VendingMachine { private CashManager cashManager; private InventoryManager inventoryManager; public VendingMachine() { this.cashManager = new CashManager(); this.inventoryManager = new InventoryManager(); } public void addProduct(Product product) { inventoryManager.addProduct(product); } public void insertCoin(Coin coin) { cashManager.insertCoin(coin); } public void insertNote(Note note) { cashManager.insertNote(note); } public void selectProduct(String productName) { if (inventoryManager.isProductAvailable(productName)) { Product product = inventoryManager.getProduct(productName); if (cashManager.getBalance() >= product.getPrice()) { int change = cashManager.returnChange(product.getPrice()); product.decreaseQuantity(); System.out.println("Dispensing: " + productName); System.out.println("Returning change: " + change + " cents."); } else { System.out.println("Insufficient funds. Please insert more money."); } } else { System.out.println("Product is out of stock."); } } public void restockProduct(String productName, int quantity) { inventoryManager.restockProduct(productName, quantity); } public void collectMoney() { System.out.println("Collecting money: " + cashManager.getBalance() + " cents."); } }
public class VendingMachineApp { public static void main(String[] args) { // Creating product objects Product soda = new Product("Soda", 50, 10); Product chips = new Product("Chips", 30, 5); Product candy = new Product("Candy", 20, 7); // Creating VendingMachine instance VendingMachine vendingMachine = new VendingMachine(); vendingMachine.addProduct(soda); vendingMachine.addProduct(chips); vendingMachine.addProduct(candy); // Insert coins and select products vendingMachine.insertCoin(new Coin(25)); vendingMachine.insertCoin(new Coin(25)); vendingMachine.selectProduct("Soda"); vendingMachine.insertCoin(new Coin(25)); vendingMachine.insertCoin(new Coin(10)); vendingMachine.selectProduct("Chips"); // Restocking vendingMachine.restockProduct("Chips", 5); // Collecting money vendingMachine.collectMoney(); } }
Output: Dispensing: Soda Returning change: 0 cents. Dispensing: Chips Returning change: 5 cents. Collecting money: 5 cents.