-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoan.java
More file actions
236 lines (185 loc) · 5.63 KB
/
Loan.java
File metadata and controls
236 lines (185 loc) · 5.63 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Holds input data for a loan and performs computations to determine payment amount needed
* to payoff that loan in a given loan term with a given interest rate and the total that
* will be paid over the life of the loan.
*
* @author Alan M
*
*/
public class Loan
{
// input data for loan, interest rate is assumed to be out of 100
private double loanAmount, interestRate;
private int loanTermYears, loanTermMonths;
private int paymentFrequency, compoundingFrequency;
// used for computations
private double interestMultiplier; // (1 + interest / 100)
private int paymentsPerCompounding;
//private int periods;
private double amountRemaining; // used when computing the total paid
// computations based on input data
private double total, paymentAmount;
private final int MONTHS_PER_YEAR = 12;
/**
* Default constructor.
*/
public Loan()
{
loanAmount = 0;
interestRate = 0;
loanTermYears = 0;
loanTermMonths = 0;
interestMultiplier = 1;
}
/**
* Default constructor.
*/
public Loan(double amt, double interest, int years)
{
this.loanAmount = amt;
this.interestRate = interest;
this.loanTermYears = years;
this.interestMultiplier = 1 + interestRate / 100;
this.paymentAmount = computePaymentAmount();
this.total = paymentAmount * loanTermYears;
}
public double computePaymentAmount()
{
return getNumerator() / getDenominator();
}
/**
* Helper method to compute the numerator in the equation used to compute payment amount.
* Numerator = Principal * (1 + interest rate) ^ n
* @return The value for the numerator in the equation used by the compute() method.
*/
private double getNumerator() {
// value of (1 + interest rate) ^ n
double multiplier = Math.pow(interestMultiplier, loanTermYears);
// returns the value of principal * (1 + interest rate) ^ n
return (loanAmount * multiplier);
}
/**
* Helper method to compute the denominator in the equation used to compute payment amount.
* Denominator = Sum( (1 + interest rate) ^ j [for j = 0 to j = n - 1] )
* @return The value for the denominator in the equation used by the compute() method.
*/
private double getDenominator() {
double denominator = 1;
for(int period = 1; period < loanTermYears; period++)
{
denominator += Math.pow(interestMultiplier, period);
}
return denominator;
}
/**
* Returns the payment amount that needs to be paid periodically to pay off the loan in the given term.
*
* @return The payment amount.
*/
public double getPaymentAmount() { return paymentAmount; }
// / paymentsPerCompounding; }
/**
* Returns the total amount that will be paid to pay off the loan in the given term.
*
* @return The total that will be paid over the life of the loan.
*/
public double getTotalPaid() { return total; }
/**
* Sets the amount of the loan to the passed in value.
*
* @param amount The amount of the loan.
*/
public void setAmount(double amount) { this.loanAmount = amount; }
/**
* Sets the term of the loan to the passed in years and months.
*
* @param years The years of the loan term.
* @param months The months of the loan term.
*/
public void setLoanTerm(int years, int months) {
this.loanTermYears = years;
this.loanTermMonths = months;
}
/**
* Sets the frequency of the payments that will be made throughout a year to payoff the loan to
* the passed in string.
*
* @param freq Frequency of the payments that will be made throughout a year to payoff the loan.
*/
/*public void setPaymentFrequency(String freq) {
this.payFreq = freq;
switch(freq) {
case("Yearly"):
paymentsPerCompounding = 1;
break;
case("Monthly"):
paymentsPerCompounding = MONTHS_PER_YEAR;
break;
}
}*/
/**
* Sets the frequency of the compounding done on the loan in a given year to the passed in string.
*
* @param freq Frequency of the compounding done on the loan in a given year.
*//*
public void setCompoundingFrequency(String freq) {
this.compFreq = freq;
switch(freq) {
case("Yearly"):
compoundingFrequency = 1;
break;
case("Monthly"):
compoundingFrequency = MONTHS_PER_YEAR;
break;
}
}
public void setInterestRate(double interest) {
this.interestRate = interest;
// adjust interest rate to account for frequency of compounding per year
if(compFreq != null)
{
switch(compFreq) {
case("Yearly"):
interestRate /= 1;
break;
case("Monthly"):
interestRate /= MONTHS_PER_YEAR;
break;
}
}
this.interestMultiplier = (interestRate / 100) + 1;
}*/
/**
* Computes the payment amount needed payoff the loan and the total amount that will be paid over the life of the loan.
*
* ASSUMES: PAYMENT FREQUENCY = COMPOUNDING FREQUENCY.
*
* Equation used to compute payment amount:
* Payments = (Principal * (1 + interest rate) ^ n ) / Sum( (1 + interest rate) ^ j [for j = 0 to j = n - 1] ).
*/
public void compute() {
this.paymentAmount = ( getNumerator() / getDenominator() );
amountRemaining = loanAmount;
// compute total to be paid for the years of the loan during the loan term
for(int i = 1; i <= loanTermYears; i++)
{
addInterest();
makePayment();
}
// account case that too much or too little is paid off
// by end of loan term - too much paid will give negative
// amount remaining, too little paid will give a positive
// amount remaining
if(amountRemaining != 0)
{
total += amountRemaining;
}
}
private void addInterest() {
amountRemaining *= interestMultiplier;
}
private void makePayment() {
amountRemaining -= paymentAmount;
total += paymentAmount;
}
}