Event Scheduler
Part of the Object Oriented Programming section of Coddy's Kotlin journey. Lesson 56 of 57.
Challenge
HardBuild an event scheduler.
Event(title, day, start, end)(hours from 0 to 24) is a class that implementsComparable<Event>, ordered by day, then start, then title. Two events are equal when their title and day match (the same event entered twice), so overrideequalsandhashCodeon those two properties. It prints asday 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)repliesinvalid: standupwhen the event does not end after it starts,duplicatefor an equal event,conflicts with reviewfor the first overlapping event in agenda order, oradded.agenda()returns the events sorted, andbusiestDay()returnsbusiest day 3: 5hfor the day with the most booked hours (the earlier day on a tie), orno 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
1Classes and Objects
Working With FilesDefining ClassesPrimary Constructorsinit Blocks and ValidationSecondary ConstructorsRecap - Library Card4Inheritance
Open Classes and SubclassesOverriding Memberssuper and ConstructorsAbstract ClassesRecap - Staff Directory2Properties and Encapsulation
Custom AccessorsVisibility ModifiersPrivate Setterslazy and lateinitRecap - Bank Account5Interfaces
Declaring InterfacesDefault MethodsMultiple InterfacesInterfaces as TypesRecap - Payment Methods8Generics
Generic FunctionsGeneric ClassesType ConstraintsVariance With in and outRecap - Priority Queue11Project: Library Management
Books and MembersBorrowing BooksPractice on your own: Kotlin playground