diff options
| author | mokha <mokha@cisco.com> | 2019-05-05 19:57:14 -0600 |
|---|---|---|
| committer | mokha <mokha@cisco.com> | 2019-05-05 19:57:14 -0600 |
| commit | 7b4cb6a2eef3a0fcde7e998832427c73599e4a0a (patch) | |
| tree | 75acc7cd8f3bd8a087e909db182c9e18a66879a1 /src/Q5 | |
| parent | 99a28d2dba93642e89a62ecf905a3bd4f138318d (diff) | |
use the exact naming convention for directories described by instructor
Diffstat (limited to 'src/Q5')
| -rw-r--r-- | src/Q5/EmployeeSavings.java | 84 | ||||
| -rw-r--r-- | src/Q5/EmployeeSavingsTest.java | 83 |
2 files changed, 167 insertions, 0 deletions
diff --git a/src/Q5/EmployeeSavings.java b/src/Q5/EmployeeSavings.java new file mode 100644 index 0000000..44eac10 --- /dev/null +++ b/src/Q5/EmployeeSavings.java @@ -0,0 +1,84 @@ +package ca.mokhan.assignment1; + +import java.util.ArrayList; +import java.util.Currency; +import java.util.Locale; +import java.util.Random; + +public class EmployeeSavings extends AddressBook { + private double accountValue; + private double[] monthlyInterests; + private double[] monthlySavings; + private double monthlyContribution; + public static final double ANNUAL_INTEREST_RATE = 0.05; + + public EmployeeSavings(String firstName, String lastName) { + this(firstName, lastName, new Random().nextInt(800 - 100) + 100.0); + } + + public EmployeeSavings(String firstName, String lastName, double monthlyContribution) { + super(firstName, "", lastName); + this.monthlyContribution = monthlyContribution; + this.accountValue = predictBalanceAfterMonths(12); + } + + // what is d1, d2? + public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) { + super(firstName, "", lastName); + } + + public double getAccountValue() { + return this.accountValue; + } + + public double[] getMonthlyInterests() { + return this.monthlyInterests; + } + + public double[] getMonthlySavings() { + return this.monthlySavings; + } + + public double[] calculateInterests() { + this.monthlyInterests = new double[12]; + for (int i = 1; i <= 12; i++) this.monthlyInterests[i - 1] = predictInterestAfterMonths(i); + return this.monthlyInterests; + } + + public double[] generateMonthlySavings() { + this.monthlySavings = new double[12]; + for (int i = 1; i <= 12; i++) this.monthlySavings[i - 1] = predictBalanceAfterMonths(i); + return this.monthlySavings; + } + + public double predictBalanceAfterMonths(int months) { + double monthlyRate = ANNUAL_INTEREST_RATE / 12.0; + double balance = 0; + + for (int i = 0; i < months; i++) + balance = (balance + this.monthlyContribution) * (1 + monthlyRate); + + return Math.round(balance * 1000) / 1000.0; + } + + public double predictInterestAfterMonths(int months) { + return Math.round( + (predictBalanceAfterMonths(months) - (months * this.monthlyContribution)) * 1000) + / 1000.0; + } + + public static String getReport(EmployeeSavings[] accounts) { + ArrayList<String> statement = new ArrayList<String>(); + + for (EmployeeSavings account : accounts) statement.add(account.toString()); + + return String.join(System.lineSeparator(), statement); + } + + @Override + public String toString() { + Currency currency = Currency.getInstance(Locale.getDefault()); + return String.format( + "%s %s%.2f", super.getFirstName(), currency.getSymbol(), this.getAccountValue()); + } +} diff --git a/src/Q5/EmployeeSavingsTest.java b/src/Q5/EmployeeSavingsTest.java new file mode 100644 index 0000000..5e9374b --- /dev/null +++ b/src/Q5/EmployeeSavingsTest.java @@ -0,0 +1,83 @@ +package ca.mokhan.assignment1; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class EmployeeSavingsTest extends TestCase { + private EmployeeSavings subject; + + public EmployeeSavingsTest(String testName) { + super(testName); + double monthlyContribution = 100.00; + this.subject = new EmployeeSavings("Tsuyoshi", "Garrett", monthlyContribution); + } + + public static Test suite() { + return new TestSuite(EmployeeSavingsTest.class); + } + + public void testPredictSavingsAfter1Month() { + // Bankers rounding rules would round this amount down to $100.41 + // the $0.007 would go to the bank. + 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]); + } + + public void testGenerateMonthlySavings() { + assertEquals(100.417, subject.generateMonthlySavings()[0]); + assertEquals(201.252, subject.generateMonthlySavings()[1]); + assertEquals(302.507, subject.generateMonthlySavings()[2]); + } + + public void testGetReport() { + EmployeeSavings[] accounts = + new EmployeeSavings[] { + new EmployeeSavings("Elena", "Brandon"), + new EmployeeSavings("Thomas", "Molson"), + new EmployeeSavings("Hamilton", "Winn"), + new EmployeeSavings("Suzie", "Sarandin"), + new EmployeeSavings("Philip", "Winne"), + new EmployeeSavings("Alex", "Trebok"), + new EmployeeSavings("Emma", "Pivoto"), + new EmployeeSavings("John", "Lenthen"), + new EmployeeSavings("James", "Lean"), + new EmployeeSavings("Jane", "Ostin"), + new EmployeeSavings("Emily", "Car"), + new EmployeeSavings("Daniel", "Hamshire"), + new EmployeeSavings("Neda", "Bazdar"), + new EmployeeSavings("Aaron", "Smith"), + new EmployeeSavings("Kate", "Hen") + }; + String report = EmployeeSavings.getReport(accounts); + for (EmployeeSavings account : accounts) assertTrue(report.contains(account.getFirstName())); + } +} |
