Event Scheduler
Part of the Object Oriented Programming section of Coddy's R journey. Lesson 56 of 57.
Challenge
HardBuild an event scheduler over two files.
Event.R: the S3 classeventwith the fieldstitle,day,start,end(hours). Writenew_event();validate_event(), which stops withday must be from 1 to 31orhours must be within 0 to 24 and start before end; the helperevent(title, day, start, end), which converts the text arguments to numbers;format.event(), which returnsday 3 09-11 standup; andoverlaps(a, b),TRUEwhen two events are on the same day and each starts before the other ends.Scheduler.R:make_scheduler()keeps its events private and returnsadd(e), which repliesduplicatefor the same title on the same day,conflicts with reviewwhen the event overlaps an existing one, oradded;agenda(), the formatted events sorted by day, start and title; andbusiest_day(),busiest: day 3 (4 hours)by total hours (the earlier day on a tie), orno events.
Use call. = FALSE. The supplied code reads lines title,day,start,end, prints the reply for each, then the agenda and the busiest day.
Your code goes in Event.R and Scheduler.R. main.R holds the supplied input/output code and cannot be edited.
Try it yourself
source("Event.R")
source("Scheduler.R")
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
s <- make_scheduler()
for (line in input) {
p <- strsplit(line, ",")[[1]]
reply <- tryCatch(s$add(event(p[1], p[2], p[3], p[4])), error = function(e) paste("invalid:", conditionMessage(e)))
cat(p[1], ": ", reply, "\n", sep = "")
}
cat("agenda:\n")
for (l in s$agenda()) cat(" ", l, "\n", sep = "")
cat(s$busiest_day(), "\n", sep = "")
All lessons in Object Oriented Programming
1S3 Basics
Working With FilesLists With a ClassConstructor FunctionsPrint and Format MethodsValidators and HelpersRecap - Temperatures4Encapsulation in R
Closures as ObjectsEnvironments as ObjectsAccessor FunctionsGuarding StateRecap - Parking Meter2S3 Generics and Methods
Generics and UseMethodDefault MethodsMethods for Base GenericsNextMethod BasicsRecap - Shape Areas8Reference Classes
Defining Reference ClassesMethods and Field UpdatesCopy SemanticsInheritance and callSuperRecap - Task Queue11Project: Library Management
Books and MembersBorrowing BooksPractice on your own: Online R compiler