练习1、在银行项目中创建Account的两个子类:SavingAccount和CheckingAccount
实训目的:
继承、多态、方法的重写。
提示:
创建Account 类的两个子类:SavingAccount和CheckingAccount子类
a. 修改Account 类;将balance 属性的访问方式改为protected
b. 创建SavingAccount类,该类继承Account 类
c. 该类必须包含一个类型为double 的interestRate属性
d. 该类必须包括带有两个参数(balance 和interest_rate)的公有构造器。该构造器必须通过调用super(balance)将balance 参数传递给父类构造器。
实现CheckingAccount类
1.CheckingAccount类必须扩展Account 类
2.该类必须包含一个类型为double 的overdraftProtection属性。
3.该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调用super(balance)将balance 参数传递给父类构造器。
4.给类必须包括另一个带有两个参数(balance 和protect)的公有构造器。该构造器必须通过调用super(balance)并设置overdragtProtection属性,将balance 参数传递给父类构造器。
5.CheckingAccount类必须覆盖withdraw 方法。此方法必须执行下列检查。如果当前余额足够弥补取款amount,则正常进行。如果不够弥补但是存在透支保护,则尝试用overdraftProtection得值来弥补该差值(balance-amount)。如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。
6.编写并执行TestBanking程序。
//修改Account 类;将balance 属性的访问方式改为protected
SavingAccount类:
package banking;
public class SavingAccount extends Account {
//声明一个私有变量interestRate表示利率
private double interestRate;
public SavingAccount(double balance,double interestRate) {
super(balance);
this.interestRate =interestRate;
}
}
CheckingAccount类:
package banking;
public class CheckingAccount extends Account{
//声明一个变量overdraftProtection用来表示可以透支的额度
private double overdraftProtection;
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance,double overdraftProtection) {
super(balance);
this.overdraftProtection = overdraftProtection;
}
@Override
public boolean withdraw(double amt) {
// 如果当前余额足够弥补取款amount,则正常进行
// 如果不够弥补但是存在透支保护,
// 则尝试用overdraftProtection得值来弥补该差值(balance-amount)
if (balance >= amt) {
balance-=amt;
return true;
}else if (overdraftProtection +balance>= amt){
amt -=balance;
overdraftProtection -=amt;
// 对balance设置值为0
this.balance = 0;
return true;
}
return false;
}
}
package banking;
import org.omg.CORBA.PRIVATE_MEMBER;
public class TestBanking5 {
public static void main(String[] args) {
// 创建一个bank对象
Bank bank = new Bank();
Account account;
Customer customer;
// 用来记录每次账户操作的结果
boolean result;
//1.针对每个客户, 首先先在Bank()中进行构造--调用addCustomer()方法实现新建客户
// 然后 bank.getCustomer() 返回该customer 对象;
//2.接着声明一个账户account类 ,调用方法 customer.setAccount()设置其本人的账户;
//3.通过步骤1-2,把bank类,customer类和account类的关系链接到了一起
bank.addCustomer("Jane", "Smith");
account = new SavingAccount(500.00, 0.03);
customer = bank.getCustomer(0);
customer.setAccount(account);
System.out.println("Creating the customer Jane Smith.");
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
bank.addCustomer("Owen", "Bryant");
System.out.println("Creating the customer Owen Bryant.");
account = new CheckingAccount(500.00,0);
customer = bank.getCustomer(1);
customer.setAccount(account);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
bank.addCustomer("Tim", "Soley");
account = new CheckingAccount(500.00, 500.00);
customer = bank.getCustomer(2);
customer.setAccount(account);
System.out.println("Creating the customer Tim Soley");
System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
account = bank.getCustomer(2).getAccount();
customer.setAccount(account);
System.out.println("Creating the customer Maria Soley.");
System.out.println("Maria shares her Checking Account with her husband Tim.");
System.out.println("Retrieving the customer Jane Smith with her savings account.");
customer = bank.getCustomer(0);
// 取钱
result = customer.getAccount().withdraw(150.0);
System.out.println("Withdraw 150.00: "+result);
// 存钱
result = customer.getAccount().deposit(22.50);
System.out.println("Deposit 22.50: "+result);
// 取钱
result = customer.getAccount().withdraw(47.62);
System.out.println("Withdraw 47.62: "+result);
result = customer.getAccount().withdraw(400.0);
System.out.println("Withdraw 400.00: "+result);
System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
+"] has a balance of "+customer.getAccount().getBalance());
// 操作Owen Bryant的账户
System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
customer = bank.getCustomer(1);
result = customer.getAccount().withdraw(150.0);
System.out.println("Withdraw 150.00: "+result);
result = customer.getAccount().deposit(22.50);
System.out.println("Deposit 22.50: "+result);
result = customer.getAccount().withdraw(47.62);
System.out.println("Withdraw 47.62: "+result);
result = customer.getAccount().withdraw(400.0);
System.out.println("Withdraw 400.00: "+result);
System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
+"] has a balance of "+customer.getAccount().getBalance());
// 操作Tim Soley的账户
System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
customer = bank.getCustomer(2);
result = customer.getAccount().withdraw(150.0);
System.out.println("Withdraw 150.00: "+result);
result = customer.getAccount().deposit(22.50);
System.out.println("Deposit 22.50: "+result);
result = customer.getAccount().withdraw(47.62);
System.out.println("Withdraw 47.62: "+result);
result = customer.getAccount().withdraw(400.0);
System.out.println("Withdraw 400.00: "+result);
System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
+"] has a balance of "+customer.getAccount().getBalance());
// Maria Soley使用她丈夫的账户
System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
customer = bank.getCustomer(3);
result = customer.getAccount().deposit(150.0);
System.out.println("Deposit 150.0: "+result);
result = customer.getAccount().withdraw(750.0);
System.out.println("Withdraw 750.0: "+result);
System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
+"] has a balance of "+customer.getAccount().getBalance());
}
}