account

// 定义账户类

class Account {

    String idCard;

    double balance;

 

    // 账户类的构造方法

    public Account(String idCard, double balance) {

        this.idCard = idCard;

        this.balance = balance;

    }

}

 

// 定义定期存款账户类

class FixedDepositAccount extends Account {

    int months;

    double annualInterestRate;

 

    // 定期存款账户类的构造方法

    public FixedDepositAccount(String idCard, double balance, int months, double annualInterestRate) {

        super(idCard, balance);

        this.months = months;

        this.annualInterestRate = annualInterestRate;

    }

 

    // 重写计算利息的方法

    @Override

    public double getInterest() {

        return balance * annualInterestRate * months / 12 / 100;

    }

 

    // 重写toString方法

    @Override

    public String toString() {

        return idCard + "账户" + balance + "元存款的" + months + "月存款利息:" + String.format("%.2f", getInterest()) +

                "(年利率为" + annualInterestRate + "%)";

    }

}

 

// 定义理财账户类

class BankingAccount extends Account {

    int days;

    double annualInterestRate;

 

    // 理财账户类的构造方法

    public BankingAccount(String idCard, double balance, int days, double annualInterestRate) {

        super(idCard, balance);

        this.days = days;

        this.annualInterestRate = annualInterestRate;

    }

 

    // 重写计算利息的方法

    @Override

    public double getInterest() {

        return balance * annualInterestRate * days / 365 / 100;

    }

 

    // 重写toString方法

    @Override

    public String toString() {

        return idCard + "账户" + balance + "元存款的" + days + "天的存款利息:" + String.format("%.2f", getInterest()) +

                "(年利率为" + annualInterestRate + "%)";

    }

}

 

// 测试

public class Main {

    public static void main(String[] args) {

        FixedDepositAccount fixedDepositAccount = new FixedDepositAccount("001", 10000.0, 36, 3.50);

        BankingAccount bankingAccount = new BankingAccount("002", 10000.0, 182, 5.20);

 

        System.out.println(fixedDepositAccount.toString());

        System.out.println(bankingAccount.toString());

    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值