实验题目
实现更为复杂的透支保护机制
注释:这是练习 1 的选择练习。它包括了更为复杂的透支保护机制模型。它使用 客户储蓄。它使用客户储蓄账户完成透支保护。本练习必须在完成上述两个练 习后进行。
实验目的
继承、多态、方法的重写。
说明
修改 SavingAccount 类
- 仿照练习 1“Account 类的两个子类”一节实现 SavingsAccount 类。
- SavingAccount 类必须扩展 Account 类。
- 该类必须包含一个类型为 double 的 interestRate 属性。
- 该类必须包括一个带有两个参数(balance和interest_rate)的公有构造器。 该构造器必须通过调用 super(balance)来将 balance 参数传递给父类构造器
修改 CheckingAccount 类
- 仿照练习 1“Account 类的两个子类”一节实现 CheckingAccount 类。
- CheckingAccount 类必须扩展 Account 类
- 该类必须包括一个关联属性,称作 protectedBy,表示透支保护。该属性的 类型为 SavingAccount,缺省值必须是 null。除此之外该类没有其他的数据属 性。
- 该类必须包括一个带有参数(balance)的公有构造器,该构造器必须通过调 用 super(balance)将 balance 参数传递到父类构造器。
- 修 改 构 造 器 为 CheckingAccount(double balance,SavingAccount protect)构造器。该构造器必须通过调用 super(balance)将 balance 参数 传递给父类构造器。
- CheckingAccount 类必须覆盖 withdraw 方法。该类必须执行下面的检查: 如果当前余额足够弥补取款 amount,则正常进行。如果不够弥补但是存在透支 保护则尝试用储蓄账户来弥补这个差值(balance-amount)。如果后一个交易 失败,则整个交易一定失败,但余额未受影响。
修改 Customer 类,使其拥有两个账户:一个储蓄账户和一个支票账户:两个 都是可选的。
- 在练习 5_续 1 修改,原来的 Customer 类包含一个称作 account 的关联属 性,可用该属性控制一个 Account 对象。重写这个类,使其包含两个关联属性: savingAccount 和 checkingAccount,这两个属性的缺省值是 null 。
- 包含两个访问方法:getSaving 和 getChecking,这两个方法分别返回储蓄 和支票总数。
- 包含两个相反的方法:SetSaving 和 setChecking,这两个方法分别为 savingAccount 和 checkingAccount 这两个关联属性赋值。
完成 TestBanking 程序。结果如下:
Customer [Simms, Jane] hasacheckingbalanceof200.0andasavingsbalanceof500.0CheckingAcct [JaneSimms] : withdraw150.00succeeds?trueCheckingAcct [JaneSimms] : deposit22.50succeeds?trueCheckingAcct [JaneSimms] : withdraw147.62succeeds?trueCustomer [Simms, Jane] hasacheckingbalanceof0.0andasavingsbalanceof424.88Customer [Bryant, Owen] hasacheckingbalanceof200.0CheckingAcct [OwenBryant] : withdraw100.00succeeds?trueCheckingAcct [OwenBryant] : deposit25.00succeeds?trueCheckingAcct [OwenBryant] : withdraw175.00succeeds?falseCustomer [Bryant, Owen] hasacheckingbalanceof125.0
代码
【Account.java】类
packagebanking; publicclassAccount { protecteddoublebalance; //银行帐户的当前(或即时)余额//公有构造器 ,这个参数为 balance 属性赋值publicAccount(doubleinit_balance) { this.balance=init_balance; } //用于获取经常余额publicdoublegetBalance() { returnbalance; } /*** 向当前余额增加金额* @param amt 增加金额* @return 返回 true(意味所有存款是成功的)*/publicbooleandeposit(doubleamt){ balance+=amt; returntrue; } /*** 从当前余额中减去金额* @param amt 提款数目* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。*/publicbooleanwithdraw(doubleamt){ if (amt<balance){ balance-=amt; returntrue; }else{ returnfalse; } } }
【Customer.java】类
packagebanking; publicclassCustomer { privateStringfirstName; privateStringlastName; privateSavingsAccountsavingsAccount=null; //储蓄账户privateCheckingAccountcheckingAccount=null; //支票账户privateintnumOfAccounts; publicCustomer(Stringf, Stringl) { this.firstName=f; this.lastName=l; } publicvoidsetSavings(SavingsAccountsavingsAccount) { this.savingsAccount=savingsAccount; } publicvoidsetChecking(CheckingAccountcheckingAccount) { this.checkingAccount=checkingAccount; } publicSavingsAccountgetSavings() { returnsavingsAccount; } publicCheckingAccountgetChecking() { returncheckingAccount; } publicStringgetFirstName() { returnfirstName; } publicStringgetLastName() { returnlastName; } /*** 获取 numOfAccounts* @return numOfAccounts*/publicintgetNumOfAccounts() { returnnumOfAccounts; } }
【Bank.java】类
packagebanking; /*** 银行类*/publicclassBank { privateCustomer[] customers; //Customer对象的数组privateintnumberOfCustomer; //整数,跟踪下一个 customers 数组索引/*** 公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。*/publicBank() { customers=newCustomer[10]; } /*** 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。* @param f 姓* @param l 名*/publicvoidaddCustomer(Stringf,Stringl){ customers[numberOfCustomer++]=newCustomer(f,l); } /*** 通过下标索引获取 customer* @param index 下标索引* @return customer*/publicCustomergetCustomer(intindex) { returncustomers[index]; } publicintgetNumOfCustomers() { returnnumberOfCustomer; } }
【CheckingAccount.java】类
packagebanking; publicclassCheckingAccountextendsAccount{ privateSavingsAccountprotectedBy=null; publicCheckingAccount(doublebalance) { super(balance); } publicCheckingAccount(doublebalance, SavingsAccountprotect) { super(balance); this.protectedBy=protect; } /*** 此方法必须执行下列检查。如 果当前余额足够弥补取款 amount,则正常进行。* 如果不够弥补但是存在透支保护,则尝试用 overdraftProtection 得值来弥补该差值(balance-amount).* 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。* @param amt 提款数目* @return 返回true代表交易成功,否则交易失败*/publicbooleanwithdraw(doubleamt){ if (balance<amt){ if(protectedBy==null){ returnfalse; } if (amt-balance>protectedBy.getBalance()) { returnfalse; }else { protectedBy.balance-=amt-balance; balance=0; returntrue; } }else { balance-=amt; returntrue; } } }
【SavingsAccount.java】类
packagebanking; publicclassSavingsAccountextendsAccount{ privatedoubleinterestRate; publicSavingsAccount(doublebalance, doubleinterest_Rate) { super(balance); this.interestRate=interest_Rate; } }
【TestBanking.java】类
packagebanking;/** This class creates the program to test the banking classes.* It creates a new Bank, sets the Customer (with an initial balance),* and performs a series of transactions with the Account object.*/importbanking.*; publicclassTestBanking { publicstaticvoidmain(String[] args) { Bankbank=newBank(); Customercustomer; Accountaccount; // Create two customers and their accountsbank.addCustomer("Jane", "Simms"); customer=bank.getCustomer(0); account=customer.getChecking(); customer.setSavings(newSavingsAccount(500.00, 0.05)); customer.setChecking(newCheckingAccount(200.00, customer.getSavings())); bank.addCustomer("Owen", "Bryant"); customer=bank.getCustomer(1); customer.setChecking(newCheckingAccount(200.00)); // Test the checking account of Jane Simms (with overdraft protection)customer=bank.getCustomer(0); System.out.println("Customer ["+customer.getLastName() +", "+customer.getFirstName() +"]"+" has a checking balance of "+customer.getChecking().getBalance() +" and a savings balance of "+customer.getSavings().getBalance()); account=customer.getChecking(); System.out.println("Checking Acct [Jane Simms] : withdraw 150.00 succeeds? "+account.withdraw(150.00)); System.out.println("Checking Acct [Jane Simms] : deposit 22.50 succeeds? "+account.deposit(22.50)); System.out.println("Checking Acct [Jane Simms] : withdraw 147.62 succeeds? "+account.withdraw(147.62)); System.out.println("Customer ["+customer.getLastName() +", "+customer.getFirstName() +"]"+" has a checking balance of "+account.getBalance() +" and a savings balance of "+customer.getSavings().getBalance()); System.out.println(); // Test the checking account of Owen Bryant (without overdraft protection)customer=bank.getCustomer(1); account=customer.getChecking(); System.out.println("Customer ["+customer.getLastName() +", "+customer.getFirstName() +"]"+" has a checking balance of "+account.getBalance()); System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? "+account.withdraw(100.00)); System.out.println("Checking Acct [Owen Bryant] : deposit 25.00 succeeds? "+account.deposit(25.00)); System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? "+account.withdraw(175.00)); System.out.println("Customer ["+customer.getLastName() +", "+customer.getFirstName() +"]"+" has a checking balance of "+account.getBalance()); System.out.println(); } }