My Solution for Design a Vending Machine with Score: 8/10

by alchemy_zephyr540

Requirements

  • The Machine needs to present a list of what items is selling and the price
  • When a customer come in, he/she can pick an item
    • if still have item in stock, he needs to pay: coin, paper cash or card
      • if customer pay cash, the machine also needs to give correct change
    • if no in stock, display an message
  • Also need an internal management system:
    • to keep track if an inventory like daily, and send out alert if stock is low
    • keep the cash storage, if it is full or to low cannot give out change also need to alert the management team





Core Objects & Relationships


VendingMachine:

Inventory

Payments

Transaction

ManagementService

CashStorage



Inventory

Map inventory

addItem(item, count)

checkStock(String id)

removeItem(id)

addItem(id, count)


Payments:

Use Strategy pattern, create a PaymentStrategy Interface:

  • CashPaymentStrategy
  • CoinPaymentStrategy
  • CardPaymentStrategy

Use a factory class to create encapsulate the creation of the correct PaymentStrategy 

Also need a Transaction Class to book keeping

  • item,
  • time
  • payment type
  • total cash get
  • change give out

ManagementService

  • check inventory, if the stock is less then MIN_ITEM_COUNT, send out an alert code ITEM_LOW_STOCK
  • check cash storage, if cash full send out CASH_FULL alert
  • check the coin storage, if coin count less than MIN_COINT_COUNT snedout an LOW_COIN alert



APIs & Class Members (write code)

public class VendingMachine{ private Inventory inventory; private PaymentProcessor paymentProcessor; private ManagementService managementService; private TransactionCollector transaction; private CashStorage cashStorage; public boolean vendItem(String itemId, payType, amount); } public class Inventory { // singleten private List<String, Item> items; private List<String, Integer> stock; Inventory getInstance(); void addNewItem(Item item, int stock); boolean refill(String code, int stock); int getStock(String code) int sellItem(String item) } public class Item{ String code; String name; double price; } public PaymentProcessor { PaymentStrategy paymentStrategy; boolean pay(double amount); } public class CashPaymentStrategy implements PaymentProcessor public class CoinPaymentStrategy implements PaymentProcessor public class CardPaymentStrategy implements PaymentProcessor public class PaymentFactory{ PaymentFactory createPaymentStrategy(PaymentType); } public enum PaymentType{ CASH, COIN, CARD } public class TransactionCollector { List<Transaction> transactions; addTransaction(Transaction); getAll(); } public class Transaction{ Datetime createdAt; String ItemCode; String ItemName; String paymentType; String amountPaid; } public class Alert{ String message; Type ErroType; Datetime createdAt; }


Tradeoffs, SOLID & Scalability







Supports markdown