继承性与super的使用练习

练习1:

Account:

package com.aff.sup;

public class Account {
    private int id;// 账号
    private double balance;// 余额
    private double annualInterestRate;// 年利率

    public Account(int id, double balance, double annualInterestRate) {
        super();
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    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 getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public double getMonthlyInterest() {
        return this.annualInterestRate / 12;

    }

    // 取钱
    public void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            System.out.println("成功取出" + amount);
        } else {
            System.out.println("余额不足");
        }
    }

    // 存钱
    public void deposit(double amount) {
        balance += amount;
        System.out.println("成功存入" + amount);
    }

}

 

TestAccount:

package com.aff.sup;

//写一个用户测试Account类, 在用户程序中创建一个账号为1122,余额20000,年利率4.5%的Account对象
//使用withdraw方法提款30000,并打印个余额
//再使用withdraw 方法提款2500 使用deposit方法存款3000,然后打印余额和月利率
public class TestAccount {
    public static void main(String[] args) {
        Account acct = new Account(1122, 20000, 0.045);
        acct.withdraw(30000);
        System.out.println("当前余额:" + acct.getBalance());

        acct.withdraw(2500);
        acct.deposit(3000);
        System.out.println("当前余额:" + acct.getBalance() + "月利率:" + acct.getMonthlyInterest());
    }
}

输出结果:

余额不足
当前余额:20000.0
成功取出2500.0
成功存入3000.0
当前余额:20500.0月利率:0.00375

 

 

 

练习2:创建Acount类的一个子类CheckAccount代表可透支账户,该账户中定义一个属性overdraft代表可透支余额,

在CheckAccount类中重写withdraw方法,

其算法如下:

如果(取款金额<账户余额), 可直接取款

如果(取款金额>账户余额),就散需要透支的额度,判断可透支overdraft是否足够支付本次透支需要,

如果可以将账户余额修改为0,重减可透支金额

如果不可以,提示用户超过可透支限额

 

Account:

package com.aff.sup;

public class Account {
    protected int id;// 账号
    protected double balance;// 余额
    protected double annualInterestRate;// 年利率

    public Account(int id, double balance, double annualInterestRate) {
        super();
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    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 getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public double getMonthlyInterest() {
        return this.annualInterestRate / 12;

    }

    // 取钱
    public void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            System.out.println("成功取出" + amount);
        } else {
            System.out.println("余额不足");
        }
    }

    // 存钱
    public void deposit(double amount) {
        balance += amount;
        System.out.println("成功存入" + amount);
    }

}

 

CheckAccount:

package com.aff.sup;

public class CheckAccount extends Account {
    private double overdraft;// 可透支额度

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

    public double getOverdraft() {
        return overdraft;
    }

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

    // 存在可透支额度的取钱
    public void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            System.out.println("成功取出" + amount);
        } else if (overdraft >= amount - balance) {
            overdraft -= (amount - balance);
            balance = 0;
        } else {
            System.out.println("超过可透支限额");
        }
    }
}

 

TestCheckAccount:

package com.aff.sup;
//写一个用户测试Account类, 在用户程序中创建一个账号为1122,余额20000,年利率4.5%的CheckAccount对象 //使用withdraw方法提款5000,并打印个余额与可透支额度 //再使用withdraw 方法提款18000, 并打印个余额与可透支额度 //再使用withdraw 方法提款3000 ,并打印个余额与可透支额度 public class TestCheckAccount { public static void main(String[] args) { CheckAccount ca = new CheckAccount(1122, 20000, 0.045, 5000); ca.withdraw(5000); System.out.println("当前余额:" + ca.getBalance() + "可透支额度:" + ca.getOverdraft()); ca.withdraw(18000); System.out.println("当前余额:" + ca.getBalance() + "可透支额度:" + ca.getOverdraft()); ca.withdraw(3000); System.out.println("当前余额:" + ca.getBalance() + "可透支额度:" + ca.getOverdraft()); } }

输出结果:

成功取出5000.0
当前余额:15000.0可透支额度:5000.0
当前余额:0.0可透支额度:2000.0
超过可透支限额
当前余额:0.0可透支额度:2000.0

 

 

posted @ 2020-03-18 14:19  林淼零  阅读(156)  评论(0编辑  收藏  举报