Recap - Staff Payroll
Part of the Object Oriented Programming section of Coddy's R journey. Lesson 16 of 57.
Challenge
MediumBuild a payroll over three files. The generic pay() is given in Employee.R.
Employee.R:new_employee(name, salary, ..., class = character());pay.employee()is the salary divided by 12;format.employee()returnsAda: 5000.00withpay()formatted to two decimals; andmanager_cost(staff), the total monthly pay of everyone who inherits frommanager.Manager.R:new_manager(name, salary, bonus)with the extra fieldbonus;pay.manager()adds a twelfth of the bonus to the employee pay withNextMethod();format.manager()adds(manager)after the employee text withNextMethod().Intern.R:new_intern(name, salary);pay.intern()is half the employee pay, usingNextMethod(). Interns keep the employee format.
The supplied code reads lines employee Bo 36000, manager Ada 60000 12000 or intern Cy 24000, prints every person's format, then the managers' total.
Your code goes in Employee.R, Manager.R and Intern.R. main.R holds the supplied input/output code and cannot be edited.
Try it yourself
source("Employee.R")
source("Manager.R")
source("Intern.R")
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
staff <- list()
for (line in input) {
p <- strsplit(line, " ")[[1]]
staff[[length(staff) + 1]] <- switch(p[1],
employee = new_employee(p[2], as.numeric(p[3])),
manager = new_manager(p[2], as.numeric(p[3]), as.numeric(p[4])),
intern = new_intern(p[2], as.numeric(p[3])))
}
for (x in staff) cat(format(x), "\n", sep = "")
cat(sprintf("managers cost %.2f per month", manager_cost(staff)), "\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 Books3S3 Inheritance
Class VectorsMethod Lookup OrderExtending With NextMethodChecking With inherits()Recap - Staff PayrollPractice on your own: Online R compiler