diff options
| author | mo <mo.khan@gmail.com> | 2019-05-25 15:27:51 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2019-05-25 15:27:51 -0600 |
| commit | 856c35dbc7080922bfff7c10a7a844a9d0a1cd65 (patch) | |
| tree | 739b413bc2a29adc35229ef2fd9b30581601a206 /src/Q5 | |
| parent | 46f68298527e0c59b9f34003b687259477093fde (diff) | |
start assignment 2
Diffstat (limited to 'src/Q5')
| -rw-r--r-- | src/Q5/EmployeeSavings.java | 184 | ||||
| -rw-r--r-- | src/Q5/EmployeeSavingsTest.java | 84 | ||||
| -rw-r--r-- | src/Q5/README.md | 119 |
3 files changed, 0 insertions, 387 deletions
diff --git a/src/Q5/EmployeeSavings.java b/src/Q5/EmployeeSavings.java deleted file mode 100644 index e4b067d..0000000 --- a/src/Q5/EmployeeSavings.java +++ /dev/null @@ -1,184 +0,0 @@ -/** - * Assignment 1, COMP268 Class: EmployeeSavings.java - * - * @description Represents an employee savings account. - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q5; - -import Q1.*; -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; - - /** - * Constructs an EmployeeSavings object with a randomly generated monthly contribution. - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - */ - public EmployeeSavings(String firstName, String lastName) { - this(firstName, lastName, new Random().nextInt(800 - 100) + 100.0); - } - - /** - * Constructs an EmployeeSavings object - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - * @param monthlyContribution the monthly contribution to the savings account - */ - public EmployeeSavings(String firstName, String lastName, double monthlyContribution) { - super(firstName, "", lastName); - this.monthlyContribution = monthlyContribution; - this.accountValue = predictBalanceAfterMonths(12); - } - - /** - * Constructs an EmployeeSavings object - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - * @param d1 unknown - * @param d2 unknown - */ - public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) { - super(firstName, "", lastName); - } - - /** - * Return the predicted value of the account after 12 months. - * - * @return the predicted account value after 12 months - */ - public double getAccountValue() { - return this.accountValue; - } - - /** - * Return an array of the predicted interest earned for each month in the upcoming year. - * - * @return the predicted interest earned for each month - */ - public double[] getMonthlyInterests() { - return this.monthlyInterests; - } - - /** - * Return an array of the predicted monthly savings earned for each month in the upcoming year. - * - * @return the predicted monthly savings earned for each month - */ - public double[] getMonthlySavings() { - return this.monthlySavings; - } - - /** - * Return an array of the predicted interest earned for each month in the upcoming year. - * - * @return the predicted interest earned for each month - */ - public double[] calculateInterests() { - this.monthlyInterests = new double[12]; - for (int i = 1; i <= 12; i++) this.monthlyInterests[i - 1] = predictInterestAfterMonths(i); - return this.monthlyInterests; - } - - /** - * Return an array of the predicted monthly savings earned for each month in the upcoming year. - * - * @return the predicted monthly savings earned for each month - */ - public double[] generateMonthlySavings() { - this.monthlySavings = new double[12]; - for (int i = 1; i <= 12; i++) this.monthlySavings[i - 1] = predictBalanceAfterMonths(i); - return this.monthlySavings; - } - - /** - * Predicts the balance of an accounts after n months. - * - * @param months The # of months from now to predict a balance for. - * @return the predicted balance - */ - 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; - } - - /** - * Predicts the interest of an accounts after n months. - * - * @param months The # of months from now to predict the interest for. - * @return the predicted interest - */ - public double predictInterestAfterMonths(int months) { - double totalBalance = predictBalanceAfterMonths(months); - double totalContributions = (months * this.monthlyContribution); - - return Math.round((totalBalance - totalContributions) * 1000) / 1000.0; - } - - /** - * Generates a report that can be printed to the console. - * - * @param accounts the list of accounts to print a statement for - * @return a string that represents a report for the accounts given. - */ - 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); - } - - /** - * Returns The first name, and predicted account balance after 12 months. - * - * @return a string that represents a summary for an account. - */ - @Override - public String toString() { - Currency currency = Currency.getInstance(Locale.getDefault()); - return String.format( - "%s %s%.2f", super.getFirstName(), currency.getSymbol(), this.getAccountValue()); - } - - public static void main(String[] args) { - EmployeeSavings[] accounts = { - 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") - }; - - System.out.print(EmployeeSavings.getReport(accounts)); - } -} diff --git a/src/Q5/EmployeeSavingsTest.java b/src/Q5/EmployeeSavingsTest.java deleted file mode 100644 index a85ab32..0000000 --- a/src/Q5/EmployeeSavingsTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package ca.mokhan.test; - -import Q5.*; -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())); - } -} diff --git a/src/Q5/README.md b/src/Q5/README.md deleted file mode 100644 index c46f8da..0000000 --- a/src/Q5/README.md +++ /dev/null @@ -1,119 +0,0 @@ -Learning Profile for Assignment #1, And Question #5 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Solve the following problem using a program: -Suppose you save $100 each month into a savings account with an annual interest rate of 5%. -Thus, the monthly interest rate is 0.05/12 = 0.00417. -After the first month, the value in the account becomes 100 * (1 + 0.00417) = 100.417 -After the second month, the value in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252 -And after the third month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507 -... and so on. - -Write a program that randomly generates monthly savings amounts for the 15 runners in Problem 4. -Each monthly saving should be in the range of $100 to $800. -Extend the AddressBook class to store the monthly savings generated by the random number generator. -Then, display the final account value for each of the 15 runners. - - -2. Description of the Code: - -I created a constructor overload that accepts the `monthlyContribution` -as a parameter. This made it easier to write unit tests rather than -depend on random monthlyContributions. The constructor that accepts a -`firstName` and `lastName` will generate a random `monthlyContribution` -and chain to the overloaded constructor. - -I had no idea what `d1` and `d2` means, so I didn't use those -parameters. - -I designed this class so that you could predict the future value for `n` -months from now. I did assign the `accountValue` to a prediction of 12 -months from now. I would have preferred to implement an interface that -didn't require private instance variables and rather depend on -calculations on the fly but I did my best to bride the requirements with -my own preferences. - -3. Errors and Warnings: - -```bash -モ mvn test -[INFO] Scanning for projects... -[INFO] -[INFO] -------------------< ca.mokhan.comp268:assignment1 >-------------------- -[INFO] Building assignment1 1.0-SNAPSHOT -[INFO] --------------------------------[ jar ]--------------------------------- -[INFO] -[INFO] --- fmt-maven-plugin:2.8:format (default) @ assignment1 --- -[INFO] Processed 47 files (0 reformatted). -[INFO] -[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ assignment1 --- -[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! -[INFO] skip non existing resourceDirectory /Users/mokha/development/gh/comp-268/src/main/resources -[INFO] -[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ assignment1 --- -[INFO] Changes detected - recompiling the module! -[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! -[INFO] Compiling 24 source files to /Users/mokha/development/gh/comp-268/target/classes -[INFO] -[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ assignment1 --- -[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! -[INFO] skip non existing resourceDirectory /Users/mokha/development/gh/comp-268/src/test/resources -[INFO] -[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ assignment1 --- -[INFO] Changes detected - recompiling the module! -[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! -[INFO] Compiling 24 source files to /Users/mokha/development/gh/comp-268/target/test-classes -[INFO] -[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ assignment1 --- -[INFO] Surefire report directory: /Users/mokha/development/gh/comp-268/target/surefire-reports - -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running ca.mokhan.comp268.AppTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 3.620 s -[INFO] Finished at: 2019-05-13T21:26:40-06:00 -[INFO] ------------------------------------------------------------------------ -``` - -4. Sample Input and Output: -[Provide some test cases with sample input and output of your program.] - -Tests are available in `EmployeeSavingsTest.java`. - -5. Discussion: |
