class Account {
String idCard;
double balance;
// 账户类的构造方法
public Account(String idCard, double balance) {
this.idCard = idCard;
this.balance = balance;
}
public double getInterest() {
return 0.0;
}
}
// 定义定期存款账户类
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 Zzm {
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());
}
}