<%@ page import="java.text.*,java.util.*,Amortization,AmortizationFactory" %>
<%

// process page

DecimalFormat df = (DecimalFormat)NumberFormat.getInstance(); df.setGroupingSize(3);
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);

String action = request.getParameter("action"); String debtString = request.getParameter("debt"); String paymentString = request.getParameter("payment"); String aprString = request.getParameter("apr"); String totalPaymentString = request.getParameter("totalPayment"); String yearsString = request.getParameter("years"); String totalInterestString = request.getParameter("totalInterest"); String periodType = request.getParameter("periodType");

double debt = -1;
double apr = -1;
int years = -1;
int periods = -1;
double payment = -1;
double totalPayment = -1;
double totalInterest = -1;
double[] principalPaid = new double[0]; double[] interestPaid = new double[0]; double[] cumulativePrincipal = new double[0]; double[] cumulativeInterest = new double[0]; double[] cumulativePayment = new double[0]; double[] principalBalance = new double[0]; Amortization amortization = null;

boolean hasError = false;
String message = null;

if (action == null || action.equals("Clear")) {

        debtString = null;
        paymentString = null;
        aprString = null;
        totalPaymentString = null;
        yearsString = null;
        totalInterestString = null;
        periodType = null;

}
else if (debtString == null || debtString.equals("")) {

        hasError = true;
        message = "Enter a positive decimal # for <i>Initial Debt</i>!";

}
else if (aprString == null || aprString.equals("")) {

        hasError = true;
        message = "Enter a positive decimal # for <i>Periodic Rate</i>!";

}
else if (yearsString == null || yearsString.equals("")) {

        hasError = true;
        message = "Enter a positive integer for <i># of Periods</i>!";

}
else if (periodType == null || periodType.equals("")) {

        hasError = true;
        message = "Check either <i>Periods by Months</i> or <i>Periods by Years</i>!";

}
else
{

        try
        {
            debt = df.parse(debtString).doubleValue();
            apr = Double.valueOf(aprString).doubleValue();
            years  = Integer.valueOf(yearsString).intValue();
        }
        catch (NumberFormatException nfe)
        {
            hasError = true;
            message = "Enter a decimal # for <i>Initial Debt</i>, a decimal # for <i>Periodic Rate</i> and an integer for <i># of Periods</i>!";
        }

        if (!hasError)
        {
            if (debt <= 0 || apr <= 0 || years <= 0)
            {
                hasError = true;
                message = "Enter a decimal # for <i>Initial Debt</i>, a decimal # for <i>Periodic Rate</i> and an integer for <i># of Periods</i>!";
            }
            else
            {
                if (periodType.equals("yearly"))
                {
                    amortization = AmortizationFactory.create(debt, apr, years, AmortizationFactory.YEARLY);
                }
                else if (periodType.equals("monthly"))
                {
                    amortization = AmortizationFactory.create(debt, apr, years, AmortizationFactory.MONTHLY);
                }
                payment = amortization.getPayment();
                totalPayment = amortization.getTotalPayment();
                totalInterest = amortization.getTotalInterest();
                debtString = df.format(debt);
                paymentString = df.format(payment);
                totalPaymentString = df.format(totalPayment);
                totalInterestString = df.format(totalInterest);
        
                if (action.equals("Amortization Schedule"))
                {
                    principalPaid = amortization.getPrincipalPaid();
                    interestPaid = amortization.getInterestPaid();
                    cumulativePrincipal = amortization.getCumulativePrincipal();
                    cumulativeInterest = amortization.getCumulativeInterest();
                    cumulativePayment = amortization.getCumulativePayment();
                    principalBalance = amortization.getPrincipalBalance();
                }
            }
        }

}

// print page
%>
<html>
<head><title>Amortization Calculator</title></head> <body bgcolor="white">
<font size=3>
Enter <i>Initial Debt</i>, <i>APR</i>, <i># of Years</i>, and select <i>Periods by Years</i> or <i>Periods by Months</i> below to calculate <i>Periodic Payment</i>, <i>Total Payment</i>, <i>Total Interest</i>, and optionally <i>Amortization Schedule</i>. <p>
<form method=POST action="Amortization.jsp"> <table border=0>
<tr>
<td><i>Initial Debt</i>
<%

if (debtString == null || debtString.equals("")) {
%>
<td><input type=text name=debt size=15 maxlength=15 align=right> <%

}
else
{
%>
<td><input type=text name=debt value=<%= debtString %> size=15 maxlength=15 align=right> <%

}
%>
<td><i>Periodic Payment</i>
<%

if (paymentString == null || paymentString.equals("")) {
%>
<td><input type=text name=payment size=15 maxlength=15 align=right> <%

}
else
{
%>
<td><input type=text name=payment value=<%= paymentString %> size=15 maxlength=15 align=right> <%

}
%>
<tr>
<td><i>APR</i>
<%

if (aprString == null || aprString.equals("")) {
%>
<td><input type=text name=apr size=15 maxlength=15> <%

}
else
{
%>
<td><input type=text name=apr value=<%= aprString %> size=15 maxlength=15> <%

}
%>
<td><i>Total Payment</i>
<%

if (totalPaymentString == null || totalPaymentString.equals("")) {
%>
<td><input type=text name=totalPayment size=15 maxlength=15> <%

}
else
{
%>
<td><input type=text name=totalPayment value=<%= totalPaymentString %> size=15 maxlength=15> <%

}
%>
<tr>
<td><i># of Years</i>
<%

if (yearsString == null || yearsString.equals("")) {
%>
<td><input type=text name=years size=15 maxlength=15> <%

}
else
{
%>
<td><input type=text name=years value=<%= yearsString %> size=15 maxlength=15> <%

}
%>
<td><i>Total Interest</i>
<%

if (totalInterestString == null || totalInterestString.equals("")) {
%>
<td><input type=text name=totalInterest size=15 maxlength=15> <%

}
else
{
%>
<td><input type=text name=totalInterest value=<%= totalInterestString %> size=15 maxlength=15> <%

}
%>
</table>
<%

if (periodType == null || periodType.equals("")) {
%>
<i><input type=radio name=periodType value="yearly">Periods by Years</i> <br>
<i><input type=radio name=periodType value="monthly">Periods by Months</i> <%

}
else if (periodType.equals("yearly")) {
%>
<i><input type=radio checked name=periodType value="yearly">Periods by Years</i> <br>
<i><input type=radio name=periodType value="monthly">Periods by Months</i> <%

}
else if (periodType.equals("monthly")) {
%>
<br>
<i><input type=radio name=periodType value="yearly">Periods by Years</i> <br>
<i><input type=radio checked name=periodType value="monthly">Periods by Months</i> <%

}
%>
<br>
<p>
<i>
<td><input type=submit name=action value="Calculate"> <td><input type=submit name=action value="Clear"> <td><input type=submit name=action value="Amortization Schedule"> </i>
</form>
<p>
<%

if (action == null || action.equals("Clear")) {
}
else if (hasError)
{
%>
<%= message %>
<%

}
else if (action.equals("Amortization Schedule")) {
%>
<table border=1>
<tr><th>Period<th>Principal Paid<th>Interest Paid<th>Cumulative Principal<th>Cumulative interest<th>Cumulative Payment<th>Principal Balance <%

        for (int period = 0; period <= amortization.getPeriods(); period++)
        {

%>
<tr>
<td align=right><%= period %>
<td align=right><%= df.format(principalPaid[period]) %> <td align=right><%= df.format(interestPaid[period]) %> <td align=right><%= df.format(cumulativePrincipal[period]) %> <td align=right><%= df.format(cumulativeInterest[period]) %> <td align=right><%= df.format(cumulativePayment[period]) %> <td align=right><%= df.format(principalBalance[period]) %> <%

}
%>
</table>
<%

}
%>
</font>
<hr>
<a href="http://www.computing-wisdom.com"><i>Computing Wisdom In. Home Page</i></a> </body>
</html>