您好,欢迎访问三七文档
13、在2的基础上,实现下面的类图:Step1:创建Account类,作为各种账户的基类。然后用继承来创建两个专用的账户类:SavingAccount(储蓄账户)类和CheckingAccount(支票账户)类。(1)SavingAccount类,从Account派生其新特征:A.储蓄账户能获得利息,增加一个属性interestRate表示利率。B.着时间的推移,储蓄账户可以获得利息,所以为SavingAccount类增加一个addInterest()方法(注:未在UML图中画出),用于把利息增加到原balance上。利息的计算规则为interestRate*balance;(2)CheckingAccount类,从Account派生新特征:A.帐户允许有透支额度,增加属性overdraftProtection表示最大透支额度。B.取款规则发生了改变:如果当前余额足够支付要提取的金额amount,按照常规进行处理。如果当前余额不够,但帐户有透支额度,那么所差的部分作为透支处理。2透支处理规则:比较amount(当前提款金额)和balance(当前帐户余额)若当前透支额amount-balanceoverdraftProtection,那么整个交易应该放弃,提款失败。否则提款后的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方法的正确性。3测试程序最终输出的结果如下:CustomerReports================================Customer:JaneSmithSavingsAccount:Currentbalanceis500.0Interestrate=0.03CheckingAccount:Currentbalanceis500.0OverdraftProtection=0.0CheckingAccount:Currentbalanceis500.0OverdraftProtection=500.0Customer:OwenBryantCheckingAccount:Currentbalanceis200.0OverdraftProtection=0.0Customer:TimSoleySavingsAccount:Currentbalanceis1500.0Interestrate=0.03CheckingAccount:Currentbalanceis200.0OverdraftProtection=0.0Customer:MariaSoleyCheckingAccount:Currentbalanceis200.0OverdraftProtection=0.0SavingsAccount:Currentbalanceis150.0Interestrate=0.03TestingaccountofcustomerNo.1...Withdraw300.00trueWithdraw300.00falseBalanceofaccountNO.1is200.0Withdraw300.00trueWithdraw300.00falseBalanceofaccountNO.2is200.0Withdraw300.00trueWithdraw300.00trueBalanceofaccountNO.3is0.0代码usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceConsoleApplication24{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(balanceamount){balance-=amount;//returntrue;5}else{Console.WriteLine(余额不足);//returnfalse;}}}publicclassCheckingAccount:Account{privatedoubleoverdraftProtection;publicCheckingAccount(doubleb):base(b){}publicCheckingAccount(doubleb,doubleop):base(b){overdraftProtection=op;}publicoverridevoidwithdraw(doubleamount){if(balance-amount0)balance=balance-amount;else{if(balance+overdraftProtectionamount){overdraftProtection-=amount-balance;balance=0;}elseConsole.WriteLine(超支);}}6publicclassCustomer{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()7{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];}8}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;ibank.getNumOfCustomers();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.Wr
本文标题:c#银行系统编程
链接地址:https://www.777doc.com/doc-3743321 .html