Vending Machine

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-14

Problem Statement

Design the Low-Level Design (LLD) of a Vending Machine system that dispenses products like snacks, beverages, and chocolates when a user inserts money and selects an item.


The design should model the core classes, interactions, and states of the vending machine — focusing on object-oriented design principles such as encapsulation, abstraction, and state

management.


🧠 Functional Requirements


Display Products


•The machine should display available items and their prices.


Accept Coins / Notes

•The user can insert coins or notes (e.g., ₹10, ₹20, ₹50, ₹100).
•The machine should keep track of the total inserted amount.

Select Product

•The user selects a product using a button or keypad.
•The machine checks if the selected product:
•Exists
•Is in stock
•Has sufficient balance inserted for purchase

Dispense Product

•If the user has inserted enough money, the machine dispenses the product.

Return Change

•If the inserted money exceeds the product price, return the remaining balance as change.

Cancel Transaction

•The user can cancel before selection and get a refund.

Refill and Maintenance

•Admins can:
•Refill products
•Refill change
•Check inventory
•Power on/off the machine


🚫 Non-Functional Requirements


•Ensure accurate balance management and change return.
•Design should support easy state transitions (e.g., Idle → AcceptingMoney → Dispensing).
•Extensible to support new payment types (e.g., card, wallet).
•Thread-safe for concurrent access in multi-user vending machines.


🧱 Suggested Classes


ClassResponsibility


VendingMachine

Central class managing the current state, product inventory, and transaction flow.


State (Interface)

Defines actions (insertCoin, selectProduct, dispenseProduct, refund, etc.).


IdleState, HasMoneyState, DispenseState, SoldOutState

Concrete implementations of different machine states.


Product

Represents an item with name, price, and quantity.


Inventory

Manages available products and their stock.


Coin / Note (Enum)

Represents denominations supported by the machine.


Transaction

Maintains information about the current transaction (amount inserted, selected item, etc.).


ChangeDispenser

Calculates and returns appropriate change.


Admin

Responsible for refilling and maintaining the machine.


🔄 Example Flow


1.Idle State
1 2Machine displays: 3[A1: Coke ₹50] [A2: Chips ₹30] [A3: Candy ₹10]
1.User Inserts Money
1 2Insert ₹50 3→ State changes from Idle → HasMoney
1.User Selects Product
1 2Select A2 (Chips ₹30) 3→ Machine dispenses Chips 4→ Returns ₹20 change 5→ State → Idle
1.Cancel Transaction
1 2User inserts ₹100 3Cancels transaction → Refund ₹100


🧪 Example Input / Output


Input:

1 2Insert ₹100 3Select Product: A1 (Coke ₹50)


Output:

1 2Dispensing Coke... 3Returning ₹50 change. 4Thank you!


Input:

1 2Insert ₹10 3Select Product: A1 (Coke ₹50)


Output:

1 2Insufficient balance. Please insert ₹40 more.


⚙️ Constraints


•Number of slots ≤ 50
•Each product quantity ≤ 100
•Supports denominations: ₹10, ₹20, ₹50, ₹100
•Return change if available, otherwise display “Change not available.”


🎯 Goals


Your design should:


•Use the State Design Pattern for machine states (Idle, HasMoney, Dispensing, SoldOut).
•Ensure extensibility for new payment methods or product types.
•Cleanly separate core machine logic, inventory management, and UI interaction.
•Handle edge cases — insufficient funds, product sold out, no change.


✅ In short:

Design a modular, state-driven Vending Machine system that can accept money, dispense products, handle change, and allow for easy maintenance and refilling — demonstrating OOP and state management principles.

Solution

Comments