java-本章作业8

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
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值