summaryrefslogtreecommitdiff
path: root/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java
blob: 44eac104b648aa6fb4ff3f338da18bd817423f5e (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
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());
  }
}