Java中的每月储蓄计算器

时间:2017-04-29 16:27:07

标签: java arrays

**编辑以修复递归方法。

我正在做一些家庭作业来创建一个程序,随机生成每月15个人的储蓄金额。每月保存金额应在100美元至800美元之间,并存入年利率为5%的储蓄账户。因此,月利率为0.05 / 12 = 0.00417。所有金额都应该运行12个月。

我创建了以下类来执行所请求的功能:

public class EmployeeSavings extends AddressBook {

    private double accountValue;
    private double[] monthlyInterests;
    private double[] monthlySavings;
    public static final double ANNUAL_INTEREST_RATE = 0.05;

    /**
     * Using the superclass constructor with args (fn, mn, ln), create a new
     * instance of class EmployeeSavings. Initialize instance variables to
     * values of the arguments passed through the constructor parameters. All
     * parameters defined through the variable list are defined in this
     * instance.
     *
     * @param fn - first name of the employee.
     * @param ln - last name of the employee.
     */
    public EmployeeSavings(String fn, String ln) {
        super(fn, " ", ln);
    }

    /**
     * Using the superclass constructor with args (fn, mn, ln), create a new
     * instance of class EmployeeSavings. Initialize instance variables to
     * values of the arguments passed through the constructor parameters. All
     * parameters defined through the variable list are defined in this
     * instance.
     *
     * @param fn - first name of the employee.
     * @param ln - last name of the employee.
     * @param d1 - monthly savings of the employee.
     * @param d2 - monthly interests of the employee.
     */
    public EmployeeSavings(String fn, String ln, double[] d1, double[] d2) {
        super(fn, " ", ln);
        d1 = monthlySavings;
        d2 = monthlyInterests;

        System.out.println("Savings: ");
        System.out.print(Arrays.toString(monthlySavings));
        System.out.println("Interests: ");
        System.out.println(Arrays.toString(monthlyInterests));
        System.out.println("The total account value is: ");
        System.out.println(accountValue);
        System.out.println();
    }

    public static String getReport(EmployeeSavings[] arr) {
        for (EmployeeSavings arr1 : arr) {
            System.out.println("First name: " + arr1.getfirstName());
            System.out.println("Last name: " + arr1.getlastName());
            System.out.println("Account Value: " + arr1.accountValue());
        }
        return "This is your report.";
    }

    public double getAccountValue() {
        for (int i = 0; i < monthlyInterests.length; i++) {
            accountValue = (accountValue + monthlyInterests[i] + monthlySavings[i]);
        }
        return accountValue;
    }

    public double[] calculateInterests() {
        for (int i = 0; i < 13; i++) {
            monthlyInterests[i] = (monthlySavings[i] * (0.05 / 12));
        }
        return monthlyInterests;
    }

    public double[] generateMonthlySavings() {
        for (int i = 0; i < 13; i++) {
            monthlySavings[i] = (Math.random() * 700 + 101);

        }
        return monthlySavings;
    }

    public double[] getMonthlyInterests() {
        return monthlyInterests;
    }

    public double[] getMonthlySavings() {
         return monthlySavings;
    }
}

与此同时,我的主要方法如下:

public static void main(String[] args) {

    double[] d1 = new double [12];
    double[] d2 = new double [12];

    EmployeeSavings [] employees = new EmployeeSavings[15];
    employees[0] = new  EmployeeSavings("Elena", "Brandon", d1, d2);
    employees[1] = new  EmployeeSavings("Thomas", "Molson", d1, d2);
    employees[2] = new  EmployeeSavings("Hamilton", "Winn", d1, d2);
    employees[3] = new  EmployeeSavings("Suzie", "Sarandin", d1, d2);
    employees[4] = new  EmployeeSavings("Philip", "Winne", d1, d2);
    employees[5] = new  EmployeeSavings("Alex", "Trebok", d1, d2);
    employees[6] = new  EmployeeSavings("Emma", "Pivoto", d1, d2);
    employees[7] = new  EmployeeSavings("John", "Lenthen", d1, d2);
    employees[8] = new EmployeeSavings("James", "Lean", d1, d2);
    employees[9] = new EmployeeSavings("Jane", "Ostin", d1, d2);
    employees[10] = new EmployeeSavings("Emily", "Car", d1, d2);
    employees[11] = new EmployeeSavings("Daniel", "Hamshire", d1, d2);
    employees[12] = new EmployeeSavings("Neda", "Bazdar", d1, d2);
    employees[13] = new EmployeeSavings("Aaron", "Smith", d1, d2);
    employees[14] = new EmployeeSavings("Kate", "Hen", d1, d2);

    EmployeeSavings.getReport(employees);

    }

}

现在我没有永久地返回,而是将null值作为输出,例如:

名字:艾琳娜 中间名:
姓氏:Brandon

储蓄: 空值 兴趣爱好: 空值 总帐户值为: 0.0

知道如何在我的方法中获取随机生成的值以传递给“getReport”方法吗?或者那些方法首先没有正确输出?

谢谢! 乙

1 个答案:

答案 0 :(得分:0)

变化:

System.out.println("Account Value: " + arr1.accountValue);

使用:

 System.out.println("Account Value: " + arr1.getAccountValue());

我认为这是问题。

无论如何,在构造函数

public EmployeeSavings(String fn, String ln, double[] d1, double[] d2)

您再次实例化MonthlySavings和monthlyInterests,我不知道它是否是您想要的东西但是您重新实例化它们 将它们与d1和d2相关联后