Created
November 7, 2020 20:56
-
-
Save skywall/14c7651e9c7bab09921c474f1bee0a74 to your computer and use it in GitHub Desktop.
TicketMachine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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