R/account.R

#' @name account
#' @aliases account
#' @author Suberlin Sinaga @2021
#' @description Store the account information that owns the asset(s)/instrument(s)
#' @title Account manager
#' @return  Object of type \code{\link{account}}
#' @importFrom dplyr tibble
#' @importFrom stringr str_match
#' @importFrom stringr str_extract_all
account <- R6Class(
  "account",
  inherit = portfolio,
  public = list(
    #' @description A simple method to add account
    #' @param platform: The platform where we have the accounts
    #' @param name: The name of the account owner
    #' @param username: The username of the account owner
    #' @param totDep: The number of deposit
    #' @param totWithd: The number of withdrawal had been done
    #' @return NA
    add_account = function(platform, name, username, totDep, totWithd) {
      cat("Creating account...\n")
      n = (str_match(names(private), "(?<=account)[0-9]*") %>%
             na.omit() %>%
             length() %>%
             max(0, na.rm = TRUE)) + 1

      if (n > 1){
        cat(paste("\tAccount no.", n, "...\n"))
        private[[paste0("account", n)]] = list(
          id = n,
          platform = platform,
          username = username,
          name = name,
          totDep = totDep,
          totWithd = totWithd
        )
      } else {
        cat("\tCreating first account...\n")
        private$account1 = tibble(
          id = n,
          platform = platform,
          username = username,
          name = name,
          totDep = totDep,
          totWithd = totWithd
        )
      }
    }
  ),

  active = list(
    #' @description Method to get the last number of the account
    #' @return The number of account has been registered already.
    last_acc = function() {
      n = (str_match(names(private), "(?<=account)[0-9]*") %>%
             na.omit() %>%
             length() %>%
             max(0, na.rm = TRUE))
      if (n == 0) {
        ("No account registered yet!")
      } else {
        n <- str_extract_all(names(private), "(?<=account)[0-9]*", simplify = TRUE) %>% max()
        (paste("You registered", n, "account(s)."))
      }
    },
    list_all_account = function(x) {
      all_accounts <- (str_match(names(private), ifelse(missing(x), "account[0-9]*", x))) %>% na.omit()
      all_account_names = c()
      all_usernames = c()
      all_platforms = c()
      all_ids = c()

      for (name in all_accounts) {
        all_account_names[name] <- private[[name]]$name
        all_usernames[name] <- private[[name]]$username
        all_platforms[name] <- private[[name]]$platform
        all_ids[name] <- private[[name]]$id
      }

      return(tibble(account_id = all_ids, account_name = all_account_names, username  = all_usernames, platform = all_platforms))
    }
  ),
  lock_objects = FALSE
)
blakcjack/ims documentation built on Dec. 19, 2021, 9:52 a.m.