Setting up a Vending Machine with Swift
The vending machine should have the following features:
- Display the available items and their prices.
- Accept coins and bills as payment.
- Dispense the selected item.
- Return change if the amount inserted is greater than the item price.
- Refund the money if the transaction is canceled.
We will use the following classes and data structures to design the vending machine:
The Item class represents an item that is available in the vending machine. It has the following properties:
name: the name of the item.price: the price of the item.
The VendingMachine class represents the vending machine. It has the following properties:
items: an array of Item objects representing the available items.balance: the current balance of the machine.selectedItem: the currently selected item.coinTypes: an array of coin types accepted by the machine.billTypes: an array of bill types accepted by the machine.
It has the following methods:
displayItems(): displays the available items and their prices.insertCoin(_ coin: Coin): inserts a coin into the machine.insertBill(_ bill: Bill): inserts a bill into the machine.selectItem(_ index: Int): selects an item from the available items.dispenseItem(): dispenses the selected item.refund(): refunds the money inserted into the machine.
The Coin and Bill enums represent the types of coins and bills accepted by the vending machine. They have the following cases:
.penny: a penny..nickel: a nickel..dime: a dime..quarter: a quarter.
.one: a one dollar bill..five: a five dollar bill..ten: a ten dollar bill..twenty: a twenty dollar bill.