package com.zyedu.homework.homework08;
public class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
//存款
public void deposit(double amount) {
balance += amount;
}
//取款
public void withdraw(double amount) {
balance -= amount;
}
//get和setBalance方法
}
要求:
1.在BankAccount类的基础上扩展新类 CheckingAccount,对每次存款和取款都收取1美元的手续费;
2.在BankAccount类的基础上扩展新类 SavingAccount, 有每个月三次免手续费的存款和取款。在earnMonthlyInterest方法中 每个月都有利息产生,并且重置交易计数。
CheckingAccount.java——重写deposit和withdraw
super.deposit(amount - 1);//巧妙的调用了父类的deposit
super.withdraw(amount + 1);//巧妙地调用了父类的withdraw
package com.zyedu.homework.homework08;
public class CheckingAccount extends BankAccount{
public CheckingAccount(double initialBalance) {
super(initialBalance);
}
@Override
public void deposit(double amount) {
super.deposit(amount - 1);//巧妙的调用了父类的deposit
}
@Override
public void withdraw(double amount) {
super.withdraw(amount + 1);//巧妙地调用了父类的withdraw
}
}
CheckingAccount.java——重写deposit和withdraw
在此类中设置一个变量count,利息设置一个变量rate
package com.zyedu.homework.homework08;
/**
* 重写deposit()和withdraw()
*/
public class SavingsAccount extends BankAccount{
private int count = 3;
private double rate = 0.01;
public SavingsAccount(double initialBalance) {
super(initialBalance);
//在这个构造器中就不给count和rate值了
//可利用setCount和setRate赋值or修改值
}
public void earnMonthlyInterest() {
count = 3;
super.deposit(getBalance() * rate);
}
@Override
public void deposit(double amount) {
if (count > 0) {
super.deposit(amount);
} else {
super.deposit(amount - 1);
}
count--;
}
@Override
public void withdraw(double amount) {
if (count > 0) {
super.withdraw(amount);
} else {
super.withdraw(amount + 1);
}
count--;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}