Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coffee Vending Machine (LLD - Java)

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)

Requirements Coverage

1) Menu to display available items

  • Implemented in: src/CoffeeVendingMachine/CoffeeMachine.java
  • Method: displayMenu()
  • It iterates over CofeeTypes and prints the base coffee price.

2) Different types of coffees (Espresso, Cappuccino, Latte)

  • Implemented in:
    • src/CoffeeVendingMachine/Coffee/Espresso.java
    • src/CoffeeVendingMachine/Coffee/Cappuccino.java
    • src/CoffeeVendingMachine/Coffee/Latte.java
  • Creation via factory:
    • src/CoffeeVendingMachine/Factory/CoffeeFactory.java

3) Each coffee has price and ingredients

  • Interface:
    • src/CoffeeVendingMachine/Coffee/Coffee.java
  • Contract:
    • Integer getPrice()
    • Map<Ingredients, Integer> getRecipe()

4) Each coffee has a specific ingredient percentage

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

5) Decorations (extras) should be able to add to existing coffee

Extras are implemented using the Decorator Pattern.

  • Base decorator:
    • src/CoffeeVendingMachine/Decorators/BaseDecorator.java
  • Extras:
    • MilkDecorator
    • SugarDecorator
    • ChocolateSyrupDecorator

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);

6) Inventory management (ingredient availability tracking)

  • 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

7) Payment support (multiple money insertions)

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.

8) CoffeeVendingMachine states (ready, select, payment)

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()

How to Run

From the repository root:

javac $(find src -name '*.java')
java -cp src CoffeeVendingMachine.CoffeeVendingMachineDemo

Demo entry point:

  • src/CoffeeVendingMachine/CoffeeVendingMachineDemo.java

Project Structure (High Level)

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

Notes / Possible Enhancements

  • Strict percentage enforcement: add recipe validation so every coffee recipe must sum to 100.
  • Menu improvements: print recipes/ingredients in the menu output.

About

A Java Low-Level Design implementation of a Coffee Vending Machine using Factory, Decorator, and State design patterns. Supports multiple coffee types, add-ons, ingredient inventory tracking, and multi-step payments with ready/select/paid states.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages