We begin by loading the dining class.

Dining <- R6::R6Class(
  "Dining",

  public = list(
    initialize = function(name, short_name, category_code, capacity, unique_id){
      private$validate(name, short_name, category_code, capacity, unique_id)

      private$.name <- name
      private$.short_name <- short_name
      private$.category_code <- category_code
      private$.capacity <- capacity
      private$.id <- unique_id
      private$.currently_dining <- 0
    },

    add_diner = function(){
      private$.currently_dining <- private$.currently_dining + 1
      invisible(self)
    },

    remove_diner = function(){
      private$.currently_dining <- private$.currently_dining - 1

      if (private$.currently_dining <= 0){
        private$.currently_dining <- 0
      }

      invisible(self)
    }
  ),

  active = list(

    name = function(value){
      if(missing(value)){
        private$.name
      } else{
        stop("Cannot set `$value\n`")
      }
    },

    short_name = function(value){
      if(missing(value)){
        private$.short_name
      } else{
        stop("Cannot set `$short_name`\n")
      }
    },

    category_code = function(value){
      if(missing(value)){
        private$.category_code
      } else{
        stop("Cannot set `$category_code\n`")
      }
    },

    capacity = function(value){
      if(missing(value)){
        private$.capacity
      } else{
        stop("Cannot set `$capacity\n`")
      }
    },

    unique_id = function(value){
      if(missing(value)){
        private$.unique_id
      } else {
        stop("Cannot set `$unique_id`\n")
      }
    },

    service_time = function(value){
      if(missing(value)){

        #returns the service time in seconds
        if(private$.category_code == "quick-service"){
          sample(c(25, 30, 35, 40, 45, 50) *60, 1)
        } else if (private$.category_code == "table-service"){
          sample(c(45,50,55,60,65,70,75)*60, 1)
        } else {
          -1
        }
      } else {
        stop("Cannot set `$service_time`; function of `$category_code`\n")
      }
    },

    currently_dining = function(value){
      if(missing(value)){
        private$.currently_dining
      } else {
        stop("Cannot set `$currently_dining`; use add_diner() and remove_diner()\n")
      }
    }
  ),

  private = list(
    .name = NULL,
    .short_name = NULL,
    .category_code = NULL,
    .capacity = NULL,
    .id = -1,
    .currently_dining = NA,

    validate = function(name, short_name, category_code, capacity, unique_id){
      stopifnot(is.character(name))
      stopifnot(is.character(short_name))
      stopifnot(is.character(category_code))
      stopifnot(is.numeric(capacity))
      stopifnot(is.numeric(unique_id))
    }
  )
)

Testing

We now test the various components of the Dining class. Some components are trivaly simple and therefore we do not test these. Specifically, we test:

Creating Dining Experience

#create dining experience
d1 <- Dining$new("Bob Evans", "be", "table-service", 100, 1)

#check defaults
stopifnot(d1$name == "Bob Evans")
stopifnot(d1$short_name == "be")
stopifnot(d1$category_code == "table-service")
stopifnot(d1$capacity == 100)
stopifnot(d1$unique_id == 1)
stopifnot(d1$currently_dining == 0)

#if we made it here, the test has passed
print("Pass: Creating dining experiences")
## [1] "Pass: Creating dining experiences"

Adding and Removing Diners

#create dining experience
d1 <- Dining$new("Bob Evans", "be", "table-service", 100, 1)

#add two diners and check the number of people dining
d1$add_diner()$add_diner()
stopifnot(d1$currently_dining == 2)

#remove a diner and check the number of people dining
d1$remove_diner()
stopifnot(d1$currently_dining == 1)

#remove diners twice (altough only one more exists) and check that the number of
#people dining is zero
d1$remove_diner()$remove_diner()
stopifnot(d1$currently_dining == 0)

#if we made it here, the test has passed
print("Pass: Adding and removing diners")
## [1] "Pass: Adding and removing diners"