银行存取业务(手续费,利息)-Java

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;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值