使用Scala的银行系统
在这个项目中,我们将使用 Scala 构建一个简单的银行管理系统,它将执行以下任务:
1) 显示所有客户或特定客户的现有详细信息。
2) 添加或删除帐户
3) 存款或取款
4) 检查透支限额
5) 显示利息和股息金额
方法:
该程序使用了 Scala 中的继承、Scala 中的方法、If-else、方法覆盖、类和对象以及 ListBuffer 的概念。
有关上述概念的更多信息,请参阅以下链接。
a) Scala 中的继承: https://www.geeksforgeeks.org/inheritance-in-scala/
b) Scala 中的方法: https://www.geeksforgeeks.org/scala-functions-basics/
c) 方法覆盖: https://www.geeksforgeeks.org/method-overriding-in-scala/
d) 列表缓冲区:https://www.geeksforgeeks.org/scala-listbuffer/
该程序有 5 个类,分别命名为 Account、SavingAccount、CurrentAccount、Bank,以及主类 Banking_System。
1) Account:它是父类,其中包含三个方法,分别是debit()、credit()和 details() 。该类包含4个ListBuffer ,用于存储银行活期账户持有人的详细信息,按顺序存储,这样一个客户的银行账号索引及其对应的电话号码将存储在不同ListBuffer的相同索引值中。
如图所示,第一个客户详细信息将存储在所有 ListBuffer 的第一个索引中,第二个客户等也将存储。 Credit() 和 details() 方法稍后将用作覆盖方法以显示借方金额和详细信息任何客户。
下面的例子解释了它是如何工作的。
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))
}
}
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))
}
}
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))
}
}
}
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")
}
}
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()
}
}
}
}
解释:
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 :它是Account 类的子类,包含一个名为interest()的方法和一个名为details() 的覆盖方法。 Interest()方法用于显示任何特定帐户的利息金额。它要求用户输入帐号,然后使用下面显示的公式来显示特定帐户将获得的利息金额。
amount_initiallly + (amount_initially * 利息) / 100
Details()方法是来自类 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))
}
}
解释:
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:它也是Account 类的子类,包含一个名为debit()的覆盖方法。当用户想要提款时使用此方法。它检查透支限额,这是银行设定的最低限额,要求每个账户的余额都高于透支限额,以避免现金扣除。它使用以下公式:
balance_current –withdrawn_amount < overdraft_limit
如果此公式失败,则银行将拒绝提款请求。
斯卡拉
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))
}
}
}
解释:
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 :这个类也扩展了 Account 类,里面有 3 个方法,分别是opening() 和 closed() 。此类用于开仓、平仓和查找股息金额。
Opening()方法用于在银行开设任何新账户。它向用户询问其名称、集合和帐号,并询问用户初始余额。然后它使用 ListBuffer 添加功能将所有这些分别添加到不同的 ListBuffer。
List_Buffer += 元素
Closing()方法用于关闭任何现有帐户和 ListBuffer 中的所有相关详细信息。它要求用户输入帐号,然后使用 ListBuffer 的 indexOf() 方法检查该特定帐号的索引,然后分别从所有 ListBuffer 中删除该特定索引。
List_Buffer -= 元素
斯卡拉
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")
}
}
解释:
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)银行系统/主类:作为主类工作,有对象和if-else语句。这个类作为主函数,代码执行从这里开始。它包含 4 个对象,每个类和选择语句一个。选择语句用于使代码由用户驱动和菜单驱动。将检查用户输入的选择并返回该特定功能。为此,它使用 if-else 语句。
它为每个类和 if-else 语句提供 1 个对象来执行不同的任务。
下面是上述问题的实现:
斯卡拉
// 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()
}
}
}
}
输出:
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