Skip to content

Instantly share code, notes, and snippets.

@skywall
Created November 7, 2020 20:56
Show Gist options
  • Save skywall/14c7651e9c7bab09921c474f1bee0a74 to your computer and use it in GitHub Desktop.
Save skywall/14c7651e9c7bab09921c474f1bee0a74 to your computer and use it in GitHub Desktop.
TicketMachine
class TicketMachine @Inject constructor(
private val printer: Printer,
private val display: Display,
private val coinCounter: CoinCounter,
) {
private var selectedTicketType: TicketType? = null
init {
display.show("Select ticket:")
}
fun selectTicket(ticketType: TicketType) {
selectedTicketType = ticketType
val missingCoins = coinCounter.getMissingCoins(ticketType)
display.show("Ticket selected: ${ticketType.name}. Insert coins: $missingCoins")
}
fun insertCoin() {
val selectedTicketType = this.selectedTicketType
if (selectedTicketType == null) {
display.show("Returning coin. Select ticket first!")
return
}
coinCounter.insertCoin()
val missingCoins = coinCounter.getMissingCoins(selectedTicketType)
when (missingCoins) {
0 -> {
printer.printTicket(selectedTicketType)
reset()
}
else -> {
display.show("Insert coins: $missingCoins")
}
}
}
fun abort() {
display.show("Abort. Returning coins: ${coinCounter.getInsertedCoins()}")
reset()
}
private fun reset() {
selectedTicketType = null
coinCounter.reset()
display.show("Select ticket:")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment