-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathMortgageCalculator.java
143 lines (104 loc) · 4.27 KB
/
MortgageCalculator.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
package Algorithms;
import java.text.NumberFormat;
import java.util.Scanner;
/**
* Write a Java program without a graphical user interface that
* calculates and displays the mortgage payment amount given the
* amount of the mortgage, the term of the mortgage, and the
* interest rate of the mortgage.
*
* Then display the balance over the term of the loan.
*
* @author http://learn-java-by-example.com
*
*/
public class MortgageCalculator {
/**
* Display monthly balance for the term of a loan
*
* @param loanAmount total amount of loan
* @param termInYears term of loan in years
* @param interestRate loan interest rate, 5.6% = 5.6
* @param monthlyPayment monthly payment
*/
public static void displayMonthlyBalance(int loanAmount,
int termInYears, double interestRate, double monthlyPayment) {
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
interestRate /= 100.0;
double monthlyRate = interestRate / 12.0;
int termInMonths = termInYears * 12;
// Loop through the term of the loan tracking the balance
double balance = loanAmount;
for (int i=0; i<termInMonths; i++) {
// Add interest to the balance
balance += (balance * monthlyRate);
// Subtract the monthly payment
balance -= monthlyPayment;
// Display running balance
System.out.println("Balance after payment "+(i+1)+": "+
currencyFormat.format(balance));
}
}
/**
* Calculates the monthly payment for a specified loan
*
* @param loanAmount total amount of loan
* @param termInYears term of loan in years
* @param interestRate loan interest rate, 5.6% = 5.6
* @return monthly payment
*/
public static double calculateMonthlyPayment(
int loanAmount, int termInYears, double interestRate) {
// Convert interest rate into a decimal
// eg. 6.5% = 0.065
interestRate /= 100.0;
// Monthly interest rate
// is the yearly rate divided by 12
double monthlyRate = interestRate / 12.0;
// The length of the term in months
// is the number of years times 12
int termInMonths = termInYears * 12;
// Calculate the monthly payment
// Typically this formula is provided so
// we won't go into the details
// The Math.pow() method is used
// to calculate values raised to a power
double monthlyPayment =
(loanAmount*monthlyRate) /
(1-Math.pow(1+monthlyRate, -termInMonths));
return monthlyPayment;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for details of loan
System.out.print("Enter loan amount: ");
int loanAmount = scanner.nextInt();
System.out.print("Enter loan term (in years): ");
int termInYears = scanner.nextInt();
System.out.print("Enter interest rate: ");
double interestRate = scanner.nextDouble();
// Calculate the monthly payment
double monthlyPayment = calculateMonthlyPayment(
loanAmount, termInYears, interestRate);
// NumberFormat is useful for formatting numbers
// In our case we'll use it for
// formatting currency and percentage values
NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance();
NumberFormat interestFormat =
NumberFormat.getPercentInstance();
// Display details of the loan
System.out.println("Loan Amount: "+
currencyFormat.format(loanAmount));
System.out.println("Loan Term: "+
termInYears+" years");
System.out.println("Interest Rate: "+
interestFormat.format(interestRate));
System.out.println("Monthly Payment: "+
currencyFormat.format(monthlyPayment));
// Now display the monthly balance for
// the term of the loan
displayMonthlyBalance(
loanAmount, termInYears, interestRate, monthlyPayment);
}
}