实验题目
实现更为复杂的透支保护机制
注释:这是练习 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] has a checking balance of 200.0 and a savings balance of 500.0
Checking Acct [Jane Simms] : withdraw 150.00 succeeds? true
Checking Acct [Jane Simms] : deposit 22.50 succeeds? true
Checking Acct [Jane Simms] : withdraw 147.62 succeeds? true
Customer [Simms, Jane] has a checking balance of 0.0 and a savings balance of 424.88
Customer [Bryant, Owen] has a checking balance of 200.0
Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? true
Checking Acct [Owen Bryant] : deposit 25.00 succeeds? true
Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? false
Customer [Bryant, Owen] has a checking balance of 125.0
代码
【Account.java】类
package banking;
public class Account {
protected double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public boolean withdraw(double amt){
if (amt < balance){
balance-=amt;
return true;
}else{
return false;
}
}
}
【Customer.java】类
package banking;
public class Customer {
private String firstName;
private String lastName;
private SavingsAccount savingsAccount = null; //储蓄账户
private CheckingAccount checkingAccount = null; //支票账户
private int numOfAccounts;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public void setSavings(SavingsAccount savingsAccount) {
this.savingsAccount = savingsAccount;
}
public void setChecking(CheckingAccount checkingAccount) {
this.checkingAccount = checkingAccount;
}
public SavingsAccount getSavings() {
return savingsAccount;
}
public CheckingAccount getChecking() {
return checkingAccount;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/**
* 获取 numOfAccounts
* @return numOfAccounts
*/
public int getNumOfAccounts() {
return numOfAccounts;
}
}
【Bank.java】类
package banking;
/**
* 银行类
*/
public class Bank {
private Customer[] customers; //Customer对象的数组
private int numberOfCustomer; //整数,跟踪下一个 customers 数组索引
/**
* 公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
*/
public Bank() {
customers = new Customer[10];
}
/**
* 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
* @param f 姓
* @param l 名
*/
public void addCustomer(String f,String l){
customers[numberOfCustomer++]=new Customer(f,l);
}
/**
* 通过下标索引获取 customer
* @param index 下标索引
* @return customer
*/
public Customer getCustomer(int index) {
return customers[index];
}
public int getNumOfCustomers() {
return numberOfCustomer;
}
}
【CheckingAccount.java】类
package banking;
public class CheckingAccount extends Account{
private SavingsAccount protectedBy=null;
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance, SavingsAccount protect) {
super(balance);
this.protectedBy = protect;
}
/**
* 此方法必须执行下列检查。如 果当前余额足够弥补取款 amount,则正常进行。
* 如果不够弥补但是存在透支保护,则尝试用 overdraftProtection 得值来弥补该差值(balance-amount).
* 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。
* @param amt 提款数目
* @return 返回true代表交易成功,否则交易失败
*/
@Override
public boolean withdraw(double amt){
if (balance < amt){
if(protectedBy==null){
return false;
}
if (amt-balance> protectedBy.getBalance()) {
return false;
}else {
protectedBy.balance-=amt-balance;
balance=0;
return true;
}
}else {
balance-=amt;
return true;
}
}
}
【SavingsAccount.java】类
package banking;
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(double balance, double interest_Rate) {
super(balance);
this.interestRate = interest_Rate;
}
}
【TestBanking.java】类
package banking;/*
* 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.
*/
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account;
// Create two customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
account = customer.getChecking();
customer.setSavings(new SavingsAccount(500.00, 0.05));
customer.setChecking(new CheckingAccount(200.00, customer.getSavings()));
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.setChecking(new CheckingAccount(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();
}
}