银行利息计算(java)

程序设计了一个Bank基类,以及两个子类ConstructionBank和HanKouBank,分别模拟建设银行和汉口银行的利息计算。这两个银行对非整年的利息按天计算,利率不同。在测试类中,存入8000元,年利率0.035,存期1年零256天,计算并输出了两个银行的利息和利息差额。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写程序实现不同银行的利息计算。假设Bank类中已有按整年year计算利息的一般方法,其中year只能取正整数。而建设银行ConstructionBank和汉口银行HanKouBank准备重写计算利息的方法,建设银行的非整年的利息按天计算,利率是0.0001,汉口银行的非整年的利息也是按天计算,利率是0.00012。UML类图如下。

完成上面的程序,并编写一个类对其进行测试。要求分别在建设银行和汉口银行各存入8000元,存期为1.256(即1年零256天),年利率为0.035,计算并输出存在两个银行的利息为多少,两个银行利息差额,并输出。

package main;



public class Bank {

    int savedMoney;

    double year;

    double interest;

    double interestRate;

    Bank(int savedMoney,double year,double interest,double interestRate)

    {

        this.savedMoney=savedMoney;

        this.year=year;

        this.interest=interest;

        this.interestRate=interestRate;

    }

    public int getSavedMoney()

    {

        return savedMoney;

    }

    public double getYear()

    {

        return year;

    }

    double computerInterest()

    {

        return this.savedMoney*this.interestRate;

    }

    public double getInterestRate()

    {

        return interestRate;

    }

    void setInterestRate(double rate)

    {

        this.interestRate=rate;

    }

    public static void main(String[] args)

    {

        ConstructionBank constructionBank = new ConstructionBank(8000,0.256,0.035,0.0001);

        constructionBank.computerInterest();

        HanKouBank hanKouBank = new HanKouBank(8000,0.256,0.035,0.0001);

        hanKouBank.computerInterest();

   

    }

}

class ConstructionBank extends Bank

{

    ConstructionBank(int savedMoney, double year, double interest, double interestRate) {

        super(savedMoney, year, interest, interestRate);

    }

    double computerInterest()

    {

        double sum=8000*0.035+year*super.savedMoney*0.0001;

        System.out.println("建设银行的利息是:"+sum);

        return sum;

    }

}

class HanKouBank extends Bank

{

    HanKouBank(int savedMoney, double year, double interest, double interestRate) {

        super(savedMoney, year, interest, interestRate);

    }

    double computerInterest()

    {

        double sum=8000*0.035+year*super.savedMoney*0.00012;

        System.out.println("汉口银行的利息是:"+sum);

        return sum;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值