Menu
Coddy logo textTech

Event Scheduler

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

challenge icon

Challenge

Hard

Build an event scheduler.

  • Event(title, day, start, end) (hours from 0 to 24) is a class that implements Comparable<Event>, ordered by day, then start, then title. Two events are equal when their title and day match (the same event entered twice), so override equals and hashCode on those two properties. It prints as day 3 09-11 standup, with two-digit hours. overlaps(other) is true on the same day when each starts before the other ends.
  • Scheduler.add(event) replies invalid: standup when the event does not end after it starts, duplicate for an equal event, conflicts with review for the first overlapping event in agenda order, or added.
  • agenda() returns the events sorted, and busiestDay() returns busiest day 3: 5h for the day with the most booked hours (the earlier day on a tie), or no events.

The supplied code reads events as title,day,start,end lines, prints the reply for each, then the agenda (one event per line) and the busiest day.

Your code goes in Event.kt and Scheduler.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()
    val scheduler = Scheduler()
    for (line in input) {
        val p = line.split(",")
        println(scheduler.add(Event(p[0], p[1].toInt(), p[2].toInt(), p[3].toInt())))
    }
    for (e in scheduler.agenda()) println(e)
    println(scheduler.busiestDay())
}

All lessons in Object Oriented Programming

Practice on your own: Kotlin playground