java-本章作业8
按照顺序源码如下
package com.Start300.homework;
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;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
package com.Start300.homework;
//新类CheckingAccount对每次存款和取款都收取1美元的手续费
public class CheckingAccount extends BankAccount {//新的账号
//没有新的属性 生成一个构造器就可以了
public CheckingAccount(double initialBalance) {
super(initialBalance);
}
@Override
public void deposit(double amount) {//存款
super.deposit(amount - 1);
//一元钱转入银行的账号
}
@Override
public void withdraw(double amount) {//取款
super.withdraw(amount + 1);
//一元钱转入银行的账号
}
}
package com.Start300.homework;
//每个月都有利息产生 每月有三次免手续费
public class SavingsAccount extends BankAccount {
//新增加属性
private int count = 3;
private double rate = 0.01;//利率
public void earnMonthlyInterest() {//每个月初 统计上个月的利息 同时将count=3
count = 3;
super.deposit(getBalance() * rate);
}
@Override
public void deposit(double amount) {//存款
//判读是否还可以免手续费
if (count > 0) {
super.deposit(amount);
} else {
super.deposit(amount - 1);//1块钱转入银行
}
count--;//减去一次
}
@Override
public void withdraw(double amount) {//取款
//判断是否还可以面手续费
if (count > 0) {
super.withdraw(amount);
} else {
super.withdraw(amount + 1);//1块钱转入银行
}
count--;//减去一次
}
//无参构造器
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;
}
}
package com.Start300.homework;
public class HomeWork08 {
public static void main(String[] args) {
// CheckingAccount checkingAccount = new CheckingAccount(1000);
// checkingAccount.deposit(10);
// checkingAccount.withdraw(9);
// 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());//1300
savingsAccount.deposit(100);
System.out.println(savingsAccount.getBalance());//1300+100-1
//月初 定时器自动调用一下earnMonthlyInterest
savingsAccount.earnMonthlyInterest();
System.out.println(savingsAccount.getBalance());//1399+13.99
savingsAccount.withdraw(100);//免手续费
System.out.println(savingsAccount.getBalance());//1402.99-100=1312.99
savingsAccount.withdraw(100);
savingsAccount.withdraw(100);
System.out.println(savingsAccount.getBalance());//1112.99
savingsAccount.deposit(100);//扣手续费
System.out.println(savingsAccount.getBalance());//1112.99+100-1
}
}