Menu
Coddy logo textTech

Event Scheduler

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

challenge icon

Challenge

Hard

Build an event scheduler with the special methods of this section. Event.new(title, day, start, finish) has readers for all four. It includes Comparable and orders by day, then start hour, then title. Two events are equal with ==, eql? and hash when they have the same title on the same day (the same event entered twice). to_s returns day 3 09-11 standup, with two-digit hours. overlaps?(other) is true on the same day when each starts before the other finishes. Scheduler#add(event) returns duplicate for an equal event, conflicts with review for the first stored event (in order) that overlaps, and otherwise stores it and returns added. Scheduler includes Enumerable; its each yields the stored events in order. busiest_day returns busiest day 3: 5h, the day with the most scheduled hours (the earlier day on a tie), or no events.

The supplied code reads events as title,day,start,finish lines and prints the reply for each, then every event in order through each, and the busiest day.

Your code goes in event.rb and scheduler.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'event'
require_relative 'scheduler'

# Supplied input/output code: keep it as it is
input = []
while (line = gets)
  input << line.chomp
end
scheduler = Scheduler.new
input.each do |line|
  title, day, start, finish = line.split(",")
  puts scheduler.add(Event.new(title, day.to_i, start.to_i, finish.to_i))
end
scheduler.each { |event| puts event }
puts scheduler.busiest_day

All lessons in Object Oriented Programming

Practice on your own: Online Ruby compiler