/********************************************************************
Program Name: Mortgage Calcuator.Mortgage Amortization Information
Programmers Name: Jake Foster
Program Description:
This class holds loan amortization information for an specific mortgage
loan, apr, term, and payment.
Limitations: Exception handling is not complete.
********************************************************************/
/************* import libraries ************************************/
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
/*******************************************************************/
public class AmortizationTable
{
private static final int FLD_WIDTH = 11;
private AmoritizationData amoritizationTable[];
private MortgageInformation mortgageInfo;
public AmortizationTable(double principal, double term, double apr)
{
mortgageInfo = new MortgageInformation(principal, term, apr);
}
public AmortizationTable(MortgageInformation info)
{
this.mortgageInfo = info;
}
public void DisplayAmortizationTable()
{
JTextArea tableArea = new JTextArea(30, 50);
JScrollPane scrollpane;
String output = new String();
CalculateAmortizationTable();
//include the loan information as the header
output = mortgageInfo.HeaderString() + "\n\n";
//add the table
output += getAmortizationTable();
tableArea.setText(output);
scrollpane = new JScrollPane(tableArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JOptionPane.showMessageDialog(null,
scrollpane,
"Amortization Table",
JOptionPane.INFORMATION_MESSAGE);
}
private void CalculateAmortizationTable()
{
int numPayments = mortgageInfo.NumberPayments();
double intRate = mortgageInfo.MonthlyRate();
double principal = mortgageInfo.getPrincipal();
double payment = mortgageInfo.Payment();
double loanBalance;
double interestPaid;
double principalBalance;
double interestBalance;
double principalPaid;
double totalPaid;
double totalInterestPaid;
//calculate total paid on the loan over the entire term of the loan
totalPaid = numPayments * payment;
//calculate total interest paid over the entire term of the loan
totalInterestPaid = totalPaid - principal;
//make room for the initial entries
amoritizationTable = new AmoritizationData[numPayments + 1];
//order of arguments in constructor are:
//period, loanBalance, principalBalance, interestBalance, principal paid, interestPaid
amoritizationTable[0] = new AmoritizationData(0, totalPaid, principal, totalInterestPaid, 0, 0);
//intialized the Amortization Table to the initial loan values, so start counting at 1
for(int count = 1; count < amoritizationTable.length; count++)
{
//calculate loan balance from previous loan balance and mortgage payment
loanBalance = amoritizationTable[count-1].loanBalance - payment;
//interest payment is the product of the interest rate per period and the remaining balance
//remaining balance is the previous periods Principal Balance
interestPaid = intRate * amoritizationTable[count-1].principalBalance;
//calculate new principal balance from previous principle balance, payment, and interest paid
principalBalance = amoritizationTable[count-1].principalBalance - (payment - interestPaid);
//calculate interest balance using previous interest balance and new interest payment
interestBalance = amoritizationTable[count-1].interestBalance - interestPaid;
//Calculate principal paid using previous principle paid, payment, and interest paid
principalPaid = amoritizationTable[count-1].principalPaid + (payment - interestPaid);
//Calculate total interest paid, using the previous total interest paid and current interest paid
totalInterestPaid = amoritizationTable[count-1].interestPaid + interestPaid;
//generate new data entry in the amortization table
//period, loanBalance, principleBalance, interestBalance, principalPaid, interestPaid
amoritizationTable[count] = new AmoritizationData(count,
loanBalance,
principalBalance,
interestBalance,
principalPaid,
totalInterestPaid);
}
}
private String getAmortizationTable()
{
String output = new String();
NumberFormat mf = NumberFormat.getCurrencyInstance( Locale.US);
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
output = "# \tLoan Balance\tPrin Balance\tInt Balance\tPrin Paid\tInt Paid\n" +
"---\t------------\t------------\t-----------\t-----------\t-----------\n";
for (int i = 0; i < amoritizationTable.length; i++)
{
output = output + amoritizationTable[i].period + "\t" +
Justify.right(mf.format(amoritizationTable[i].loanBalance), FLD_WIDTH) + "\t" +
Justify.right(mf.format(amoritizationTable[i].principalBalance), FLD_WIDTH) + "\t" +
Justify.right(mf.format(amoritizationTable[i].interestBalance), FLD_WIDTH) + "\t" +
Justify.right(mf.format(amoritizationTable[i].principalPaid), FLD_WIDTH) + "\t" +
Justify.right(mf.format(amoritizationTable[i].interestPaid), FLD_WIDTH) + "\n";
}
return output;
}
}