Commit 5bb100c

mokha <mokha@cisco.com>
2019-05-04 15:19:26
calculate monthly interests
1 parent e2f0956
Changed files (2)
assignments
assignment1
src
main
java
ca
mokhan
test
java
ca
mokhan
assignments/assignment1/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java
@@ -25,14 +25,30 @@ public class EmployeeSavings extends AddressBook
     super(firstName, "", lastName);
   }
 
-  //public double getAccountValue() { }
-  //public double[] calculateInterests() { }
+  public double getAccountValue() { return predictBalanceAfterMonths(12); }
+  public double[] calculateInterests()
+  {
+    return new double[] {
+      predictInterestAfterMonths(1),
+      predictInterestAfterMonths(2),
+      predictInterestAfterMonths(3),
+      predictInterestAfterMonths(4),
+      predictInterestAfterMonths(5),
+      predictInterestAfterMonths(6),
+      predictInterestAfterMonths(7),
+      predictInterestAfterMonths(8),
+      predictInterestAfterMonths(9),
+      predictInterestAfterMonths(10),
+      predictInterestAfterMonths(11),
+      predictInterestAfterMonths(12),
+    };
+  }
   //public double[] generateMonthlySavings() { }
   //public double[] getMonthlyInterests() {}
   //public double[] getMonthlySavings() {}
   //public static String getReport(EmployeeSavings[] savings) {}
 
-  public double predictSavingsAfterMonths(int months)
+  public double predictBalanceAfterMonths(int months)
   {
     double monthlyRate = ANNUAL_INTEREST_RATE / 12.0;
     double balance = 0;
@@ -43,4 +59,9 @@ public class EmployeeSavings extends AddressBook
 
     return Math.round(balance * 1000) / 1000.0;
   }
+
+  public double predictInterestAfterMonths(int months)
+  {
+    return Math.round((predictBalanceAfterMonths(months) - (months * this.monthlyContribution)) * 1000) / 1000.0;
+  }
 }
assignments/assignment1/src/test/java/ca/mokhan/assignment1/EmployeeSavingsTest.java
@@ -24,8 +24,38 @@ public class EmployeeSavingsTest extends TestCase
   {
     // Bankers rounding rules would round this amount down to $100.41
     // the $0.007 would go to the bank.
-    assertEquals(100.417, subject.predictSavingsAfterMonths(1));
-    assertEquals(201.252, subject.predictSavingsAfterMonths(2));
-    assertEquals(302.507, subject.predictSavingsAfterMonths(3));
+    assertEquals(100.417, subject.predictBalanceAfterMonths(1));
+    assertEquals(201.252, subject.predictBalanceAfterMonths(2));
+    assertEquals(302.507, subject.predictBalanceAfterMonths(3));
+  }
+
+  public void testPredictInterestAfter1Month()
+  {
+    // Bankers rounding rules would round this amount down to $100.41
+    // the $0.007 would go to the bank.
+    assertEquals(0.417, subject.predictInterestAfterMonths(1));
+    assertEquals(1.252, subject.predictInterestAfterMonths(2));
+    assertEquals(2.507, subject.predictInterestAfterMonths(3));
+  }
+
+  public void testGetAccountValue()
+  {
+    assertEquals(subject.predictBalanceAfterMonths(12), subject.getAccountValue());
+  }
+
+  public void testCalculateInterests()
+  {
+    assertEquals(0.417, subject.calculateInterests()[0]);
+    assertEquals(1.252, subject.calculateInterests()[1]);
+    assertEquals(2.507, subject.calculateInterests()[2]);
+    assertEquals(4.184, subject.calculateInterests()[3]);
+    assertEquals(6.285, subject.calculateInterests()[4]);
+    assertEquals(8.811, subject.calculateInterests()[5]);
+    assertEquals(11.764, subject.calculateInterests()[6]);
+    assertEquals(15.147, subject.calculateInterests()[7]);
+    assertEquals(18.96, subject.calculateInterests()[8]);
+    assertEquals(23.206, subject.calculateInterests()[9]);
+    assertEquals(27.886, subject.calculateInterests()[10]);
+    assertEquals(33.002, subject.calculateInterests()[11]);
   }
 }