现有类Account表示银行账户,Account类派生出FixedDepositAccount类和BankingAccount类,FixedDepositAccount表示定期存款账户,BankingAccount表示理财账户。 要求:
(1)Account类的定义已经在下面给出
(2)FixedDepositAccount类有自己特有的属性月数int months以及对应的get和set方法,有参构造方法FixedDepositAccount(String id, double balance, int months, double rate)
(3)BankingAccount类有自己特有的属性天数int days以及对应的get和set方法,有参构造方法BankingAccount(String id, double balance, int days, double rate)
(4)在FixedDepositAccount和BankingAccount中重写getInterest()方法计算利息。
说明:定期存款账户以月为利息的计算单位,计算利息的公式为:利息= 余额balance×年利率rate×月数months/12。理财账户以天作为利息的计算单位,计算利息的公式为:利息= 余额balance×年利率rate×天数days/365。
Account类的定义和主类已经在下面给出,你只需编写FixedDepositAccount类和BankingAccount类:
裁判测试程序样例:
import java.util.*;
class Account {
private String id;
private double balance;//余额
private double rate;//年利率
public Account() {
}
public Account(String id, double balance,double rate) {
this.id = id;
this.balance = balance;
this.rate=rate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getInterest(){
return 0;
}
}
public class Main {
public static void main(String[] args) {
//建立一个3年期的定期账户
Scanner sc=new Scanner(System.in);
FixedDepositAccount fda = new FixedDepositAccount();
fda.setId(sc.next());
fda.setBalance(sc.nextDouble());
fda.setMonths(sc.nextInt());
fda.setRate(sc.nextDouble());
System.out.printf("%s账户%.2f元存款的%d月存款利息为:%.2f\n",fda.getId(),fda.getBalance(),fda.getMonths(),fda.getInterest());
//建立一个182天的理财账户
BankingAccount ba = new BankingAccount();
ba.setId(sc.next());
ba.setBalance(sc.nextDouble());
ba.setDays(sc.nextInt());
ba.setRate(sc.nextDouble());
System.out.printf("%s账户%.2f元存款的%d天存款利息为:%.2f\n",ba.getId(),ba.getBalance(),ba.getDays(),ba.getInterest());
}
}
/* 请在这里填写答案 编写FixedDepositAccount类和BankingAccount类 */
输入样例:
在这里给出一组输入。例如:
001 100000 36 0.035
002 100000 182 0.052
结尾无空行
输出样例:
在这里给出相应的输出。例如:
001账户100000.00元存款的36月存款利息为:10500.00
002账户100000.00元存款的182天存款利息为:2592.88
结尾无空行
代码如下:
class FixedDepositAccount extends Account{
int months;
public int getMonths() {
return months;
}
public void setMonths(int months) {
this.months = months;
}
public FixedDepositAccount() {
super();
}
public FixedDepositAccount(String id, double balance,int months, double rate) {
super(id, balance, rate);
this.months=months;
}
public double getInterest() {
return super.getBalance()*super.getRate()*months/12;
}
}
class BankingAccount extends Account {
int days;
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
public BankingAccount() {
super();
}
public BankingAccount(String id, double balance, int days, double rate){
super(id, balance, rate);
this.days=days;
}
public double getInterest() {
return super.getBalance()*super.getRate()*days/365;
}
}