最新c银行系统编程汇总.docx
《最新c银行系统编程汇总.docx》由会员分享,可在线阅读,更多相关《最新c银行系统编程汇总.docx(11页珍藏版)》请在冰豆网上搜索。
最新c银行系统编程汇总
c银行系统编程
3、在2的基础上,实现下面的类图:
Step1:
创建Account类,作为各种账户的基类。
然后用继承来创建两个专用的账户类:
SavingAccount(储蓄账户)类和CheckingAccount(支票账户)类。
(1)SavingAccount类,从Account派生
其新特征:
A.储蓄账户能获得利息,增加一个属性interestRate表示利率。
B.着时间的推移,储蓄账户可以获得利息,所以为SavingAccount类增加一个addInterest()方法(注:
未在UML图中画出),用于把利息增加到原balance上。
利息的计算规则为interestRate*balance;
(2)CheckingAccount类,从Account派生
新特征:
A.帐户允许有透支额度,增加属性overdraftProtection表示最大透支额度。
B.取款规则发生了改变:
如果当前余额足够支付要提取的金额amount,按照常规进行处理。
如果当前余额不够,但帐户有透支额度,那么所差的部分作为透支处理。
透支处理规则:
比较amount(当前提款金额)和balance(当前帐户余额)
若当前透支额amount-balance>overdraftProtection,那么整个交易应该放弃,提款失败。
否则提款后的balance应为0,提款后的最大透支额度overdraftProtection应该为原有最大透支额度减去(amount-balance)。
(3)再看Account类
其withdraw行为要在其具体的子类中才能确定,故将其withdraw方法设计成一个抽象方法,由子类去改写。
因此Account类也应重新设计成一个抽象类。
Step2:
具体实现参考:
1、和上一题相比,Account类中的balance属性的访问控制修饰符变成了protected。
其withdraw方法变成一个抽象方法,因此Account类是一个抽象类。
2、SavingsAccount类设计
1)定义一个double型的数据属性interestRate(利率)。
2)定义一个带有两个参数balance和interest_rate的构造方法。
这个构造方法通过super(balance)调用父类的构造方法完成对balance属性的初始化。
3)增加一个addInterest()方法,将所获利息加到balance上。
4)实现父类中的withdraw抽象方法。
3、CheckingAccount类
1)定义一个double型的属性(最大透支额度)。
2)定义一个带有一个balance参数的构造方法。
这个构造方法通过super(balance)调用父类的构造方法。
3)定义另外一个带有两个参数的构造方法。
这个构造方法通过super(balance)调用父类的构造方法,并且对属性overdraftProtection进行设置。
4)对成员方法withdraw进行改写,改写规则见Step1。
Step3:
仿照银行和客户之间的关系修改Customer类,使一个客户可以有多个帐号。
Step4:
编写测试程序,首先添加四个客户到银行,并分别为其添加帐号。
打印报表。
并对第一个客户的各个帐户进行操作以验证SavingsAccount和CheckingAccount类中withdraw方法的正确性。
测试程序最终输出的结果如下:
CustomerReports
================================
Customer:
JaneSmith
SavingsAccount:
Currentbalanceis500.0Interestrate=0.03
CheckingAccount:
Currentbalanceis500.0OverdraftProtection=0.0
CheckingAccount:
Currentbalanceis500.0OverdraftProtection=500.0
Customer:
OwenBryant
CheckingAccount:
Currentbalanceis200.0OverdraftProtection=0.0
Customer:
TimSoley
SavingsAccount:
Currentbalanceis1500.0Interestrate=0.03
CheckingAccount:
Currentbalanceis200.0OverdraftProtection=0.0
Customer:
MariaSoley
CheckingAccount:
Currentbalanceis200.0OverdraftProtection=0.0
SavingsAccount:
Currentbalanceis150.0Interestrate=0.03
TestingaccountofcustomerNo.1...
Withdraw300.00true
Withdraw300.00false
BalanceofaccountNO.1is200.0
Withdraw300.00true
Withdraw300.00false
BalanceofaccountNO.2is200.0
Withdraw300.00true
Withdraw300.00true
BalanceofaccountNO.3is0.0
代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceConsoleApplication2
{
publicabstractclassAccount
{
protecteddoublebalance;
publicAccount(doublebalance)
{
this.balance=balance;
}
publicdoublegetBalance()
{
returnbalance;
}
publicvoiddeposit(doubleamount)
{
balance+=amount;
}
publicabstractvoidwithdraw(doubleamount);
}
publicclassSavingsAccount:
Account
{
privatedoubleinterestRate;
publicSavingsAccount(doubleb,doubleir)
:
base(b)
{
interestRate=ir;
}
publicoverridevoidwithdraw(doubleamount)
{
if(balance>amount)
{
balance-=amount;
//returntrue;
}
else
{
Console.WriteLine("余额不足");
//returnfalse;
}
}
}
publicclassCheckingAccount:
Account
{
privatedoubleoverdraftProtection;
publicCheckingAccount(doubleb)
:
base(b)
{
}
publicCheckingAccount(doubleb,doubleop)
:
base(b)
{
overdraftProtection=op;
}
publicoverridevoidwithdraw(doubleamount)
{
if(balance-amount>0)
balance=balance-amount;
else
{
if(balance+overdraftProtection>amount)
{
overdraftProtection-=amount-balance;
balance=0;
}
elseConsole.WriteLine("超支");
}
}
publicclassCustomer
{
privateintnumberOfSavingsAccount,numberOfCheckingAccount;
privatestringfirstName;
privatestringlastName;
//privateAccountaccount;
privateSavingsAccount[]savingsaccount=newSavingsAccount[10];
privateCheckingAccount[]checkingaccount=newCheckingAccount[10];
publicCustomer(stringfirstName,stringlastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
publicvoidaddSavingsAccount(SavingsAccountacct)
{
savingsaccount[numberOfSavingsAccount++]=acct;
}
publicvoidaddCheckingAccount(CheckingAccountacct)
{
checkingaccount[numberOfCheckingAccount++]=acct;
}
publicSavingsAccountgetSavingsAccount(intindex)
{
returnsavingsaccount[index];
}
publicCheckingAccountgetCheckingAccount(intindex)
{
returncheckingaccount[index];
}
publicintgetNumberOfSavingsAccount()
{
returnnumberOfSavingsAccount;
}
publicintgetNumberOfCheckingAccount()
{
returnnumberOfCheckingAccount;
}
publicStringgetFirstName()
{
returnfirstName;
}
publicStringgetLastName()
{
returnlastName;
}
}
publicclassBank
{
privateCustomer[]customers;
privateintnumberOfCustomers;
publicBank()
{
customers=newCustomer[100];
numberOfCustomers=0;
}
publicvoidaddCustomer(stringf,stringl)
{
customers[numberOfCustomers++]=newCustomer(f,l);
}
publicintgetNumOfCustomers()
{
returnnumberOfCustomers;
}
publicCustomergetCustomer(intindex)
{
returncustomers[index];
}
}
publicclassTesting
{
publicstaticvoidMain(String[]args)
{
Bankbank=newBank();
bank.addCustomer("Simms","Jane");
bank.addCustomer("Bryant","Owen");
bank.addCustomer("Soley","Tim");
bank.addCustomer("Soley","Maria");
for(inti=0;i{
Customerc=bank.getCustomer(i);
Console.WriteLine("Customer["+(i+1)+"]is"+
c.getFirstName()+","+c.getLastName());
if(i==0)
{
bank.getCustomer(0).addSavingsAccount(newSavingsAccount(500.0,0.03));
Console.WriteLine("\tSavingsAccount:
Currentbalanceis500Interestrate=0.03");
bank.getCustomer(0).addCheckingAccount(newCheckingAccount(500.0,0.0));
Console.WriteLine("\tCheckingAccount:
Currentbalanceis"+c.getCheckingAccount(0).balance+"OverdraftProtection="+c.getCheckingAccount(0).overdraftProtection);
bank.getCustomer(0).addCheckingAccount(newCheckingAccount(500.0,500.0));
Console.WriteLine("\tCheckingAccount:
Currentbalanceis"+c.getCheckingAccount
(1).balance+"OverdraftProtection="+c.getCheckingAccount
(1).overdraftProtection);
}
if(i==1)
{
bank.getCustomer
(1).addCheckingAccount(newCheckingAccount(200.0,0.0));
Console.WriteLine("\tCheckingAccount:
Currentbalanceis"+c.getCheckingAccount(0).balance+"OverdraftProtection="+c.getCheckingAccount(0).overdraftProtection);
}
if(i==2)
{
bank.getCustomer
(2).addSavingsAccount(newSavingsAccount(1500.0,0.03));
Console.WriteLine("\tSavingsAccount:
Currentbalanceis1500Interestrate=0.03");
bank.getCustomer
(2).addCheckingAccount(newCheckingAccount(200.0,0.0));
Console.WriteLine("\tCheckingAccount:
Currentbalanceis"+c.getCheckingAccount(0).balance+"OverdraftProtection="+c.getCheckingAccount(0).overdraftProtection);
}
if(i==3)
{
bank.getCustomer(3).addCheckingAccount(newCheckingAccount(200.0,0.0));
Console.WriteLine("\tCheckingAccount:
Currentbalanceis"+c.getCheckingAccount(0).balance+"OverdraftProtection="+c.getCheckingAccount(0).overdraftProtection);
bank.getCustomer(3).addSavingsAccount(newSavingsAccount(150.0,0.03));
Console.WriteLine("\tSavingsAccount:
Currentbalanceis200.0Interestrate=0.03");
}
}
Console.WriteLine("\nTestingaccountofcustomerNo.1...");
Customercc=bank.getCustomer(0);
Accountacct=cc.getSavingsAccount(0);
Console.WriteLine("Withdraw300.0");
acct.withdraw(300.0);
Console.WriteLine("Withdraw300.0");
acct.withdraw(300.0);
Console.WriteLine("BalanceofaccountNo.1is"+acct.getBalance());
acct=cc.getCheckingAccount(0);
Console.WriteLine("Withdraw300.0");
acct.withdraw(300.0);
Console.WriteLine("Withdraw300.0");
acct.withdraw(300.0);
Console.WriteLine("BalanceofaccountNo.2is"+acct.getBalance());
acct=cc.getCheckingAccount
(1);
Console.WriteLine("Withdraw300.0");
acct.withdraw(300.0);
Console.WriteLine("Withdraw300.0");
acct.withdraw(300.0);
Console.WriteLine("BalanceofaccountNo.3is"+acct.getBalance());
}
}
}
}