-
Notifications
You must be signed in to change notification settings - Fork 20
/
LoanCalculator.java
102 lines (74 loc) · 1.99 KB
/
LoanCalculator.java
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
package abcbank;
public class LoanCalculator
{
private double loanAmount;
private int numberOfYears;
private double yearlyInterestRate;
// no default no arg constructor generated if a constructor with args is delcared pmc
public LoanCalculator ()
{
}
public LoanCalculator (double loanAmount, int numberOfYears,
double yearlyInterestRate)
{
this.loanAmount = loanAmount;
this.numberOfYears = numberOfYears;
this.yearlyInterestRate = yearlyInterestRate;
}
public double getLoanAmount ()
{
return loanAmount;
}
public double getMonthlyPayment ()
{
double monthlyPayment;
double monthlyInterestRate;
int numberOfPayments;
if (numberOfYears != 0 && yearlyInterestRate != 0)
{
//calculate the monthly payment
monthlyInterestRate = yearlyInterestRate / 1200;
numberOfPayments = numberOfYears * 12;
monthlyPayment =
(loanAmount * monthlyInterestRate) /
(1 - (1 / Math.pow ((1 + monthlyInterestRate), numberOfPayments)));
monthlyPayment = Math.round (monthlyPayment * 100) / 100.0;
}
else
monthlyPayment = 0;
return monthlyPayment;
}
public int getNumberOfYears ()
{
return numberOfYears;
}
public double getTotalCostOfLoan ()
{
return getMonthlyPayment () * numberOfYears * 12;
}
public double getTotalInterest ()
{
return getTotalCostOfLoan () - loanAmount;
}
public double getYearlyInterestRate ()
{
return yearlyInterestRate;
}
public void setLoanAmount (double loanAmount)
{
this.loanAmount = loanAmount;
}
public void setNumberOfYears (int numberOfYears)
{
this.numberOfYears = numberOfYears;
}
public void setYearlyInterestRate (double yearlyInterestRate)
{
this.yearlyInterestRate = yearlyInterestRate;
}
public String toString ()
{
return getLoanAmount () + "," + getNumberOfYears () + "," +
getYearlyInterestRate ();
}
}