*
* Write the program in Java (with a graphical user interface) so that it will allow the user to select which way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from a menu of mortgage loans:
- 7 year at 5.35%
- 15 year at 5.5 %
- 30 year at 5.75%
In either case, display the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert comments in the program to document the program.
*/
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MortCalc4rev
{
public static void main (String[] args)
{
JFrame window = new MortCalcGUI();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
class MortCalcGUI extends JFrame implements ActionListener
{
//declare GUI function variables
protected JButton calcBn1, calcBn2, calcBn3, resetBn, quitBn, calcBn;
protected JLabel pymtLabel, lnAmtLabel, titleLabel, instrLabel, amortTblLabel, lnTrmLabel, lnIntLabel;
protected JTextField pymtFld, lnAmtFld, lnTrmFld, lnIntFld;
protected JTextArea areaAmortTable;
//Declare variables & loan array and initialize with values
double intPd,prinPd,Balance,mnthlyPymt,lnAmount;
int[] lnTrm = {7,15,30}; // loan term for 7 years, 15 years, and 30 years
double[] intRate = {5.35,5.50,5.75}; // interest rates for 7 years, 15 years, and 30 years
double lnAmt = lnAmount;
double trmYrs;
double intRt;
public void actionPerformed (ActionEvent e)
{
//Perform actions based upon which button was pressed (provides looping function)
if ("calcPymt1".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
double pymtAmt = calcPymt1();
pymtFld.setText("" + (currency.format(pymtAmt)));
areaAmortTable.append ("Payment # \t Remaining Balance \t Interest Paid\n");
// Need to set the loan amount to the value from the field first
// You need to do this for each of your loan calculations for 7, 15, and 30 years.
lnAmt = Double.parseDouble(lnAmtFld.getText());
for (int x = 1; x <=(lnTrm[0]*12); x++) //Loop through all monthly payments for each loan
{
Balance=lnAmt; //Set Balance to Loan Amount
//Calculations on monthly payment
intPd=(((intRate[0]/100.0)/12.0)*Balance); //Monthly Interest paid
prinPd=(pymtAmt-intPd); //Applied towards principle
lnAmt=(Balance-prinPd); //loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(lnAmt) + " \t\t " + currency.format(intPd)+"\n");
}
if (Balance <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\t Remaining balance = $0.00");
//payment Number = 0;
//interest Paid = 0.0;
}
}
else if ("calcPymt2".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
double pymtAmt = calcPymt2 ();
pymtFld.setText("" + (currency.format(pymtAmt)));
areaAmortTable.append ("Payment # \t Remaining Balance \t Interest Paid\n");
lnAmt = Double.parseDouble(lnAmtFld.getText());
for (int x = 1; x <=(lnTrm[1]*12); x++) //Loop through all monthly payments for each loan
{
Balance = lnAmt;
//Calculations on monthly payment
intPd=((intRate[1]/100/12)*Balance); //Monthly Interest paid
prinPd=(pymtAmt-intPd); //Applied towards principle
lnAmt=((Balance-prinPd)); //loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(lnAmt) + " \t\t " + currency.format(intPd)+"\n");
}
if (lnAmt <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\t Remaining balance = $0.00");
//paymentNumber = 0;
//Interest Paid = 0.0;
}
}
else if ("calcPymt3".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
double pymtAmt = calcPymt3 ();
pymtFld.setText("" + (currency.format(pymtAmt)));
areaAmortTable.append ("Payment # \t Remaining Balance \t Interest Paid\n");
lnAmt = Double.parseDouble(lnAmtFld.getText());
for (int x = 1; x <=(lnTrm[2]*12); x++) //Loop through alll monthly payments for each loan
{
Balance = lnAmt;
//Calculations on monthly payment
intPd=((intRate[2]/100/12)*Balance); //Monthly Interest paid
prinPd=(pymtAmt-intPd); //Applied towards principle
lnAmt=((Balance-prinPd)); //loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(lnAmt) + " \t\t " + currency.format(intPd)+"\n");
}
if (lnAmt <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\t Remaining balance = $0.00");
//paymentNumber = 0;
//Interest Paid = 0.0;
}
}
else if ("Clear".equals(e.getActionCommand()))
{
clrFld ();
}
else
{
//end and close application
System.exit(0);
}
}
public double calcPymt1 ()
{
//Check for valid numeric imput
try
{
//Perform payment calculations if input is valid
pymtFld.setText ("");
double Pymt =(lnAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)),lnTrm[0]*12));
return Pymt;
}
catch (NumberFormatException event)
{
//Display error message if input is invalid
JOptionPane.showMessageDialog(null, " Invalid Entry!\nPlease enter only numeric values!!", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public double calcPymt2 ()
{
//Check for valid numeric imput
try
{
//Perform payment calculations if input is valid
pymtFld.setText ("");
double Pymt =(lnAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)),lnTrm[1]*12));
return Pymt;
}
catch (NumberFormatException event)
{
//Display error message if input is invalid
JOptionPane.showMessageDialog(null, " Invalid Entry!\nPlease enter only numeric values!!", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public double calcPymt3 ()
{
//Check for valid numeric imput
try
{
//Perform payment calculations if input is valid
pymtFld.setText ("");
double Pymt =(lnAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)),lnTrm[2]*12));
return Pymt;
}
catch (NumberFormatException event)
{
//Display error message if input is invalid
JOptionPane.showMessageDialog(null, " Invalid Entry!\nPlease enter only numeric values!!", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
public void clrFld ()
{
//Set all text fields to blank
pymtFld.setText ("");
lnAmtFld.setText ("");
areaAmortTable.setText ("");
lnTrmFld.setText("");
lnIntFld.setText("");
}
public MortCalcGUI ()
{
//Set up and initialize buttons for GUI
calcBn1 = new JButton ("5.35 / 7yrs");
calcBn1.setActionCommand ("calcPymt1");
calcBn2 = new JButton ("5.50 / 15yrs");
calcBn2.setActionCommand ("calcPymt2");
calcBn3 = new JButton ("5.75 / 30yrs");
calcBn3.setActionCommand ("calcPymt3");
calcBn = new JButton ("Calculate");
calcBn.setActionCommand ("calcPmt");
resetBn = new JButton ("Reset");
resetBn.setActionCommand ("Clear");
quitBn = new JButton ("Exit");
quitBn.setActionCommand ("Exit");
//Set up labels and field sizes for GUI
titleLabel = new JLabel ("Welcome to the McBride's Mortgage Calculator");
instrLabel = new JLabel ("Enter the amount of the loan and choose from the selection of Rates/Terms:");
pymtLabel = new JLabel ("Monthly Payment:");
lnAmtLabel = new JLabel ("Enter the amount:");
lnTrmLabel = new JLabel ("Enter the term:");
lnIntLabel = new JLabel ("Enter the rate:");
lnAmtFld = new JTextField ("", 10);
pymtFld = new JTextField ("", 12);
lnTrmFld = new JTextField ("", 3);
lnIntFld = new JTextField ("", 5);
amortTblLabel = new JLabel ("Amortization Table");
areaAmortTable = new JTextArea (10, 300);
JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//Set up listeners for each button
calcBn1.addActionListener(this);
calcBn2.addActionListener(this);
calcBn3.addActionListener(this);
calcBn.addActionListener(this);
resetBn.addActionListener(this);
quitBn.addActionListener(this);
//Construct GUI and set layout
JPanel myContentPane = new JPanel();
myContentPane.setLayout (null);
myContentPane.add (titleLabel);
titleLabel.setBounds (200, 30, 700, 15);
myContentPane.add(instrLabel);
instrLabel.setBounds (120, 70, 450, 15);
myContentPane.add (lnAmtLabel);
lnAmtLabel.setBounds (20, 110, 100, 25);
myContentPane.add (lnAmtFld);
lnAmtFld.setBounds (127, 110, 100, 25);
myContentPane.add (lnTrmLabel);
lnTrmLabel.setBounds (230, 110, 100, 25);
myContentPane.add (lnIntLabel);
lnIntLabel.setBounds (430, 110, 100, 25);
myContentPane.add(calcBn1);
calcBn1.setBounds (75, 150, 125, 30);
myContentPane.add (calcBn2);
calcBn2.setBounds (250, 150, 125, 30);
myContentPane.add (calcBn3);
calcBn3.setBounds (420, 150, 125, 30);
myContentPane.add (pymtLabel);
pymtLabel.setBounds (130, 200, 100, 25);
myContentPane.add (lnTrmFld);
lnTrmFld.setBounds (320, 110, 100, 25);
myContentPane.add (lnIntFld);
lnIntFld.setBounds (520, 110, 100, 25);
myContentPane.add (pymtFld);
pymtFld.setBounds (300, 200, 100, 25);
pymtFld.setEditable (false);
myContentPane.add (amortTblLabel);
amortTblLabel.setBounds (240, 250, 300, 25);
// Add the scrollpane, set its bounds and set the table as not editable.
// Table is part of scrollpane now, so add and place only the scrollpane.
myContentPane.add (scrollPane);
scrollPane.setBounds (125, 280, 400, 270);
areaAmortTable.setEditable(false);
myContentPane.add (resetBn);
resetBn.setBounds (250, 570, 125, 30);
myContentPane.add (quitBn);
quitBn.setBounds (400, 570, 125, 30);
myContentPane.add(calcBn);
calcBn.setBounds (110, 570, 125, 30);
this.setContentPane(myContentPane);
this.pack();
this.setTitle("McBride's Financial Services ");
//Set window size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(675, 700);
}
public double lnAmount ()
{
double lnAmount = Double.parseDouble(lnAmtFld.getText());
return lnAmount;
}
}
This post has been edited by edwiprkid: 06 January 2009 - 08:01 AM

New Topic/Question
Reply




MultiQuote




|