Skip to contents

An extension of the MainAccount class used to model specialized sub-accounts such as goals, needs, or debt repayment accounts. Child accounts inherit all the core functionality of MainAccount while adding features like allocation percentage, account status, and priority levels for fund distribution.

Details

  • Inherits from MainAccount.

  • Can be part of a parent account and participate in fund distribution.

  • Tracks its own balance, transaction history, and priority for receiving funds.

Methods

initialize(name, allocation, status, parent, path, priority)

Constructor method.

deposit(amount, transaction_number, by, channel, date)

Overridden deposit method with status check.

change_status(status)

Updates the status of the account.

get_account_status()

Returns and prints the account's status.

get_priority()

Returns the priority level of the account.

set_priority(priority)

Sets a new priority level.

See also

Super class

finman::MainAccount -> ChildAccount

Public fields

allocation

Numeric. Share of distributed income (0-1 scale).

status

Character. Indicates whether the account is "active" or "closed".

parent

Optional. Reference to the parent account (if hierarchical).

path

Character. Logical path to the account (used for organizing accounts).

priority

Numeric. Determines order of distribution among children (higher = more).

Methods

Inherited methods


Method new()

Initializes a new ChildAccount object by setting the name, allocation, status, parent, path, and priority. Inherits initialization logic from the MainAccount class.

Usage

ChildAccount$new(
  name,
  allocation = 0,
  status = "active",
  parent = NULL,
  path = NULL,
  priority = 0
)

Arguments

name

Character. The name of the child account.

allocation

Numeric. Allocation weight for distributing funds (default is 0).

status

Character. Status of the account: "active", "inactive", or "closed" (default is "active").

parent

MainAccount or NULL. Optional parent account reference.

path

Character or NULL. Path or label to identify account lineage.

priority

Numeric. Priority value used when distributing small amounts (default is 0).

Examples

# Create a basic ChildAccount with default values
acc <- ChildAccount$new(name = "Emergency Fund")

# Create a ChildAccount with custom allocation and priority
acc2 <- ChildAccount$new(
  name = "Education",
  allocation = 0.3,
  status = "active",
  path = "main_account/education",
  priority = 2
)

# View the account status
acc2$get_account_status()


Method deposit()

Deposits funds into a ChildAccount if the account is active. Records the transaction, updates the balance, and distributes the funds to any nested child accounts, if applicable.

Usage

ChildAccount$deposit(
  amount,
  transaction_number = NULL,
  by = "User",
  channel = NULL,
  date = Sys.time()
)

Arguments

amount

Numeric. Amount of money to deposit. Must be greater than 0.

transaction_number

Character or NULL. Optional transaction ID. If NULL, an ID is generated automatically.

by

Character. Identifier for who made the deposit (default is "User").

channel

Character or NULL. Channel through which the deposit is made (e.g., "Mobile", "Bank").

date

POSIXct. Timestamp of the transaction (default is current time).

Details

This method overrides the deposit() method from the MainAccount class. It includes a status check to ensure the account is active before proceeding. If the account is inactive or closed, the deposit is blocked and a message is printed.

Returns

No return value. Updates the account state and prints a summary.

Examples

# Create an active child account
acc <- ChildAccount$new(name = "Savings", allocation = 0.5)

# Deposit funds into the account
acc$deposit(amount = 100, channel = "Mobile")

# Attempting to deposit into an inactive account
acc$change_status("inactive")
acc$deposit(amount = 50, channel = "Mobile")  # Will not proceed


Method change_status()

Changes the status of the ChildAccount. If the new status is "closed", the account must have a zero balance; otherwise, an error is thrown.

Usage

ChildAccount$change_status(status)

Arguments

status

Character. The desired new status of the account. Acceptable values include "active", "inactive", or "closed".

Details

If the account is already "closed", a message is printed and no changes are made. If closing is requested and the balance is not zero, an error is raised to ensure proper fund handling before deactivation.

Returns

No return value. Modifies the account's status in place and prints a message.

Examples

acc <- ChildAccount$new(name = "Emergency Fund", allocation = 0.3)
acc$change_status("inactive")  # Changes status to inactive
acc$change_status("active")    # Re-activates the account

# Attempting to close with non-zero balance triggers error
acc$deposit(100, channel = "Mobile")
\dontrun{
acc$change_status("closed")    # Will raise an error
}

# Withdraw funds then close
acc$withdraw(100, channel = "Transfer")
acc$change_status("closed")    # Successful closure


Method get_account_status()

Retrieves and prints the current status of the child account.

Usage

ChildAccount$get_account_status()

Returns

Character. The current status of the account: typically "active", "inactive", or "closed".

Examples

acc <- ChildAccount$new(name = "School Fees", allocation = 0.4)
acc$get_account_status()
# Output: "School Fees is active"


Method get_priority()

Returns the current priority level assigned to the child account.

Usage

ChildAccount$get_priority()

Returns

Numeric. The priority value used in fund distribution (higher values indicate higher priority).

Examples

acc <- ChildAccount$new(name = "Emergency Fund", priority = 3)
acc$get_priority()
# [1] 3


Method set_priority()

Updates the priority level of the child account. Higher priority values indicate a stronger preference for receiving funds during distribution.

Usage

ChildAccount$set_priority(priority)

Arguments

priority

Numeric. The new priority value to assign to the account.

Returns

No return value. Prints a message confirming the new priority.

Examples

acc <- ChildAccount$new(name = "Education Fund", priority = 1)
acc$set_priority(5)
# Priority for Education Fund set to 5


Method clone()

The objects of this class are cloneable with this method.

Usage

ChildAccount$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

library(R6)
library(uuid)
library(tidyverse)
#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
#>  dplyr     1.1.4      readr     2.1.5
#>  forcats   1.0.0      stringr   1.5.1
#>  ggplot2   3.5.2      tibble    3.3.0
#>  lubridate 1.9.4      tidyr     1.3.1
#>  purrr     1.1.0     
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#>  dplyr::filter() masks stats::filter()
#>  dplyr::lag()    masks stats::lag()
#>  Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Create a basic ChildAccount instance
child <- ChildAccount$new(
  name = "Emergency Fund",
  allocation = 0.3,
  priority = 2
)

# Check initial status and priority
child$get_account_status()
#> Emergency Fund is active 
#> [1] "active"
child$get_priority()
#> [1] 2

# Deposit into the child account
child$deposit(
  amount = 1000,
  channel = "Bank Transfer"
)
#> Deposited: 1000 via Bank Transfer - Transaction ID: sys1 

# Change account status to inactive
child$change_status("inactive")
#> Emergency Fund has become inactive .

# Try another deposit (won't proceed if inactive)
child$deposit(
  amount = 500,
  channel = "Bank Transfer"
)
#> Deposit not allowed. Account is not active.

# Close the account after setting balance to zero
child$withdraw(
  amount = child$balance,
  channel = "Transfer to Main"
)
#> Withdrew: 1000 via Transfer to Main - Transaction ID: sys2 
child$change_status("closed")
#> Emergency Fund has been closed.


## ------------------------------------------------
## Method `ChildAccount$new`
## ------------------------------------------------

# Create a basic ChildAccount with default values
acc <- ChildAccount$new(name = "Emergency Fund")

# Create a ChildAccount with custom allocation and priority
acc2 <- ChildAccount$new(
  name = "Education",
  allocation = 0.3,
  status = "active",
  path = "main_account/education",
  priority = 2
)

# View the account status
acc2$get_account_status()
#> Education is active 
#> [1] "active"

## ------------------------------------------------
## Method `ChildAccount$deposit`
## ------------------------------------------------

# Create an active child account
acc <- ChildAccount$new(name = "Savings", allocation = 0.5)

# Deposit funds into the account
acc$deposit(amount = 100, channel = "Mobile")
#> Deposited: 100 via Mobile - Transaction ID: sys1 

# Attempting to deposit into an inactive account
acc$change_status("inactive")
#> Savings has become inactive .
acc$deposit(amount = 50, channel = "Mobile")  # Will not proceed
#> Deposit not allowed. Account is not active.

## ------------------------------------------------
## Method `ChildAccount$change_status`
## ------------------------------------------------

acc <- ChildAccount$new(name = "Emergency Fund", allocation = 0.3)
acc$change_status("inactive")  # Changes status to inactive
#> Emergency Fund has become inactive .
acc$change_status("active")    # Re-activates the account
#> Emergency Fund has become active .

# Attempting to close with non-zero balance triggers error
acc$deposit(100, channel = "Mobile")
#> Deposited: 100 via Mobile - Transaction ID: sys1 
if (FALSE) { # \dontrun{
acc$change_status("closed")    # Will raise an error
} # }

# Withdraw funds then close
acc$withdraw(100, channel = "Transfer")
#> Withdrew: 100 via Transfer - Transaction ID: sys2 
acc$change_status("closed")    # Successful closure
#> Emergency Fund has been closed.

## ------------------------------------------------
## Method `ChildAccount$get_account_status`
## ------------------------------------------------

acc <- ChildAccount$new(name = "School Fees", allocation = 0.4)
acc$get_account_status()
#> School Fees is active 
#> [1] "active"
# Output: "School Fees is active"

## ------------------------------------------------
## Method `ChildAccount$get_priority`
## ------------------------------------------------

acc <- ChildAccount$new(name = "Emergency Fund", priority = 3)
acc$get_priority()
#> [1] 3
# [1] 3

## ------------------------------------------------
## Method `ChildAccount$set_priority`
## ------------------------------------------------

acc <- ChildAccount$new(name = "Education Fund", priority = 1)
acc$set_priority(5)
#> Priority for Education Fund set to 5 
# Priority for Education Fund set to 5