📜  使用继承计算 FD、RD 利息的Java程序(1)

📅  最后修改于: 2023-12-03 14:49:57.408000             🧑  作者: Mango

使用继承计算 FD、RD 利息的 Java 程序

这个 Java 程序使用了继承来计算 FD(定存)和 RD(定期)账户的利息。程序会提示用户输入账户类型、本金和期限,并根据账户类型和期限计算对应的利息,最终输出本金和利息总额。

实现细节
账户类

首先定义一个 Account 类作为所有账户类型的基类,该类包含以下属性:

  • type: 账户类型(FD 或 RD)
  • principal: 本金
  • term: 期限

并包含以下方法:

  • calculateInterest(): 抽象方法,用于计算利息

代码片段如下所示:

public abstract class Account {
    protected String type;
    protected double principal;
    protected int term;

    public Account(String type, double principal, int term) {
        this.type = type;
        this.principal = principal;
        this.term = term;
    }

    public abstract double calculateInterest();
}
FD 账户类

接下来定义 FDAccount 类作为 FD 账户的类,该类继承自 Account 类。除了基类的属性和方法外,该类还包含以下属性:

  • interestRate: 年利率

并覆盖了父类中的 calculateInterest() 方法,计算方法如下所示:

public class FDAccount extends Account {
    private double interestRate;

    public FDAccount(double principal, int term) {
        super("FD", principal, term);
        if (term < 1) {
            throw new IllegalArgumentException("Term should be at least 1 year");
        }
        if (principal < 1000) {
            throw new IllegalArgumentException("Minimum principal is 1000");
        }
        if (term >= 1 && term < 2) {
            this.interestRate = 0.06;
        } else if (term >= 2 && term < 3) {
            this.interestRate = 0.065;
        } else if (term >= 3 && term < 5) {
            this.interestRate = 0.07;
        } else {
            this.interestRate = 0.075;
        }
    }

    @Override
    public double calculateInterest() {
        double interest = principal * Math.pow(1 + interestRate, term) - principal;
        return Math.round(interest * 100) / 100.0; // round to 2 decimal places
    }
}
RD 账户类

最后定义 RD 账户的类 RDAccount,与 FDAccount 类似,该类继承自 Account 类,并覆盖了父类中的 calculateInterest() 方法,计算方法如下所示:

public class RDAccount extends Account {
    private double interestRate;

    public RDAccount(double principal, int term) {
        super("RD", principal, term);
        if (term < 1) {
            throw new IllegalArgumentException("Term should be at least 1 year");
        }
        if (principal < 5000) {
            throw new IllegalArgumentException("Minimum principal is 5000");
        }
        if (term >= 1 && term < 2) {
            this.interestRate = 0.065;
        } else if (term >= 2 && term < 3) {
            this.interestRate = 0.0675;
        } else if (term >= 3 && term < 5) {
            this.interestRate = 0.07;
        } else {
            this.interestRate = 0.075;
        }
    }

    @Override
    public double calculateInterest() {
        double interest = principal * interestRate * term;
        return Math.round(interest * 100) / 100.0; // round to 2 decimal places
    }
}
主程序

主程序中接受用户输入账户类型、本金和期限,并动态创建对应的账户对象来计算利息,最终输出结果。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter account type (FD or RD): ");
        String type = scanner.nextLine().toUpperCase();
        System.out.print("Enter principal: ");
        double principal = Double.parseDouble(scanner.nextLine());
        System.out.print("Enter term (in years): ");
        int term = Integer.parseInt(scanner.nextLine());

        Account account;
        if (type.equals("FD")) {
            account = new FDAccount(principal, term);
        } else if (type.equals("RD")) {
            account = new RDAccount(principal, term);
        } else {
            System.out.println("Invalid account type");
            return;
        }

        double interest = account.calculateInterest();
        double totalAmount = principal + interest;

        System.out.println("Principal: " + principal);
        System.out.println("Interest: " + interest);
        System.out.println("Total amount: " + totalAmount);
    }
}
示例输出

以下是几个程序测试的示例输出:

Enter account type (FD or RD): FD
Enter principal: 10000
Enter term (in years): 2
Principal: 10000.0
Interest: 1330.0
Total amount: 11330.0
Enter account type (FD or RD): RD
Enter principal: 7000
Enter term (in years): 3
Principal: 7000.0
Interest: 1470.0
Total amount: 8470.0
Enter account type (FD or RD): XX
Invalid account type