summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormokha <mokha@cisco.com>2019-05-04 09:19:26 -0600
committermokha <mokha@cisco.com>2019-05-04 09:19:26 -0600
commit5bb100c7c322a7fee7fd67ee61a2c0f30538ef1a (patch)
treede42ee69c6c59270e93c3387dc97b919bb7789ae
parente2f0956e077e0ac3fe3f349df002898139cabed8 (diff)
calculate monthly interests
-rw-r--r--assignments/assignment1/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java27
-rw-r--r--assignments/assignment1/src/test/java/ca/mokhan/assignment1/EmployeeSavingsTest.java36
2 files changed, 57 insertions, 6 deletions
diff --git a/assignments/assignment1/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java b/assignments/assignment1/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java
index 304f649..c55ce5f 100644
--- a/assignments/assignment1/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java
+++ b/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;
+ }
}
diff --git a/assignments/assignment1/src/test/java/ca/mokhan/assignment1/EmployeeSavingsTest.java b/assignments/assignment1/src/test/java/ca/mokhan/assignment1/EmployeeSavingsTest.java
index d0c394c..fe5753b 100644
--- a/assignments/assignment1/src/test/java/ca/mokhan/assignment1/EmployeeSavingsTest.java
+++ b/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]);
}
}