1.面向对象练习
package com.logic.homework;
//要求:(1)在上面类的基础上扩展新类CheckingAccount对每次存款和取款都收取1美元的手续费
//(2)扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用)
// ,并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数
// (3)体会重写的好处
public class Homework08 {
public static void main(String[] args) {
CheckingAccount checkingAccount = new CheckingAccount(1000);
checkingAccount.deposit(10);
checkingAccount.withdraw(20);
System.out.println(checkingAccount.getBalance());
//测试SavingsAccount
SavingsAccount savingsAccount = new SavingsAccount(1000);
savingsAccount.deposit(100);
savingsAccount.deposit(100);
savingsAccount.deposit(100);
System.out.println(savingsAccount.getBalance());
savingsAccount.deposit(100);
System.out.println(savingsAccount.getBalance());
//月初,定时器自动调用一下earnMonthlyInterest方法
savingsAccount.earnMonthlyInterest();
System.out.println(savingsAccount.getBalance());
savingsAccount.deposit(1000);
savingsAccount.deposit(1000);
savingsAccount.deposit(1000);
savingsAccount.deposit(1000);
System.out.println(savingsAccount.getBalance());
}
}
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;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
package com.logic.homework;
public class SavingsAccount extends BankAccount {
private int count = 3;
private double rate = 0.01;
public SavingsAccount(double initialBalance) {
super(initialBalance);
}
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;
}
@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 void earnMonthlyInterest() {
count = 3;
super.deposit(getBalance() * rate);
}
}
package com.logic.homework;
public class CheckingAccount extends BankAccount {
public CheckingAccount(double initialBalance) {
super(initialBalance);
}
@Override
public void deposit(double amount) {
super.deposit(amount - 1);//巧妙地使用了父类地deposit方法
//1元转入银行账号
}
@Override
public void withdraw(double amount) {
super.withdraw(amount + 1);
//1元转入银行账号
}
}