-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoanPresentation.java
125 lines (108 loc) · 3.98 KB
/
LoanPresentation.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
import java.util.Scanner;
import java.math.RoundingMode;
import java.text.NumberFormat;
/**
* Loan payment calculator that uses a separate method for each input. There is no
* error checking for invalid strings.
*
* @author Ken Fogel
*/
public class LoanPresentation {
// Class or instance variables available to all methods in the class only
private double presentValue;
private double annualPercentageRate;
private double term;
private double payment;
private Scanner sc;
private LoanBusiness loanBusiness;
/**
* Constructor
* Instantiates the Scanner and LoanBusiness objects
*/
public LoanPresentation() {
sc = new Scanner(System.in);
loanBusiness = new LoanBusiness();
}
/**
* Request the amount of the loan. If the string entered cannot be converted
* to a double then exceptions will be thrown
*/
private void inputPresentValue() {
System.out.println("Please enter the amount of the loan: ");
presentValue = sc.nextDouble();
}
/**
* Request the annual interest rate of the loan. If the string entered cannot be
* converted to a double then exceptions will be thrown
*/
private void inputAnnualPercentageRate() {
System.out.println("Please enter the annual percentage rate: ");
annualPercentageRate = sc.nextDouble();
}
/**
* Request the term/length of the loan. If the string entered cannot be
* converted to a double then exceptions will be thrown
*/
private void inputTerm() {
System.out.println("Please enter the years to repay the loan: ");
term = sc.nextDouble();
}
/**
* Method to display the result of the calculation rounded and formatted
*
*/
private void displayResult() {
NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setRoundingMode(RoundingMode.HALF_EVEN);
System.out.println("The rounded monthly payment is = " + nf.format(payment));
}
/**
* This method is where the program begins doing useful work. You can name it anything
* you want but by convention it should be a verb.
*/
public void perform() {
inputPresentValue();
inputAnnualPercentageRate();
inputTerm();
payment = loanBusiness.calculateLoanPayment(presentValue, annualPercentageRate, term);
displayResult();
}
/**
* Where the program begins
*
* @param args Command line arguments, not used in this program
*/
public static void main(String[] args) {
var loanPresentation = new LoanPresentation();
loanPresentation.perform();
}
}
/**
* Business class with a method to calculate the loan payment.
*
*/
public class LoanBusiness {
/**
* Method that receives the three values required to calculate loan payments,
* calculates the payment, and returns it.
*
* @param presentValue Amount of money being borrowed
* @param annualPercentageRate Annual interest rate
* @param term Length of loan in years
* @return Loan payment
*/
public double calculateLoanPayment(double presentValue, double annualPercentageRate, double term) {
double monthlyPercentageRate = annualPercentageRate / 12.0;
double monthlyTerm = term * 12.0;
double divisor = 1 - Math.pow(1+monthlyPercentageRate, -monthlyTerm);
double payment = presentValue * monthlyPercentageRate / divisor;
return payment;
}
}
/***********************************************************************
* This work is licensed under the Creative Commons Attribution 4.0 *
* International License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by/4.0/ *
* or send a letter to *
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. *
***********************************************************************/