Menu
Coddy logo textTech

Recap - Ticket Office

Part of the Object Oriented Programming section of Coddy's Kotlin journey. Lesson 16 of 57.

challenge icon

Challenge

Medium

Build a small ticket office from three kinds of declaration.

  • Ticket has a private constructor taking number, show and seat. Its companion holds const val SEATS = 3, a private counter and the factory issue(show, seat), which gives tickets the numbers 1, 2, 3, ...; issued is the number of tickets issued so far. label() returns #1 Hamlet seat 2.
  • BoxOffice is an object. sell(show, seat) replies no seat 4 when the seat is outside 1 to Ticket.SEATS, seat 2 taken when it is sold for that show, and otherwise issues a ticket and replies with its label. refund(number) frees the seat of a sold ticket and replies refunded #1, or no ticket #9. report() replies Hamlet 2/3, Cats 1/3 with the shows in alphabetical order, or nothing sold.

The supplied code reads commands sell Hamlet 2, refund 1 or report, prints each reply and finally tickets issued: with the count. Numbers are never reused after a refund.

Your code goes in Ticket.kt and BoxOffice.kt. Main.kt holds the supplied input/output code and cannot be edited.

Try it yourself

fun main() {
    // Supplied input/output code: keep it as it is
    val input = generateSequence(::readLine).toList()
    for (cmd in input) {
        val p = cmd.split(" ")
        when (p[0]) {
            "sell" -> println(BoxOffice.sell(p[1], p[2].toInt()))
            "refund" -> println(BoxOffice.refund(p[1].toInt()))
            else -> println(BoxOffice.report())
        }
    }
    println("tickets issued: ${Ticket.issued}")
}

All lessons in Object Oriented Programming

Practice on your own: Kotlin playground