Broadly, think about what objects are involved in a vending machine:

  • VendingMachine - possibly an abstract class
  • DrinkMachine, SnackMachine, and classes extending VendingMachine
  • VendingProduct - an abstract class?
  • Drink, other classes extending VendingProduct
  • Coke, other classes extending Drink
  • &c, &c.

But I’m sure you can figure that out pretty easily. The nuts and bolts of the machine will take place in some kind of utility class, with methods to accept bills and coins, calculate change, etc.

Also, Getting the coffee will be got from the factory:

public class CoffeeVendingMachine extends VendingMachine {

	private Milk milk;

	private Sugar sugar;

	public Coffee getCoffee(CoffeeTypeEnum coffeeType) {
		prepareCoffee(coffeeType);
	}

	private void prepareCoffee(CoffeeTypeEnum coffeeType {
			//internal process for coffee making
		}
	}
	// ...
}

Note that prepareCoffee is private method, as users don’t want to know how coffee is made.

Class Diagram

Here is basic class diagram for vending machine:

 classDiagram
       class VendingMachine {
           -Controller controller
           -SelectionPanel selectionPanel
           -CoinCollector coinCollector
           -CoinDispenser coinDispenser
           -ProductDispenser productDispenser
           +startTransaction()
           +selectProduct(productId)
           +insertCoin(coinValue)
           +dispenseProduct()
           +dispenseChange()
           +cancelTransaction()
       }
       
       class Controller {
           -int currentBalance
           -Product selectedProduct
           +processSelection(productId)
           +processCoinInsertion(coinValue)
           +handleDispenseProduct()
           +handleDispenseChange()
           +handleCancelTransaction()
       }
       
       class SelectionPanel {
           -List~Product~ products
           +displayProducts()
           +chooseProduct(productId)
       }
    
       class CoinCollector {
           -int collectedAmount
           +collectCoin(coinValue)
           +returnCoins()
           +getCollectedAmount()
       }
       
       class CoinDispenser {
           +dispenseChange(changeAmount)
       }
       
       class Product {
           -String name
           -float price
           +getName()
           +getPrice()
       }
       
       class ProductDispenser {
           +dispenseProduct(productId)
       }
       
       VendingMachine "1" *-- "1" Controller : controls
       VendingMachine "1" *-- "1" SelectionPanel : uses
       VendingMachine "1" *-- "1" CoinCollector : uses
       VendingMachine "1" *-- "1" CoinDispenser : uses
       VendingMachine "1" *-- "1" ProductDispenser : uses
       VendingMachine "1" o-- "*" Product : offers
       SelectionPanel "1" o-- "*" Product : displays
       Controller "1" --> "*" Product : selects
  

References