Recap - Task Queue
Part of the Object Oriented Programming section of Coddy's R journey. Lesson 41 of 57.
Challenge
MediumBuild a task queue from two Reference Classes. Both definitions are wrapped in suppressMessages().
TaskinTask.R: fieldstitle,priority(numeric) anddone(logical);initializemakesdoneFALSEwhen it is not given;label()returns[x] fix (3)for a finished task or[ ] fix (3).TaskQueueinTaskQueue.R: the fieldtasks(alist);add(title, priority)appends a newTaskand returnsinvisible(.self);next_task()returns the title of the open task with the highest priority (the earliest added on a tie), ornothing;complete(title)marks the first open task with that title as done and returnsTRUE, orFALSE;report()returns one label per task in the order added, thenopen: 2.
The supplied code reads add fix 3, next and done fix, then adds two tasks in one chained call and prints the report.
Your code goes in Task.R and TaskQueue.R. main.R holds the supplied input/output code and cannot be edited.
Try it yourself
source("Task.R")
source("TaskQueue.R")
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
q <- TaskQueue$new()
for (line in input) {
p <- strsplit(line, " ")[[1]]
if (p[1] == "add") {
q$add(p[2], as.numeric(p[3]))
} else if (p[1] == "next") {
cat("next: ", q$next_task(), "\n", sep = "")
} else if (p[1] == "done") {
cat(if (q$complete(p[2])) "completed " else "no open task ", p[2], "\n", sep = "")
}
}
q$add("late", 1)$add("later", 1)
writeLines(q$report())
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