summaryrefslogtreecommitdiff
path: root/src/Q5/EmployeeSavings.java
blob: e4b067dab4900dbf7a687a24695bb6e42c68dfe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * 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));
  }
}