Menu
Coddy logo textTech

Recap - Staff Payroll

Part of the Object Oriented Programming section of Coddy's R journey. Lesson 16 of 57.

challenge icon

Challenge

Medium

Build 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() returns Ada: 5000.00 with pay() formatted to two decimals; and manager_cost(staff), the total monthly pay of everyone who inherits from manager.
  • Manager.R: new_manager(name, salary, bonus) with the extra field bonus; pay.manager() adds a twelfth of the bonus to the employee pay with NextMethod(); format.manager() adds (manager) after the employee text with NextMethod().
  • Intern.R: new_intern(name, salary); pay.intern() is half the employee pay, using NextMethod(). 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

Practice on your own: Online R compiler