Last active
June 14, 2021 14:43
-
-
Save GeorgeOduor/22a818b678777dc03d1e07210d7f6d94 to your computer and use it in GitHub Desktop.
This script is an illustration of Object oriented programming in R.It simulates a simple core banking system in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script simulates a simple core banking system in R.Its an illustration of Object oriented programming in R | |
CBS <- R6Class(classname = "CBS",public = list( | |
accs = tibble("Full Name" = NA,"IDNo" = NA,'AccountNo' = NA,'AccountBalance' = NA,'LoanBalance' = NA), | |
bank_name = NA, | |
initialize = function(accs = NULL,bank_name) { | |
self$bank_name = bank_name | |
cat(paste(' --------- \n This is ',self$bank_name,'. \n --------- \n')) | |
}, | |
create_account = function(FirstName=NULL,SecondName=NULL,IDNo=NULL) { | |
FirstName = readline(prompt = "Enter FirstName: ") | |
SecondName = readline(prompt = "Enter SecondName: ") | |
IDNo = readline(prompt = "Enter IDNo: ") | |
id_acc = private$account_setup(IDNo) | |
account_details = tibble( | |
"Full Name" = paste(FirstName,SecondName), | |
"IDNo" = as.character(pluck(id_acc,'IDNo')), | |
'AccountNo' = pluck(id_acc,'accno'), | |
'AccountBalance' = 0, | |
'LoanBalance' = 0 | |
) | |
self$accs = bind_rows(self$accs,account_details) %>% filter(!is.na(IDNo)) | |
cat(sprintf("Hi %s, welcome to ABCBank.We look foward to doing business with you!\nYour account number is %s", | |
FirstName,account_details$AccountNo)) | |
self$get_services(acc = account_details$AccountNo) | |
# return(invisible(account_details)) | |
}, | |
make_deposit = function(acc=NULL,Amount=NULL){ | |
acc = readline(prompt = "Enter acc ") | |
Amount = as.numeric(readline(prompt = "Enter Amount ")) | |
private$check_acc(acc = acc,expr = { | |
self$accs = self$accs %>% mutate(AccountBalance = ifelse(AccountNo == acc,AccountBalance+Amount,AccountBalance)) | |
cat(glue("Dear Customer,your deposit of Ksh {Amount} was received successfully.Your current balance is Ksh { | |
pull(filter(self$accs,AccountNo == acc),AccountBalance )}"))}) | |
}, | |
make_withdrawal = function(acc=NULL,Amount=NULL){ | |
acc = readline(prompt = "Enter acc ") | |
Amount = as.numeric(readline(prompt = "Enter Amount ")) | |
private$check_acc(acc = acc,expr = { | |
info = filter(self$accs,AccountNo == acc) | |
if ( (pull(info,AccountBalance) - 100) > Amount ) { | |
self$accs <- self$accs %>% mutate( | |
AccountBalance = ifelse(AccountNo == acc,AccountBalance - Amount,AccountBalance) | |
) | |
}else{ | |
message(paste(' ----------\n Dear ',pull(info,`Full Name`),',we are unable to process a your request due to low balance in your account.Please try a lower value')) | |
} | |
}) | |
}, | |
check_balance = function(acc=NULL){ | |
acc = readline(prompt = "Enter acc ") | |
private$check_acc(acc = acc,expr = { | |
balance = self$accs %>% filter(AccountNo == acc) %>% pull(AccountBalance) | |
message(glue("--------------\n Dear Customer,your account balance is Ksh {balance}.\n--------------")) | |
}) | |
}, | |
# loans | |
request_loan = function(acc=NULL,Amount=NULL){ | |
acc = readline(prompt = "Enter acc ") | |
Amount = as.numeric(readline(prompt = "Enter Amount ")) | |
private$check_acc(acc = acc,expr = { | |
info = filter(self$accs,AccountNo == acc) | |
if ( (pull(info,LoanBalance)) <= 0 ) { | |
self$accs = self$accs %>% mutate(LoanBalance = ifelse(AccountNo == acc,Amount,LoanBalance)) | |
cat(glue("Dear Customer,your loan request of Ksh {Amount} was processed successfully.Your outstanding loan balance is Ksh { | |
pull(filter(self$accs,AccountNo == acc),LoanBalance )}")) | |
}else{ | |
message(glue("Dear Customer,your loan request of Ksh {Amount} was not successfull.Please clear your outstanding loan balance of Ksh { | |
pull(filter(self$accs,AccountNo == acc),LoanBalance )}.")) | |
} | |
}) | |
}, | |
repay_loan = function(acc=NULL,Amount=NULL) { | |
acc = readline(prompt = "Enter acc ") | |
Amount = as.numeric(readline(prompt = "Enter Amount ")) | |
private$check_acc(acc = acc,expr = { | |
info = filter(self$accs,AccountNo == acc) | |
if ((pull(info,LoanBalance)) > 0 ) { | |
self$accs = self$accs %>% mutate(LoanBalance = ifelse(AccountNo == acc,LoanBalance-Amount,LoanBalance), | |
AccountBalance = ifelse((AccountNo == acc) & (LoanBalance<0),abs(LoanBalance)+AccountBalance,AccountBalance ), | |
LoanBalance = ifelse(LoanBalance < 0 ,0,LoanBalance)) | |
if ( (pull(info,LoanBalance)) >= Amount ) { | |
cat(glue("Dear Customer,your loan payment of Ksh {Amount} was processed successfully.Your outstanding loan balance is Ksh { | |
pull(filter(self$accs,AccountNo == acc),LoanBalance )}")) | |
}else{ | |
cat(glue("Dear Customer,your loan payment of Ksh {Amount} was processed successfully.Your loan is cleared,extra amount of Ksh {abs((pull(info,LoanBalance)) - Amount)} was deposited in your transactional account")) | |
} | |
}else{ | |
message("Dear Customer,you do not have an existing loan.") | |
} | |
}) | |
}, | |
check_loan_balance = function(acc=NULL){ | |
acc = readline(prompt = "Enter acc ") | |
private$check_acc(acc = acc,expr = { | |
balance = self$accs %>% filter(AccountNo == acc) %>% pull(LoanBalance) | |
cat(glue("--------------\n Dear Customer,your loan account balance is Ksh {balance}.\n--------------")) | |
}) | |
}, | |
# simulate USSD! | |
get_services = function(acc = NULL) { | |
if (is.null(acc)) { | |
cat('Welcome to ABC Banking Services!\nSelect\n1:Create new account\n2:Deposit\n3:Withdraw\n4:Request Loan\n5:Repay Loan\n6:My Account Balance\n7:My Loan Balance') | |
input <- readline(prompt="Enter here: ") | |
switch (as.character(input), | |
"1" = self$create_account(), | |
"2" = self$make_deposit(), | |
"3" = self$make_withdrawal(), | |
"4" = self$request_loan(), | |
"5" = self$repay_loan(), | |
"6" = self$check_balance(), | |
"7" = self$check_loan_balance() | |
) | |
}else{ | |
cat(paste('-----------\nSelect\n1:Deposit\n2:Withdraw\n3:Request Loan\n4:Repay Loan\n5:My Account Balance\n6:My Loan Balance')) | |
input <- readline(prompt="Enter here: \n--------------\n") | |
switch (as.character(input), | |
"1" = self$make_deposit(), | |
"2" = self$make_withdrawal(), | |
"3" = self$request_loan(), | |
"4" = self$repay_loan(), | |
"5" = self$check_balance(), | |
"6" = self$check_loan_balance() | |
) | |
} | |
}), | |
private = list( | |
account_setup = function(IDNo) { | |
if (nrow(filter(self$accs,!is.na(IDNo)) > 0 )) { | |
accno = paste0("00",substr(paste0(sample(1:100, 12, replace=F),collapse = ""),1,9)) | |
while (accno %in% self$accs$AccountNo) { | |
accno = as.character(rstudioapi::showPrompt(title = "IDNo",message = "Enter here")) | |
} | |
}else{ | |
accno = paste0("00",substr(paste0(sample(1:100, 12, replace=F),collapse = ""),1,9)) | |
} | |
# check if user exists | |
if (nrow(filter(self$accs,!is.na(IDNo)) > 0 )) { | |
while (IDNo %in% self$accs$IDNo) { | |
IDNo = as.character(rstudioapi::showPrompt(title = "IDNo",message = "That ID already exists,Enter correct IDNo ")) | |
} | |
}else{ | |
IDNo = IDNo | |
} | |
out = list( | |
'IDNo' = IDNo, | |
'accno' = accno | |
) | |
return(out) | |
}, | |
check_acc = function(acc,expr={...}) { | |
if (acc %in% self$accs$AccountNo) { | |
expr | |
}else{ | |
message("Account Supplied doesn't exist!") | |
} | |
} | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment