Banking Management System Project in Kotlin

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: 

  1. Open a new account.
  2. Close an existing account.
  3. Deposit money.
  4. Withdraw money.
  5. Check the balance.
  6. Display all accounts.

Implementation

1. Class Definitions: 

We'll start by defining the classes: BankAccount and Bank.

The Bank class manages a list of bank accounts. It exposes methods for operations like opening/closing accounts, deposits, withdrawals, etc.

The BankAccount class encapsulates account data: a unique ID, the holder's name, and the balance.
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:

The main() function provides an interactive menu, enabling users to interact with the system.
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

To test the program: 
  • 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!")
        }
    }
}
Output:
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...

Conclusion

Through this Kotlin-based Banking Management System, we get to see how Kotlin's simple and intuitive syntax enhances the coding experience. The system can be further enriched with features like transaction histories, account types (savings, checking), or even more advanced facilities with networked banking.

Comments