So I'm working on a school assignment (painfully) and I've got this particular question sorted out, minus a few details. The program basically calculates random monthly savings for 15 individuals, between $100 and $800, and then prints the amount saved for each month, the interest earned for the month and then the total earned for one year. Here's the code. I won't bother posting the AddressBook class that my EmployeeSavings class extends, as it's not really relevant to the question....
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;
}
}
And the test class....
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));
}
}
The output I get for each person is something like this:
Monthly savings for 12 months: [419.33855414289945, 685.3556014985876, 750.0193923605882, 419.55644434159916, 689.0894339019552, 114.41141555507095, 266.1838438533235, 136.00655226158167, 232.40836179739927, 577.1312357147676, 535.620933915401, 451.8150715112862]
Interest earned for each month: [1.7472439755954143, 4.61017249840451, 7.754462351983647, 9.53492446320691, 12.445859289728418, 12.974431268248416, 14.137590747921632, 14.763191343794562, 15.793072815216203, 18.26359076742447, 20.57144295360291, 22.539720097206615]
Total account value: $5432.072543426794
I'm trying to figure out how I can tweak my savings/interest methods so that each amount is printed to the second decimal place (proper dollar amount) with a $ sign before it. I've also noticed that the lists for each savings amount/interest is enclosed in [], which is sort of annoying and I'd like to fix that but not sure how. Any help would be greatly appreciated! :)
P.S. All the variables/methods in the EmployeeSavings class are required as part of the expected API for the program and they seem to frown on deviating from them, so that's not really an option.