用于计算兴趣的Java程序

时间:2017-04-07 20:10:50

标签: java

所以我正在完成一项学校任务(痛苦地)并且我已经解决了这个特定的问题,减去了一些细节。该计划基本上计算15个人的随机每月节省,在100美元到800美元之间,然后打印每个月节省的金额,当月赚取的利息,然后是一年赚取的总金额。这是代码。我不打算发布我的EmployeeSavings类扩展的AddressBook类,因为它与问题不太相关....

import java.util.Arrays;

public class EmployeeSavings extends AddressBook {

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

public EmployeeSavings(String firstName, String lastName) {
    super(firstName, "", lastName);
}   

public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) {
    super(firstName, "", lastName);
    this.monthlySavings = d1;   // .length?
    this.monthlyInterests = d2; // .length?
}

public double getAccountValue() {
    return accountValue;
}

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

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

public double[] calculateInterests() {
    accountValue = 0;
    for (int n = 0; n < monthlyInterests.length; n++) {
        accountValue += monthlySavings[n];
        monthlyInterests[n] = (accountValue * (ANNUAL_INTEREST_RATE / 12));
        accountValue += monthlyInterests[n];
    }
    accountValue = accountValue;
    return monthlyInterests;
}

public double[] generateMonthlySavings() {
    for (int n = 0; n < monthlySavings.length; n++) {
        monthlySavings[n] = (Math.random() * 700 + 100);
    }
    return monthlySavings;
}

public static String getReport(EmployeeSavings[] arr) {
    String report = "";
    for (EmployeeSavings calculate : arr) {
        calculate.calculateInterests();
        report += "<" + calculate.getFirstName() + " " + calculate.getLastName() 
        + "> \n\nMonthly savings for 12 months: \n" 
        + Arrays.toString(calculate.getMonthlySavings())
        + "\n\nInterest earned for each month: \n" 
        + Arrays.toString(calculate.getMonthlyInterests())
        + "\n\nTotal account value after 12 months: $" + calculate.getAccountValue() + "\n\n\n";
    }
    return report;
}


}

考试班......

public class TestEmployeeSavings {

public static void main(String[] args) {

    EmployeeSavings[] employees = new EmployeeSavings[] {
            new EmployeeSavings("Elena", "Brandon", new double[12], new double[12]),
            new EmployeeSavings("Thomas", "Molson", new double[12], new double[12]),
            new EmployeeSavings("Hamilton", "Winn", new double[12], new double[12]),
            new EmployeeSavings("Suzie", "Sarandin", new double[12], new double[12]),
            new EmployeeSavings("Philip", "Winne", new double[12], new double[12]),
            new EmployeeSavings("Alex", "Trebok", new double[12], new double[12]),
            new EmployeeSavings("Emma", "Pivoto", new double[12], new double[12]),
            new EmployeeSavings("John", "Lenthen", new double[12], new double[12]),
            new EmployeeSavings("James", "Lean", new double[12], new double[12]),
            new EmployeeSavings("Jane", "Ostin", new double[12], new double[12]),
            new EmployeeSavings("Emily", "Car", new double[12], new double[12]),
            new EmployeeSavings("Daniel", "Hamshire", new double[12], new double[12]),
            new EmployeeSavings("Neda", "Bazdar", new double[12], new double[12]),
            new EmployeeSavings("Aaron", "Smith", new double[12], new double[12]),
            new EmployeeSavings("Kate", "Hen", new double[12], new double[12]) };

    for (EmployeeSavings calculate : employees) {
        calculate.generateMonthlySavings();
    }
    System.out.println(EmployeeSavings.getReport(employees));





}
}

我为每个人得到的输出是这样的:

每月节省12个月: [419.33855414289945,685.3556014985876,750.0193923605882,419.55644434159916,689.0894339019552,114.41141555507095,266.1838438533235,136.00655226158167,232.40836179739927,577.1312357147676,535.620933915401,451.8150715112862]

每个月赚取的利息: [1.7472439755954143,4.61017249840451,7.754462351983647,9.53492446320691,12.445859289728418,12.974431268248416,14.137590747921632,14.763191343794562,15.793072815216203,18.26359076742447,20.57144295360291,22.539720097206615]

帐户总值:$ 5432.072543426794

我试图找出如何调整我的储蓄/利息方法,以便每个金额打印到小数点后第二位(适当的金额),前面带有$符号。我还注意到每个节省金额/利息的列表都包含在[]中,这有点烦人,我想解决这个问题,但不知道如何解决。任何帮助将不胜感激! :)

P.S。 EmployeeSavings类中的所有变量/方法都是程序的预期API的一部分,并且他们似乎对它们有偏离,因此它们不是真正的选项。

1 个答案:

答案 0 :(得分:0)

NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); 
double value = your Number;
String s = n.format(doublePayment);

这就是你要找的东西?