In: Computer Science
Mortgage Calculator
Create a form that allows you to enter in interest rate, monthly payment, principal , and number of years. Your Java Servlet program then generates a table containing a row of output for each month of a mortgage. Your table should show Month number, New Principal, Interest Paid during the month. If the load is paid off, your table needs to properly stop.
*********************************** Sample Output *********************************
Interest Calculations
Calculating for:
interest rate/year =6
starting principal=10000
payment/month =1000
months =60
Month Number | Principal | Interest |
---|---|---|
1 | 9050.00 | 50.00 |
2 | 8095.25 | 45.25 |
3 | 7135.73 | 40.48 |
4 | 6171.40 | 35.68 |
5 | 5202.26 | 30.86 |
6 | 4228.27 | 26.01 |
7 | 3249.41 | 21.14 |
8 | 2265.66 | 16.25 |
9 | 1276.99 | 11.33 |
10 | 283.37 | 6.38 |
Last Payment = 284.79
This includes interest:1.42
**************************************************************************************
HINTS:
Take a look at the "RadioActive" servlet example in the notes. This application is similar to this homework. You can ignore the bar image part ... it's not part of this homework.
You will want to have a loop for the number of 12 * (Number of years). On each iteration of the loop, you will want to calculate the interest paid which is:
interestPaid = (newPrincipal * interest)/(12*100)
The 12 is because of 12 months in a year. The 100 is due to the fact that interest rates (like 6) are computed as 0.06 in interest calculations.
newPrincipal = newPrincipal + interestPaid - monthlyPayment
To get rid of the large number of decimal places that show up when you print a double, there is a format method in the String class that can help. Consider the following code (similar to the printf stuff in System.out):
String sPrinciple = String.format("%.2f", principal);
in Java Servlet
interest.html file
<html>
<head>
<title>Simple Interest</title>
</head>
<body>
<form action="servlet/Interest" method="post">
Interest Rates:<input type="number"
name="rates"/><br/><br/>
Monthly Payment:<input type="number"
name="Amount"/><br/><br/>
Principal:<input type="number"
name="Principal"/><br/><br/>
Number of Years:<input type="number"
name="Years"/><br><br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
interest.java file
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Interest extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Int r=request.getParameter("rates");
Int a=request.getParameter("Amount");
Float p=request.getParameter("Principal");
Int y=request.getParameter("Years");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement(
"insert into interestcalculation values(?,?,?,?)");
ps.setInt(1,r);
ps.setInt(2,a);
ps.setFloat(3,p);
ps.setInt(4,y);
int i=ps.executeUpdate();
if(i>0)
out.print("You are successfully registered...");
}
catch (Exception e2)
{
System.out.println(e2);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Interest Calculations");
out.println("Calculating for:);
out.println("Interest rates/Year"+ r);
out.println("Starting Principal"+ p);
out.println("Payment/month"+ a);
out.println("months"+ y);
float newPrincipal;
int InterestPaid, number;
newPrincipal=0.0;
number=0;
out.println(" MonthNumber"+ "Principal" +"Interest");
while(newPrincipal>m){
interestPaid= (newPrincipal*r)/(12*100);
newPrincipal= newPrincipal+InterestPaid-a;
out.println(number + newPrincipal + InterestPaid);
number++;
}
out.close();
}
public void destroy() { }
}
web.xml file