This repository contains a Low Level Design (LLD) implementation of a Coffee Vending Machine in Java.
It supports:
- Displaying a menu of available coffees
- Multiple coffee types (Espresso, Cappuccino, Latte)
- Each coffee having a price and an ingredient recipe
- Adding extras (milk / sugar / chocolate syrup) using the Decorator pattern
- Inventory management for ingredient availability
- Payment flow with multiple money insertions
- A vending-machine State Machine (Ready → Selected → Paid)
- Implemented in:
src/CoffeeVendingMachine/CoffeeMachine.java - Method:
displayMenu() - It iterates over
CofeeTypesand prints the base coffee price.
- Implemented in:
src/CoffeeVendingMachine/Coffee/Espresso.javasrc/CoffeeVendingMachine/Coffee/Cappuccino.javasrc/CoffeeVendingMachine/Coffee/Latte.java
- Creation via factory:
src/CoffeeVendingMachine/Factory/CoffeeFactory.java
- Interface:
src/CoffeeVendingMachine/Coffee/Coffee.java
- Contract:
Integer getPrice()Map<Ingredients, Integer> getRecipe()
Recipes are represented as a Map<Ingredients, Integer>.
In the current code, these integers are used as ratio/percentage-like weights.
- Cappuccino and Latte recipes sum to 100.
- Espresso currently sums to 80 (this can be adjusted to 100 if you want strict “must sum to 100” percentage enforcement).
Ingredient enum:
src/CoffeeVendingMachine/Enums/Ingredients.java
Extras are implemented using the Decorator Pattern.
- Base decorator:
src/CoffeeVendingMachine/Decorators/BaseDecorator.java
- Extras:
MilkDecoratorSugarDecoratorChocolateSyrupDecorator
Each decorator updates:
getDescription()(adds extra text)getPrice()(adds extra cost)getRecipe()(adds extra ingredient requirement)
Example:
Coffee cappuccino = CoffeeFactory.getCoffee(CofeeTypes.CAPPUCCINO);
Coffee cappuccinoWithChoco = new ChocolateSyrupDecorator(cappuccino);- Implemented in:
src/CoffeeVendingMachine/Inventory.java
Key methods:
addIngredient(ingredient, amount)hasIngredients(recipe)deductIngredient(recipe)
If the recipe cannot be fulfilled, the machine throws:
src/CoffeeVendingMachine/Exceptions/InsufficientIngredientsException.java
The machine supports inserting money multiple times until the total meets/exceeds the coffee price.
- State implementation:
src/CoffeeVendingMachine/State/SelectedState.java
Important behavior:
insertMoney(amount)accumulates money:machine.setInsertedMoney(machine.getInsertedMoney() + amount);
- Only when the total inserted money ≥ price does it move to
PaidState.
State pattern is implemented in:
src/CoffeeVendingMachine/State/VendingMachineState.java
Concrete states:
ReadyState→ allows selecting coffee (also checks inventory)SelectedState→ accepts money insertion(s)PaidState→ dispenses and deducts inventory, returns change if any
State transitions happen inside CoffeeMachine through:
selectCoffee(...)insertMoney(...)dispense()cancel()
From the repository root:
javac $(find src -name '*.java')
java -cp src CoffeeVendingMachine.CoffeeVendingMachineDemoDemo entry point:
src/CoffeeVendingMachine/CoffeeVendingMachineDemo.java
src/CoffeeVendingMachine/
CoffeeMachine.java # Main machine (singleton) + menu
Inventory.java # Ingredient stock management
Coffee/ # Coffee types + Coffee interface
Decorators/ # Add-ons using Decorator pattern
Factory/ # CoffeeFactory
State/ # State pattern (Ready/Selected/Paid)
Enums/ # CofeeTypes, Ingredients
Exceptions/ # InsufficientIngredientsException
- Strict percentage enforcement: add recipe validation so every coffee recipe must sum to 100.
- Menu improvements: print recipes/ingredients in the menu output.