🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we will learn how to build a simple Banking Management System project in Kotlin. we are going to use an in-memory object to store the data.
System Overview
Our Banking Management System will be equipped to:
- Open a new account.
- Close an existing account.
- Deposit money.
- Withdraw money.
- Check the balance.
- Display all accounts.
Implementation
1. Class Definitions:
We'll start by defining the classes: BankAccount and Bank.
data class BankAccount(val accountId: Int, var holderName: String, var balance: Double)
class Bank {
private val accounts = mutableListOf<BankAccount>()
fun openAccount(holderName: String): BankAccount {
val account = BankAccount(accounts.size + 1, holderName, 0.0)
accounts.add(account)
return account
}
fun closeAccount(accountId: Int) {
accounts.removeIf { it.accountId == accountId }
}
fun deposit(accountId: Int, amount: Double) {
val account = accounts.find { it.accountId == accountId }
account?.balance = (account?.balance ?: 0.0) + amount
}
fun withdraw(accountId: Int, amount: Double): Boolean {
val account = accounts.find { it.accountId == accountId }
return if (account != null && account.balance >= amount) {
account.balance -= amount
true
} else false
}
fun checkBalance(accountId: Int): Double? {
return accounts.find { it.accountId == accountId }?.balance
}
fun displayAllAccounts() {
accounts.forEach { println("${it.accountId} - ${it.holderName} - ${it.balance}") }
}
}2. Main Function:
fun main() {
val bank = Bank()
loop@ while (true) {
println("\nOptions:\n1. Open Account\n2. Close Account\n3. Deposit\n4. Withdraw\n5. Check Balance\n6. Display All Accounts\n7. Exit")
when (readLine()?.toIntOrNull()) {
1 -> {
println("Enter Holder Name: ")
val name = readLine()!!
val account = bank.openAccount(name)
println("Account opened with ID: ${account.accountId}.")
}
// ... handle other options similarly ...
7 -> {
println("Exiting...")
break@loop
}
else -> println("Invalid choice!")
}
}
}Complete Working Code with Output
- Copy the complete code.
- Use a Kotlin compiler or IDE to compile the code and run the program.
- Follow the menu options to add books, register members, issue, and return books.
data class BankAccount(val accountId: Int, var holderName: String, var balance: Double)
class Bank {
private val accounts = mutableListOf<BankAccount>()
fun openAccount(holderName: String): BankAccount {
val account = BankAccount(accounts.size + 1, holderName, 0.0)
accounts.add(account)
return account
}
fun closeAccount(accountId: Int) {
accounts.removeIf { it.accountId == accountId }
}
fun deposit(accountId: Int, amount: Double) {
val account = accounts.find { it.accountId == accountId }
account?.balance = (account?.balance ?: 0.0) + amount
}
fun withdraw(accountId: Int, amount: Double): Boolean {
val account = accounts.find { it.accountId == accountId }
return if (account != null && account.balance >= amount) {
account.balance -= amount
true
} else false
}
fun checkBalance(accountId: Int): Double? {
return accounts.find { it.accountId == accountId }?.balance
}
fun displayAllAccounts() {
accounts.forEach { println("${it.accountId} - ${it.holderName} - ${it.balance}") }
}
}
fun main() {
val bank = Bank()
loop@ while (true) {
println("\nOptions:\n1. Open Account\n2. Close Account\n3. Deposit\n4. Withdraw\n5. Check Balance\n6. Display All Accounts\n7. Exit")
when (readLine()?.toIntOrNull()) {
1 -> {
println("Enter Holder Name: ")
val name = readLine()!!
val account = bank.openAccount(name)
println("Account opened with ID: ${account.accountId}.")
}
2 -> {
println("Enter Account ID to close: ")
val id = readLine()!!.toInt()
bank.closeAccount(id)
println("Account with ID: $id closed.")
}
3 -> {
println("Enter Account ID for deposit: ")
val id = readLine()!!.toInt()
println("Enter Amount: ")
val amount = readLine()!!.toDouble()
bank.deposit(id, amount)
println("Deposited $amount to Account ID: $id.")
}
4 -> {
println("Enter Account ID for withdrawal: ")
val id = readLine()!!.toInt()
println("Enter Amount: ")
val amount = readLine()!!.toDouble()
if (bank.withdraw(id, amount)) {
println("Withdrew $amount from Account ID: $id.")
} else {
println("Insufficient funds or invalid account!")
}
}
5 -> {
println("Enter Account ID to check balance: ")
val id = readLine()!!.toInt()
val balance = bank.checkBalance(id)
if (balance != null) {
println("Balance for Account ID $id: $balance")
} else {
println("Account not found!")
}
}
6 -> {
println("All accounts:")
bank.displayAllAccounts()
}
7 -> {
println("Exiting...")
break@loop
}
else -> println("Invalid choice!")
}
}
}Options:
1. Open Account
2. Close Account
3. Deposit
4. Withdraw
5. Check Balance
6. Display All Accounts
7. Exit
Enter your choice: 1
Enter Holder Name: John Doe
Account opened with ID: 1.
Options:
...
Enter your choice: 3
Enter Account ID for deposit: 1
Enter Amount: 500
Deposited 500.0 to Account ID: 1.
Options:
...
Enter your choice: 4
Enter Account ID for withdrawal: 1
Enter Amount: 200
Withdrew 200.0 from Account ID: 1.
Options:
...
Enter your choice: 5
Enter Account ID to check balance: 1
Balance for Account ID 1: 300.0
Options:
...
Enter your choice: 6
All accounts:
1 - John Doe - 300.0
Options:
...
Enter your choice: 7
Exiting...
Comments
Post a Comment
Leave Comment