练习9——继承的改写

1、创建Account类


package test2;

/**
 * 〈一句话功能简述〉<br> 
 * 〈实验类的继承〉
 *
 * @author abu
 * @create 2019/7/14
 * @since 1.0.0
 */
public class Account {
    private int id;
    protected double balance;
    private double annualIntersetRate;

    public Account() {
    }

    public Account(int id, double balance, double annualIntersetRate){
        //super();
        this.id = id;
        this.balance = balance;
        this.annualIntersetRate = annualIntersetRate;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualIntersetRate() {
        return annualIntersetRate;
    }

    public void setAnnualIntersetRate(double annualIntersetRate) {
        this.annualIntersetRate = annualIntersetRate;
    }

    public double getMonthlyInterest(){
        return annualIntersetRate/12;
    }

    public void withdraw(double amount){
        if(amount <= this.balance){
            balance -= amount;
        }else{
            System.out.println("没钱还来取!");
        }
    }
    
    public void deposit(double amount){
        balance += amount;
    }
}

2、测试Account类

public class TestAmount {
    public static void main(String[] args){
        //Account a = new Account();
        Account a = new Account(1122, 20000, 0.045);

        System.out.println(a.getId() + "的余额为:" + a.getBalance() +
                "月利率为: " + a.getMonthlyInterest());
        a.withdraw(30000);
        System.out.println(a.getBalance());
        a.withdraw(2500);
        a.deposit(3000);
        System.out.println(a.getId() + "的余额为:" + a.getBalance()
                + "月利率为: " + a.getMonthlyInterest());
    }

}

3、Check Account类


public class CheckAccount extends Account{
    protected double overdraft;

    public double getOverdraft() {
        return overdraft;
    }

    public void setOverdraft(double overdraft) {
        this.overdraft = overdraft;
    }

    public CheckAccount(int id, double balance, double annualIntersetRate, double overdraft) {
        super(id, balance, annualIntersetRate);
        this.overdraft = overdraft;
    }

    public void withdraw(double amount){
        if( balance >= amount ){
            balance -= amount;
        }
        else if(overdraft >= (amount - balance)){
            overdraft -= (amount - balance);
            balance = 0;//这两行顺序不可以调换,因为balance赋值为0之后,
            // 下边进行减的运算就没用了,搞了半天我才发现这个问题!!!
        }
        else{
            System.out.println("没钱还来取!透支也透支不了了!");
        }
    }
}

4、测试Check Account类

public class TestCheckAccount {
    public static void main(String[] args) {

        CheckAccount ca = new CheckAccount(1, 20000, 0.045, 5000);

        ca.withdraw(18000);
        System.out.println(ca.getId() + "的余额为:" + ca.getBalance()
               + "月利率为: " + ca.getMonthlyInterest() + "透支额度还有:" + ca.getOverdraft());

        ca.withdraw(3000);
        System.out.println(ca.getId() + "的余额为:" + ca.getBalance()
                + "月利率为: " + ca.getMonthlyInterest() + "透支额度还有:" + ca.getOverdraft());

        ca.withdraw(5000);
        System.out.println(ca.getId() + "的余额为:" + ca.getBalance()
                + "月利率为: " + ca.getMonthlyInterest() + "透支额度还有:" + ca.getOverdraft());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值