编写程序实现不同银行的利息计算。假设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;
}
}