Skip to content

Instantly share code, notes, and snippets.

@GerganaT
Created February 10, 2023 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GerganaT/7cb0c0af04e2ecf893fbea5c0ca1ebd7 to your computer and use it in GitHub Desktop.
Save GerganaT/7cb0c0af04e2ecf893fbea5c0ca1ebd7 to your computer and use it in GitHub Desktop.
Internship Task 0
package com.example.myapplication
// please note - the below is a pseudo-code and in a "normal" app all the interfaces and classes
// would be in different folders and/or packages
// also,the menu navigation could be a bit more sophisticated and in a real app I'd also add
// password handling logic
// interfaces to handle different types of banking operations
interface AccountOperationsHandler {
fun isRegisteredAccount(accountId: Int): Boolean
fun registerAccount()
fun deleteAccount(accountId: Int)
}
interface FinancialOperationsHandler {
fun depositMoney(accountId: Int, sumToDeposit: Int)
fun withdrawMoney(accountId: Int, sumToWithdraw: Int)
}
data class RegisteredBankAccount(
val accountId: Int = 0,
val isSpecialAccount: Boolean = false,
var accountBalance: Int = 0
)
// our dummy data of registered accounts - keyed per accountId
val registeredBankAccounts: MutableMap<Int, RegisteredBankAccount> = mutableMapOf()
// Handle banking operations- the API between the user and the system
class BankingVirtualAssistant : AccountOperationsHandler, FinancialOperationsHandler {
/**Util function,which reads user input from the console - used to reduce boilerplate code*/
private fun readInput(): String? = System.console()?.readLine()
fun initializeUserInteraction() {
// came up with the idea to use one's social security number as this would be an unique id
// of course in a real-time scenario that would be much more sophisticated with database
// with unique keys
println(
"Welcome to the virtual bank , enter your social security number" +
"to login, if you are registered, or No to" +
"register a new account."
)
readInput()?.let { userInput ->
if (userInput == "No") {
registerAccount()
} else {
try {
// since we're not sure if the user supplied valid input this is in try/catch
//block
val accountId = userInput.toInt()
// check if the account is registered and then allow more functionality
if (isRegisteredAccount(accountId)) {
println(
"Please pick up service " +
"- enter R to delete account," +
"- enter D to deposit money," +
"- enter W to withdraw money" +
"- enter C to return to the main menu"
)
when (readInput()) {
"R" -> deleteAccount(accountId)
"D" -> {
println("Please enter money to deposit")
try {
readInput()?.toInt()?.let { moneyToDeposit ->
depositMoney(accountId, moneyToDeposit)
}
} catch (e: Exception) {
println("Invalid input,please try again")
initializeUserInteraction()
}
}
"W" -> {
println("Please enter money to withdraw")
try {
readInput()?.toInt()?.let { moneyToWithdraw ->
withdrawMoney(accountId, moneyToWithdraw)
}
} catch (e: Exception) {
println("Invalid input,please try again")
initializeUserInteraction()
}
}
"C" -> initializeUserInteraction()
else -> throw Exception()
}
}
} catch (e: Exception) {
println("Please check your input and try again.")
initializeUserInteraction()
}
}
}
}
override fun isRegisteredAccount(accountId: Int): Boolean =
accountId in registeredBankAccounts.keys
private fun calculateTransactionTax(isSpecialAccount: Boolean, totalMoney: Int): Int =
((if (isSpecialAccount) 5 else 10) * totalMoney) / 100
override fun registerAccount() {
// attempt to turn the user's reply into a valid data and catch an error and inform user
// if the input was incorrect
try {
println(
"Please enter your social security number - this will serve as your account's " +
"unique id"
)
val userId = readInput()?.toInt()
println(
"Would you like to create a normal or a special account? " +
"- please enter N for normal or S for Special. "
)
val accountType = when (readInput()) {
"N" -> false
"S" -> true
else -> throw Exception()
}
userId?.let {
RegisteredBankAccount(userId, accountType).run {
registeredBankAccounts.put(accountId, this)
}
}
} catch (e: Exception) {
println("Please check your input and try again.")
registerAccount()
}
}
override fun deleteAccount(accountId: Int) {
println("Are you sure? This cannot be undone.Enter Y to confirm and N to exit")
readInput()?.let { userInput ->
when (userInput) {
// the money is "withdrawn" from the account and the account is deleted.
"Y" -> {
registeredBankAccounts[accountId]?.accountBalance = 0
registeredBankAccounts.remove(accountId)
println("Account deleted.")
return
}
// user exits to the main menu
"N" -> initializeUserInteraction()
else -> {
println("Please check your input and try again")
initializeUserInteraction()
}
}
}
}
override fun depositMoney(accountId: Int, sumToDeposit: Int) {
registeredBankAccounts[accountId]?.run {
// the bank charges tax at the moment of the transaction ,so we calculate the
// balance after the taxes(same goes for withdrawal)
val depositTax = calculateTransactionTax(isSpecialAccount, accountBalance)
accountBalance += (sumToDeposit - depositTax)
println(
"Your balance after the deposit is" +
" $accountBalance and your tax is $depositTax dollars"
)
}
}
override fun withdrawMoney(accountId: Int, sumToWithdraw: Int) {
registeredBankAccounts[accountId]?.run {
val withdrawalTax = calculateTransactionTax(isSpecialAccount, accountBalance)
//check that the withdrawal sum is valid to avoid incorrect numbers
if (sumToWithdraw <= accountBalance) {
accountBalance -= (sumToWithdraw - withdrawalTax)
println(
"Your balance after the withdrawal is" +
" $accountBalance and your tax is $withdrawalTax dollars"
)
} else {
println("You have entered invalid sum,please check your input and try again")
initializeUserInteraction()
}
}
}
}
fun main() {
BankingVirtualAssistant().initializeUserInteraction()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment