-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLoanCalculator.java
More file actions
97 lines (90 loc) · 3.24 KB
/
SimpleLoanCalculator.java
File metadata and controls
97 lines (90 loc) · 3.24 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
// A simple loan calculator servlet.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.text.*;
public class RegPayS extends HttpServlet {
double principal; // original principal
double intRate; // interest rate
double numYears; // length of loan in years
/* Number of payments per year. You could
allow this value to be set by the user. */
final int payPerYear = 12;
NumberFormat nf;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String payStr = "";
// Create a number format.
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
// Get the parameters.
String amountStr = request.getParameter("amount");
String periodStr = request.getParameter("period");
String rateStr = request.getParameter("rate");
try {
if(amountStr != null && periodStr != null &&
rateStr != null) {
principal = Double.parseDouble(amountStr);
numYears = Double.parseDouble(periodStr);
intRate = Double.parseDouble(rateStr) / 100;
payStr = nf.format(compute());
}
else { // one or more parameters missing
amountStr = "";
periodStr = "";
rateStr = "";
}
} catch (NumberFormatException exc) {
// Take appropriate action here.
}
// Set the content type.
response.setContentType("text/html");
// Get the output stream.
PrintWriter pw = response.getWriter();
// Display the necessary HTML.
pw.print("<html><body> <left>" +
"<form name=\"Form1\"" +
" action=\"http://localhost:8080/" +
"servlets-examples/servlet/RegPayS\">" +
"<B>Enter amount to finance:</B>" +
" <input type=textbox name=\"amount\"" +
" size=12 value=\"");
pw.print(amountStr + "\">");
pw.print("<BR><B>Enter term in years:</B>" +
" <input type=textbox name=\"period\""+
" size=12 value=\"");
pw.println(periodStr + "\">");
pw.print("<BR><B>Enter interest rate:</B>" +
" <input type=textbox name=\"rate\"" +
" size=12 value=\"");
pw.print(rateStr + "\">");
pw.print("<BR><B>Monthly Payment:</B>" +
" <input READONLY type=textbox" +
" name=\"payment\" size=12 value=\"");
pw.print(payStr + "\">");
pw.print("<BR><P><input type=submit value=\"Submit\">");
pw.println("</form> </body> </html>");
}
// Compute the loan payment.
double compute() {
double numer;
double denom;
double b, e;
numer = intRate * principal / payPerYear;
e = -(payPerYear * numYears);
b = (intRate / payPerYear) + 1.0;
denom = 1.0 - Math.pow(b, e);
return numer / denom;
}
}
/*
👋 Hi, I’m @aarushinair - Aarushi Nair (she/her/ella)
👀 I’m a Computer Science Engineering Student
💞️ I’m looking to collaborate on #java, #python, #R, #applicationdevelopment
🌱 #GirlsWhoCode #WomenInTech #WomenInIT #WomenInSTEM #CyberSecurity #QuantumComputing #BlockChain #AI #ML
📫 How to reach me: https://www.linkedin.com/in/aarushinair/
👩🏫 YouTube Channel - Code with Aarushi : https://www.youtube.com/channel/UCKj5T1ELHCmkGKujkpqtl7Q
🙋 Follow me on Twitter: https://twitter.com/aarushinair_
*/