-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmortization.java
214 lines (141 loc) · 4.36 KB
/
Amortization.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
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
//Captain-Price-TF-141
import java.io.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Amortization
{
public static void main(String[] args)
{ // primary
try
{
String input = "";
while (!input.equalsIgnoreCase("n")) //Program Statements
{
double loan = 0;
double rate = 0;
int time = 0;
String file_name = "LoanAmortization.txt";
//double loan_amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the loan amount."));
double loan_amount = getLoanAmount(loan);
//double interest_rate = Double.parseDouble(JOptionPane.showInputDialog("Enter the annual interest rate."));
double interest_rate = getInterestRate(rate);
//int years = Integer.parseInt(JOptionPane.showInputDialog("Enter the years of the loan."));
int years = getLoanYears(time);
Amortization amortization = new Amortization(loan_amount, interest_rate, years);
try
{
amortization.saveReport(file_name);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "Report saved to the file " + file_name);
input=JOptionPane.showInputDialog("Would you like to run another report? Enter Y for yes or N for no:");
}
}
finally
{
}
// stop primary
//secondary
}
double loanAmount; // amount of the loan
double interestRate; //Annual Interest Rate
double loanBalance; // Monthly Balance
double term; // used in the calculation of the monthly payment
double payment; // a double variable to hold the amount of the monthly payment
int loanYears; // number of years of the loan
/*
constructor accept three arguments
*/
public Amortization(double loan, double rate, int years)
{
loanAmount = loan;
loanBalance = loan;
interestRate = rate;
loanYears = years;
calcPayment();
}
/**
* private method that is used to calculate the monthly payment amount.
* The result is stored in the payment field.
*/
public void calcPayment()
{
// get value of Term
term = Math.pow((1+interestRate/12.0), 12.0 * loanYears);
// get monthly payment
payment = (loanAmount * interestRate/12.0 * term) / (term - 1);
}
/**
returns as an int the number of loan payments
*/
public int getNumberOfPayments()
{
return 12 * loanYears;
}
/**
saves the amortization report to a text file
*/
public void saveReport(String filename) throws IOException
{
double monthlyInterest; // The monthly interest rate
double principal; // The amount of principal
DecimalFormat dollar = new DecimalFormat("#,##0.00");
FileWriter fwriter = new FileWriter(filename);
PrintWriter outputFile = new PrintWriter(fwriter);
// Print monthly payment amount.
outputFile.println("Monthly Payment: $" + dollar.format(payment));
// Print the report header.
outputFile.println("Month" + " " + "Interest" + " " + "Principal" + " " + "Balance");
outputFile.println("----------------------------------------------------------------------------");
// Display the amortization table.
for (int month = 1; month <= getNumberOfPayments(); month++)
{
// Calculate monthly interest.
monthlyInterest = interestRate / 12.0 * loanBalance;
if (month != getNumberOfPayments())
{
// Calculate payment applied to principal
principal = payment - monthlyInterest;
}
else // This is the last month.
{
principal = loanBalance;
payment = loanBalance + monthlyInterest;
}
// Calculate the new loan balance.
loanBalance -= principal;
// Display a line of data.
outputFile.println(month + " " + dollar.format(monthlyInterest) + " " + dollar.format(principal) + " " + dollar.format(loanBalance));
}
// Close the file
outputFile.close();
}
/**
* @return
*/
public static double getLoanAmount(double loan)
{
loan = Double.parseDouble(JOptionPane.showInputDialog("Enter the loan amount."));
return loan;
}
/**
@return annual interest rate
*/
public static double getInterestRate(double rate)
{
rate = Double.parseDouble(JOptionPane.showInputDialog("Enter the annual interest rate."));
return rate;
}
/**
@return number of years of the loan.
*/
public static int getLoanYears(int time)
{
time = Integer.parseInt(JOptionPane.showInputDialog("Enter the years of the loan."));
return time;
}
}