Banking System using Scala
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.w3wiki.net/inheritance-in-scala/
b) Methods in Scala: https://www.w3wiki.net/scala-functions-basics/
c) Method Overriding: https://www.w3wiki.net/method-overriding-in-scala/
d) ListBuffer: https://www.w3wiki.net/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 + = ( 9998273493 L, 5569392838 L, 6651299039 L ) // 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 _ num 1 = readInt() // indexOf() method returns the index // of particular element val index 1 = account _ number.indexOf(acc _ num 1 ) println( "Enter the amount you want to credit: " ) credit _ amount = readInt() balance _ current(index 1 ) + = credit _ amount println( "Amount added successfully\nNew Balance is: " + balance _ current(index 1 )) } // 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 _ num 2 = readInt() val index 2 = account _ number.indexOf(acc _ num 2 ) println( "Enter the amount you want to withdraw: " ) debit _ amount = readInt() balance _ current(index 2 ) - = debit _ amount println( "Money withdrawn successfully\n" + "Remaining balance is: " + balance _ current(index 2 )) } } |
Explanation:
ListBuffer:
ListBuffer data type is used to store the valuesdebit():
amount_initially = 20000;
Input : 200Output : 19800
// Initial amount was 20000. It asks to debit 200 from it// so final amount becomes
final = 20000 – 200 => 19800credit():
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 customersInput : Show the details of all account.
Output :
Details of customer isNames 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 _ num 3 = readInt() val index 3 = account _ number.indexOf(acc _ num 3 ) interest = (balance _ current(index 3 ) * 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 _ num 4 = readInt() val index 4 = account _ number.indexOf(acc _ num 4 ) println( "Details of this account is\n" + "Name of customer is: " + name(index 4 ) + "\nAccount number is: " + account _ number(index 4 ) + "\nPhone number is: " + phone _ number(index 4 ) + "\nAccount balance is: " + balance _ current(index 4 )) } } |
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:
1234Output : 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 _ num 5 = readInt() val index 5 = account _ number.indexOf(acc _ num 5 ) println( "Enter the amount you want to withdraw: " ) var debit _ amount = readInt() if (balance _ current(index 5 ) - debit _ amount < 2000 ) { println( "Overdraft limit exceeded " + "transaction declined" ) } else { balance _ current(index 5 ) - = debit _ amount println( "Transaction successful\n" + "" + "Remaining balance is: " + balance _ current(index 5 )) } } } |
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 _ num 6 = readInt() val index 6 = account _ number.indexOf(acc _ num 6 ) name - = name(index 6 ) balance _ current - = balance _ current(index 6 ) account _ number - = account _ number(index 6 ) 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 + = ( 9998273493 L, 5569392838 L, 6651299039 L ) // 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 _ num 1 = readInt() // indexOf() method returns the index // of particular element val index 1 = account _ number.indexOf(acc _ num 1 ) println( "Enter the amount you want to credit: " ) credit _ amount = readInt() balance _ current(index 1 ) + = credit _ amount println( "Amount added successfully\nNew Balance is: " + balance _ current(index 1 )) } // 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 _ num 2 = readInt() val index 2 = account _ number.indexOf(acc _ num 2 ) println( "Enter the amount you want to withdraw: " ) debit _ amount = readInt() balance _ current(index 2 ) - = debit _ amount println( "Money withdrawn successfully\n" + "Remaining balance is: " + balance _ current(index 2 )) } } // 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 _ num 3 = readInt() val index 3 = account _ number.indexOf(acc _ num 3 ) interest = (balance _ current(index 3 ) * 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 _ num 4 = readInt() val index 4 = account _ number.indexOf(acc _ num 4 ) println( "Details of this account is\n" + "Name of customer is: " + name(index 4 ) + "\nAccount number is: " + account _ number(index 4 ) + "\nPhone number is: " + phone _ number(index 4 ) + "\nAccount balance is: " + balance _ current(index 4 )) } } // 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 _ num 5 = readInt() val index 5 = account _ number.indexOf(acc _ num 5 ) println( "Enter the amount you want to withdraw: " ) var debit _ amount = readInt() if (balance _ current(index 5 ) - debit _ amount < 2000 ) { println( "Overdraft limit exceeded " + "transaction declined" ) } else { balance _ current(index 5 ) - = debit _ amount println( "Transaction successful\n" + "" + "Remaining balance is: " + balance _ current(index 5 )) } } } // 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 _ num 6 = readInt() val index 6 = account _ number.indexOf(acc _ num 6 ) name - = name(index 6 ) balance _ current - = balance _ current(index 6 ) account _ number - = account _ number(index 6 ) println( "Account removed successfully" ) } } // Main class object Banking _ System { def main(args : Array[String]) : Unit = { // Object of all classes val obj 1 = new Account() val obj 2 = new SavingsAccount() val obj 3 = new CurrentAccount() val obj 4 = 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 ) { obj 1 .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 choice 1 = readInt() if (choice 1 == 1 ) { obj 2 .interest _ amount() } else if (choice == 2 ) { obj 2 .details() } } if (choice == 3 ) { println( "Enter 1 for credit and 2 for debit: " ) val choice 2 = readInt() if (choice 2 == 1 ) { obj 3 .credit() } if (choice 2 == 2 ) { obj 3 .debit() } } if (choice == 4 ) { println( "Enter 1 for opening account " + "and 2 for closing an account: " ) val choice 3 = readInt() if (choice 3 == 1 ) { obj 4 .opening() } else { obj 4 .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