Menu
Coddy logo textTech

Event Scheduler

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

challenge icon

Challenge

Hard

Build an event scheduler with the standard protocols. Event is a struct with title, day (1-31), start and end hours. It is Comparable (by day, then start, then title), Hashable and Equatable on title and day only (the same event entered twice), and CustomStringConvertible as day 3 09-11 standup. The class Scheduler has add(_:), which returns added, duplicate (same title on the same day) or conflicts with <title> (an existing event on that day whose hours overlap: one starts before the other ends). agenda() returns the events sorted, and busiestDay() returns busiest day 3: 5h or no events.

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

Your code goes in Event.swift and Scheduler.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

// Supplied input/output code: keep it as it is
var input: [String] = []
while let line = readLine() { input.append(line) }
let scheduler = Scheduler()
for line in input {
    let p = line.split(separator: ",").map(String.init)
    print(scheduler.add(Event(title: p[0], day: Int(p[1])!, start: Int(p[2])!, end: Int(p[3])!)))
}
for e in scheduler.agenda() { print(e) }
print(scheduler.busiestDay())

All lessons in Object Oriented Programming

Practice on your own: Swift playground