-
-
Save radhalvy/01aea76a390ffe3efffc5f4e3edb5288 to your computer and use it in GitHub Desktop.
This file contains 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
import java.io.File | |
fun main() { | |
println("1) Log in, 2) Create a new account") | |
val logInOperation = try {readLine()!!.toInt()} catch (e: NumberFormatException) {null} | |
when (logInOperation) { | |
1 /* Log into an existing account */ -> { | |
print("Enter your account: ") | |
// Changes whitespaces to underscores | |
val filePath = "src/main/textFiles/${readStringLower()}.txt".replace("\\s".toRegex(), "_") | |
if (doesExist(filePath)) { | |
println("1) Make a deposit, 2) Make a withdrawal, 3) Check account's total balance") | |
val accountOperation = try {readLine()!!.toInt()} catch (e: NumberFormatException) {null} | |
when (accountOperation) { | |
1 /* Make deposit */ -> { | |
print("Amount to deposit: ") | |
try { | |
val amount = readLine()!!.toLong() | |
if (amount > 0) makeDeposit(filePath, amount) | |
else println("Amount cannot be zero nor negative.") | |
} catch (e: NumberFormatException) { | |
println("Amount must be a number.") | |
} | |
} | |
2 /* Make withdrawal */ -> { | |
print("Amount to withdraw: ") | |
try { | |
val amount = readLine()!!.toLong() | |
val accountBalance = getBalance(filePath) | |
if (amount <= 0) println("Amount cannot be zero nor negative.") | |
else if (amount > accountBalance) println("There's not enough money in the account to complete the withdrawal.") | |
else makeWithdrawal(filePath, amount) | |
} catch (e: NumberFormatException) { | |
println("Amount must be a number.") | |
} | |
} | |
3 /* Get balance */ -> println("%,d".format(getBalance(filePath))) | |
else -> println("Invalid option. Try again.") | |
} | |
} | |
else { | |
println("This account doesn't exist. Do you want to create it?") | |
val doContinue = readStringLower() | |
if (doContinue == "yes" || doContinue == "y") { | |
createFile(filePath) | |
} else { | |
println("All right. Closing program...") | |
} | |
} | |
} | |
2 /* Create a new account */ -> { | |
print("Enter the name of your future account: ") | |
// Changes whitespaces to underscores | |
val newAccount = "src/main/textFiles/${readStringLower()}.txt".replace("\\s".toRegex(), "_") | |
createFile(newAccount) | |
} | |
else -> println("Invalid option. Try again.") | |
} | |
} | |
fun createFile(filePath: String) { | |
val file = File(filePath) | |
if (file.exists()) println("This account already exists.") | |
else { | |
file.createNewFile() | |
println("Account created.") | |
} | |
} | |
fun makeDeposit(filePath: String, amount: Long) { | |
val file = File(filePath) | |
if (file.exists()) file.appendText("+ $amount\n") | |
else println("This account doesn't exist.") | |
} | |
fun makeWithdrawal(filePath: String, amount: Long) { | |
val file = File(filePath) | |
if (file.exists()) file.appendText("- $amount\n") | |
else println("This account doesn't exist.") | |
} | |
fun getBalance(filePath: String): Long { | |
val file = File(filePath) | |
val upAmounts: MutableList<Long> = mutableListOf() | |
val downAmounts: MutableList<Long> = mutableListOf() | |
file.forEachLine { | |
// Checks if it's a deposit or a withdrawal, | |
// makes a slice of the amount and adds it to the corresponding list | |
if (it[0] == '+') upAmounts.add(it.slice(2..it.lastIndex).toLong()) | |
else downAmounts.add(it.slice(2..it.lastIndex).toLong()) | |
} | |
return upAmounts.sum() - downAmounts.sum() | |
} | |
fun doesExist(name: String): Boolean { | |
val file = File(name) | |
return file.exists() | |
} | |
private fun readStringLower(): String = readLine()!!.lowercase() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment