Open In App

Banking System using Scala

Last Updated : 05 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this project, we will build a simple Bank Management System using Scala which will perform the following task:

1) Show the existing details of all customers or a specific customer.

2) Add or remove an account

3) Deposit or Withdraw money

4) Checks the overdraft limit

5) Shows interest and dividend amount

Approach:

This program uses the concepts of Inheritance in Scala, Methods in Scala, If-else, Method Overriding, Classes and objects and ListBuffer.

For more information on the above concepts please refer to the below links.

a) Inheritance in Scala:   https://www.geeksforgeeks.org/inheritance-in-scala/

b) Methods in Scala:  https://www.geeksforgeeks.org/scala-functions-basics/

c) Method Overriding: https://www.geeksforgeeks.org/method-overriding-in-scala/

d) ListBuffer: https://www.geeksforgeeks.org/scala-listbuffer/

This program has 5 classes named as Account, SavingAccount, CurrentAccount, Bank, and the main class Banking_System.

1) Account: It is the parent class and contains three methods in it namely debit(), credit(), and details(). This class contains 4 ListBuffer which are used to store the details of current account holders of bank stored in a sequence such that the index of one customer’s bank account number and its corresponding phone number will be stored in the same index value of different ListBuffer.

As the image shows first customer details will be stored in the first index of all ListBuffer and as will be for second customer etc. Credit() and details() method will be used later as an override method to show the debit amount and details of any customer.

Below example explains how it works.

Scala




class Account{
  
  // Creating four ListBuffer which stores
  // the initial details of customer
  var name = new ListBuffer[String]()
  name += ( "Ankit", "Rohit", "Rahul" )
  var balance_current = new ListBuffer[Int]()
  balance_current += ( 20000, 30000, 40000 )
  var account_number = new ListBuffer[Int]()
  account_number += ( 1234, 5678, 9101 )
  var phone_number = new ListBuffer[Long]()
  phone_number += ( 9998273493L, 5569392838L, 6651299039L )
  
  // details() method is used to show
  // the details of all customer
  def details(): Unit ={
    println("Details of customer is\nNames of customer: " + name +
            "\nBalance list" + "is respectively: " + balance_current +
            "\nAccount number is respectively: " + account_number +
            "\nPhone number" + "is respectively: " + phone_number)
  }
  
  // Used to add money to a particular account
  def credit(): Unit = {
    var credit_amount: Int = 0
    println("Enter the account number you want to credit in: ")
  
    // readInt is used to take integer
    // value as input from user
    val acc_num1 = readInt()
  
    // indexOf() method returns the index
    // of particular element
    val index1 = account_number.indexOf(acc_num1)
    println("Enter the amount you want to credit: ")
    credit_amount = readInt()
    balance_current(index1) += credit_amount
    println("Amount added successfully\nNew Balance is: " +
             balance_current(index1))
  }
  
  // Used to withdraw money from an account
  def debit(): Unit ={
    var debit_amount : Int = 0
    println("Enter the account number " +
            "you want to withdraw from: ")
    val acc_num2 = readInt()
    val index2 = account_number.indexOf(acc_num2)
    println("Enter the amount you want to withdraw: ")
  
    debit_amount = readInt()
    balance_current(index2) -= debit_amount
    println("Money withdrawn successfully\n" +
            "Remaining balance is: " +
            balance_current(index2))
  }
}


Explanation:

ListBuffer: 
ListBuffer data type is used to store the values

debit():

amount_initially = 20000;
Input : 200

Output : 19800
// Initial amount was 20000. It asks to debit 200 from it 

// so final amount becomes
final = 20000 – 200 => 19800

credit():

amount_initially = 15000;

Input : 2000

Output : 17000

// Initial amount was 15000. It asks to credit 2000 in it so 

// final amount becomes
final = 15000 + 2000 => 17000
 

details():
Output : Details of all the customers

Input : Show the details of all account.

Output : 
Details of customer is

Names of customer: ListBuffer(Ankit, Rohit, Rahul)

Balance list is respectively: ListBuffer(20000, 30000, 40000)

Account number is respectively: ListBuffer(1234, 5678, 9101)

Phone number is respectively: ListBuffer(9998273493, 5569392838, 6651299039)
 

2) SavingAccount: It is the child class of Account class and contains a method called interest() and an override method called details(). Interest() method is used to show the interest amount of any particular account. It asks user to enter the account number and then uses the formula shown below to show the interest amount that particular account will get.

amount_initiallly + (amount_initially * interest) / 100

Details() method is an override method from class Account and is used here to show the details of any particular customer. Earlier it showed the details of all the customers. 

Scala




class SavingsAccount extends Account
{
  var interest : Double = 2
  
  // Method used to calculate interest
  def interest_amount(): Unit = {
    println("Enter the account number " +
            "to see the interest amount: ")
  
    val acc_num3 = readInt()
    val index3 = account_number.indexOf(acc_num3)
    interest =  (balance_current(index3) * 2) / 100
    println("The interest amount is: "+interest)
  }
  
  // Method used to view the details of
  // any particular account
  // using method overriding
  override def details(): Unit ={
    println("Enter the account_number to see its details: ")
    val acc_num4 = readInt()
    val index4 = account_number.indexOf(acc_num4)
  
    println("Details of this account is\n" +
            "Name of customer is: " + name(index4) +
            "\nAccount number is: " + account_number(index4) +
            "\nPhone number is: " + phone_number(index4)  +
            "\nAccount balance is: " + balance_current(index4))
  }
}


Explanation:

interest():

Input : Account number you want to see the interest amount of the month.
1234
Output : 400.0

// Interest rate is fixed initially as fixed by

// the bank. 400 is the interest amount.

details():

Input : Enter the account_number to see its details: 
1234

Output : Details of this account is

Name of customer is: Ankit

Account number is: 1234

Phone number is: 9998273493
Account balance is: 20000

// It shows the details(name, account number,

//  phone number and current balance) 

// of the account
asked by the user.
 

3) CurrentAccount: It is also a child class of Account class and contains an override method called debit(). This method is used in case when a user wants to withdraw money. It checks the overdraft limit which is nothing but the minimum limit set by the bank that requires every account to have the balance above that overdraft limit in order to avoid cash deduction. It uses the formula:

balance_current – withdrawn_amount < overdraft_limit

If this formula fails then the bank will decline to the with-drawl money request.

Scala




class CurrentAccount extends Account
{
  var overdraft_limit: Int = 2000
  
  // Using method overriding
  // method used to withdraw money
  override def debit(): Unit ={
    println("Enter the account number " +
            "you want to withdraw from: ")
  
    val acc_num5 = readInt()
    val index5 = account_number.indexOf(acc_num5)
    println("Enter the amount you want to withdraw: ")
    var debit_amount = readInt()
  
    if (balance_current(index5) - debit_amount < 2000)
    {
      println("Overdraft limit exceeded " +
              "transaction declined")
    }
    else
    {
      balance_current(index5) -= debit_amount
      println("Transaction successful\n" + "" +
              "Remaining balance is: " +
               balance_current(index5))
    }
  }
}


Explanation:

override debit():
 

// Takes the account number as input and checks the overdraft limit.

if (balance_current(index2) – debit_amount < overdraft_limit)

{

    println(“Overdraft limit exceeded”)

 } 

else

{

    balance_current(index2)  -= debit_amount
  

     println(“Remaining balance is: “+balance_current(index2))   

}

// If for example the current balance is 30000 and 

// overdraft limit is 5000 then you cannot 

// withdraw any amount greater than 25000.
 

4) Bank: This class also extends Account class and has 3 methods in it namely opening(), closing(). This class is used for opening, closing, and to find the dividend amount.

Opening() method is used to open any new account in the bank. It asks the user for its name, sets, and account number and asks user for initial balance. It then adds all this respectively to the different ListBuffer using ListBuffer add functionality.

List_Buffer +=  element

Closing() method is used to close any existing account and all the related details from ListBuffer. It asks the user to enter the account number and then checks the index of that particular account number using the indexOf() method of ListBuffer and then deletes that particular index from all the ListBuffer respectively.

List_Buffer -=  element

Scala




class Bank extends Account
{
  
  // Method to open an account
  def opening(): Unit ={
  
    // readLine() method is used to
    // take a string as input from user
    var new_name = readLine("Enter the name: ")
    name += new_name
    println("Enter the opening balance: ")
    var opening_balance = readInt()
    balance_current += opening_balance
    account_number += 1908
    println("Account added successfully")
  }
  
  // Method used to close an existing account
  def closing(): Unit ={
    println("Enter the account number: ")
    val acc_num6 = readInt()
    val index6 = account_number.indexOf(acc_num6)
    name -= name(index6)
    balance_current -= balance_current(index6)
    account_number -= account_number(index6)
    println("Account removed successfully")
  }
}


Explanation:

opening():

Input : Takes the name, account number and initial balance you want to deposit as input

Output : Account added successfully

// It asks user to enter the account number and 

// then checks the index of that particular account

// number using indexOf() method of ListBuffer 

// and then deletes that particular index 
from

// all the ListBuffer respectively.
 

closing():

Input : Takes the account number as input

Output : Account removed successfully

// It also uses the similar method showed above in opening() case.
 

5) Banking System/main class: This works as the main class and has objects and if-else statements. This class works as the main function and the code execution starts from here. It contains 4 objects one for each class and choice statement. The choice statement is used to make the code user-driven and menu-driven. The choice entered by the user will be checked and that particular functionality will be returned. For this, it uses an if-else statement.

It has 1 object for each class and if-else statements to perform different tasks.

Below is the implementation of the above problem:

Scala




// Scala program for Banking System
import scala.collection.mutable.ListBuffer
import scala.io.StdIn.{readInt, readLine}
  
class Account{
  
  // Creating four ListBuffer which stores
  // the initial details of customer
  var name = new ListBuffer[String]()
  name += ( "Ankit", "Rohit", "Rahul" )
  var balance_current = new ListBuffer[Int]()
  balance_current += ( 20000, 30000, 40000 )
  var account_number = new ListBuffer[Int]()
  account_number += ( 1234, 5678, 9101 )
  var phone_number = new ListBuffer[Long]()
  phone_number += ( 9998273493L, 5569392838L, 6651299039L )
  
  // details() method is used to show
  // the details of all customer
  def details(): Unit ={
    println("Details of customer is\nNames of customer: " + name +
            "\nBalance list" + "is respectively: " + balance_current +
            "\nAccount number is respectively: " + account_number +
            "\nPhone number" + "is respectively: " + phone_number)
  }
  
  // Used to add money to a particular account
  def credit(): Unit = {
    var credit_amount: Int = 0
    println("Enter the account number you want to credit in: ")
  
    // readInt is used to take integer
    // value as input from user
    val acc_num1 = readInt()
  
    // indexOf() method returns the index
    // of particular element
    val index1 = account_number.indexOf(acc_num1)
    println("Enter the amount you want to credit: ")
    credit_amount = readInt()
    balance_current(index1) += credit_amount
    println("Amount added successfully\nNew Balance is: " +
             balance_current(index1))
  }
  
  // Used to withdraw money from an account
  def debit(): Unit ={
    var debit_amount : Int = 0
    println("Enter the account number " +
            "you want to withdraw from: ")
    val acc_num2 = readInt()
    val index2 = account_number.indexOf(acc_num2)
    println("Enter the amount you want to withdraw: ")
  
    debit_amount = readInt()
    balance_current(index2) -= debit_amount
    println("Money withdrawn successfully\n" +
            "Remaining balance is: " +
            balance_current(index2))
  }
}
  
// Child class of class Account()
class SavingsAccount extends Account
{
  var interest : Double = 2
  
  // Method used to calculate interest
  def interest_amount(): Unit = {
    println("Enter the account number " +
            "to see the interest amount: ")
  
    val acc_num3 = readInt()
    val index3 = account_number.indexOf(acc_num3)
    interest =  (balance_current(index3) * 2) / 100
    println("The interest amount is: "+interest)
  }
  
  // Method used to view the details of
  // any particular account
  // using method overriding
  override def details(): Unit ={
    println("Enter the account_number to see its details: ")
    val acc_num4 = readInt()
    val index4 = account_number.indexOf(acc_num4)
  
    println("Details of this account is\n" +
            "Name of customer is: " + name(index4) +
            "\nAccount number is: " + account_number(index4) +
            "\nPhone number is: " + phone_number(index4)  +
            "\nAccount balance is: " + balance_current(index4))
  }
}
  
// Child class of class Account()
class CurrentAccount extends Account
{
  var overdraft_limit: Int = 2000
  
  // Using method overriding
  // method used to withdraw money
  override def debit(): Unit ={
    println("Enter the account number " +
            "you want to withdraw from: ")
  
    val acc_num5 = readInt()
    val index5 = account_number.indexOf(acc_num5)
    println("Enter the amount you want to withdraw: ")
    var debit_amount = readInt()
  
    if (balance_current(index5) - debit_amount < 2000)
    {
      println("Overdraft limit exceeded " +
              "transaction declined")
    }
    else
    {
      balance_current(index5) -= debit_amount
      println("Transaction successful\n" + "" +
              "Remaining balance is: " +
               balance_current(index5))
    }
  }
}
  
// Child class of class Account()
class Bank extends Account
{
  
  // Method to open an account
  def opening(): Unit ={
  
    // readLine() method is used to
    // take a string as input from user
    var new_name = readLine("Enter the name: ")
    name += new_name
    println("Enter the opening balance: ")
    var opening_balance = readInt()
    balance_current += opening_balance
    account_number += 1908
    println("Account added successfully")
  }
  
  // Method used to close an existing account
  def closing(): Unit ={
    println("Enter the account number: ")
    val acc_num6 = readInt()
    val index6 = account_number.indexOf(acc_num6)
    name -= name(index6)
    balance_current -= balance_current(index6)
    account_number -= account_number(index6)
    println("Account removed successfully")
  }
}
  
// Main class
object Banking_System
{
  def main(args: Array[String]): Unit = {
  
    // Object of all classes
    val obj1 = new Account()
    val obj2 = new SavingsAccount()
    val obj3 = new CurrentAccount()
    val obj4 = new Bank()
  
    println("Enter 1 for account details, " +
            "2 for SavingsAccount, " +
            "3 for CurrentAccount and " +
            "4 for closing or opening account")
  
    val choice = readInt()
    if (choice == 1)
    {
      obj1.details()
    }
  
    if (choice == 2)
    {
      println("Enter 1 for checking the interest " +
              "amount and 2 if you want to see the " +
              "details of any particular account: ")
  
      val choice1 = readInt()
      if (choice1 == 1)
      {
        obj2.interest_amount()
      }
      else if (choice == 2)
      {
        obj2.details()
      }
    }
  
    if(choice == 3)
    {
      println("Enter 1 for credit and 2 for debit: ")
      val choice2 = readInt()
  
      if (choice2 == 1)
      {
        obj3.credit()
      }
      if (choice2 == 2)
      {
        obj3.debit()
      }
    }
  
    if (choice == 4)
    {
      println("Enter 1 for opening account " +
              "and 2 for closing an account: ")
  
      val choice3 = readInt()
      if(choice3 == 1)
      {
        obj4.opening()
      }
      else
      {
        obj4.closing()
      }
    }
  }
}


Output:

1)Details:Enter 1 for account details, 2 for SavingsAccount, 3 for CurrentAccount 
and 4 for closing or opening account
1
Details of customer is
Names of customer: ListBuffer(Ankit, Rohit, Rahul)
Balance list is respectively: ListBuffer(20000, 30000, 40000)
Account number is respectively: ListBuffer(1234, 5678, 9101)
Phone number is respectively: ListBuffer(9998273493, 5569392838, 6651299039)
----------------------------------------------------------------------------------------

2)SavingsAccount: Input1 
Enter 1 for account details, 2 for SavingsAccount, 
3 for CurrentAccount and 4 for closing or opening account
2
Enter 1 for checking the interest amount and 2 if you want to see the details of any particular account: 
1
Enter the account number to see the interest amount: 
1234
The interest amount is: 400.0
Input2
Enter 1 for account details, 2 for SavingsAccount, 3 for CurrentAccount and 4 for closing or opening account
2
Enter 1 for checking the interest amount and 2 if you want to see the details of any particular account: 
2
Enter the account_number to see its details: 
1234
Details of this account is
Name of customer is: Ankit
Account number is: 1234
Phone number is: 9998273493
Account balance is: 20000
---------------------------------------------------------------------------------------

3)CurrentAccount: Input1
Enter 1 for account details, 2 for SavingsAccount, 3 for CurrentAccount and 4 for closing or opening account
3
Enter 1 for credit and 2 for debit: 
1
Enter the account number you want to credit in: 
1234
Enter the amount you want to credit: 
2000
Amount added successfully
New Balance is: 22000
Input2
Enter 1 for account details, 2 for SavingsAccount, 3 for CurrentAccount and 4 for closing or opening account
3
Enter 1 for credit and 2 for debit: 
2
Enter the account number you want to withdraw from: 
1234
Enter the amount you want to withdraw: 
2000
Transaction successful
Remaining balance is: 18000
---------------------------------------------------------------------------------------

4)Bank: Input1
Enter 1 for account details, 2 for SavingsAccount, 3 for CurrentAccount and 4 for closing or opening account
4
Enter 1 for opening account and 2 for closing an account: 
1
Enter the name: Anshul
Enter the opening balance: 
20000
Account added successfully
Input2
Enter 1 for account details, 2 for SavingsAccount, 3 for CurrentAccount and 4 for closing or opening account
4
Enter 1 for opening account and 2 for closing an account: 
2
Enter the account number: 
1234
Account removed successfully



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads