AlgoMaster Logo

Design Vending Machine

Ashish

Ashish Pratap Singh

A vending machine is a self-service automated device that dispenses items like snacks, beverages, or other products to users without the need for human assistance. These machines are commonly found in places like offices, schools, airports, and train stations.

Vending Machine Class Diagram

Users typically interact with the machine by:

  1. Selecting an item (using a code, button, or touchscreen),
  2. Inserting payment (coins, bills, cards, or digital wallets),
  3. And then receiving the item from a dispensing slot.

In this chapter, we will explore the low level design of a vending machine in detail.

Lets start by clarifying the requirements:

1. Clarifying Requirements

Before starting the design, it's important to ask thoughtful questions to uncover hidden assumptions and better define the scope of the system.

Here is an example of how a discussion between the candidate and the interviewer might unfold:

After gathering the details, we can summarize the key system requirements.

1.1 Functional Requirements

  • Accept coin-based payments using fixed denominations (e.g., $1, $5, $10)
  • Allow adding new items or restocking existing items in the vending machine
  • Allow users to view available items along with their price
  • Users can select an item by entering the item code
  • The machine should dispense the selected item if sufficient money is inserted
  • Return change if the inserted amount exceeds the item’s price
  • Allow users to cancel a transaction before item dispensing and receive a full refund
  • Display intermediate states such as inserted amount, selected item, or refund messages

1.2 Non-Functional Requirements

  • Maintainability: The system should follow object-oriented principles, ensuring modularity, testability, and ease of extension
  • Atomicity: The purchase operation must be atomic. A user either receives the item and correct change, or receives a full refund
  • Concurrency Control: The machine must handle only one transaction at a time. While a transaction is in progress, the system should remain locked to other inputs
  • Extensibility: The system should be designed in a way that future features (e.g., digital payments) can be added with minimal changes

After the requirements are clear, lets identify the core entities/objects we will have in our system.

2. Identifying Core Entities

Premium Content

This content is for premium members only.