Java Program

Mortgage Calculator Array and User Input

Page 1 of 1

10 Replies - 1804 Views - Last Post: 06 January 2009 - 08:00 AM Rate Topic: -----

#1 edwiprkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 09

Java Program

Posted 05 January 2009 - 07:52 PM

Please help, I am having problems with the user input and array for amortization table.

*
* 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


Is This A Good Question/Topic? 0
  • +

Replies To: Java Program

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java Program

Posted 05 January 2009 - 08:02 PM

Welcome!

Please post your code examples using tags:

:code:

You can edit your posting to add the code tags.
Was This Post Helpful? 0
  • +
  • -

#3 edwiprkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 09

Re: Java Program

Posted 05 January 2009 - 08:46 PM

View Postn8wxs, on 5 Jan, 2009 - 07:02 PM, said:

Welcome!

Please post your code examples using tags:

/**

*
* Date: 12/26/2008
*
* 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;
	}
}




You can edit your posting to add the code tags.

This post has been edited by edwiprkid: 05 January 2009 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java Program

Posted 05 January 2009 - 08:57 PM

Now, what problem(s) are you having? Where?
Was This Post Helpful? 0
  • +
  • -

#5 edwiprkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 09

Re: Java Program

Posted 05 January 2009 - 09:34 PM

View Postn8wxs, on 5 Jan, 2009 - 07:57 PM, said:

Now, what problem(s) are you having? Where?

Thank you for the response. This is my first time using this forum for advice. I need get the calculate button to calculate from the user input fields and display the information in the payment field and amortization table. For some reason I can put the text in the input fields but when I depress the calculate button I do not get the payment displayed in the payment text field and the amortization table.

This post has been edited by edwiprkid: 05 January 2009 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java Program

Posted 05 January 2009 - 09:48 PM

View Postedwiprkid, on 5 Jan, 2009 - 08:34 PM, said:

Thank you for the response. This is my first time using this forum for advice. I need get the calculate button to calculate from the user input fields and display the information in the payment field and amortization table. For some reason I can put the text in the input fields but when I depress the calculate button I do not get the payment displayed in the payment text field and the amortization table.


That's because there isn't any code that handles calcBn.setActionCommand ("calcPmt");
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();
...
	else if ("calcPymt2".equals(e.getActionCommand()))
...
	else if ("calcPymt3".equals(e.getActionCommand()))
...
	else if ("Clear".equals(e.getActionCommand()))
...
	else
	{
		//end and close application
		System.exit(0);
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 edwiprkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 09

Re: Java Program

Posted 05 January 2009 - 10:21 PM

View Postn8wxs, on 5 Jan, 2009 - 08:48 PM, said:

View Postedwiprkid, on 5 Jan, 2009 - 08:34 PM, said:

Thank you for the response. This is my first time using this forum for advice. I need get the calculate button to calculate from the user input fields and display the information in the payment field and amortization table. For some reason I can put the text in the input fields but when I depress the calculate button I do not get the payment displayed in the payment text field and the amortization table.


That's because there isn't any code that handles calcBn.setActionCommand ("calcPmt");
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();
...
	else if ("calcPymt2".equals(e.getActionCommand()))
...
	else if ("calcPymt3".equals(e.getActionCommand()))
...
	else if ("Clear".equals(e.getActionCommand()))
...
	else
	{
		//end and close application
		System.exit(0);
	}
}


Quote

Thank you. I really appreciate your assistance. My next question is how do I define the index element "[ ]" for the the array.



for (int x = 1; x <=(lnTrm[2]*12); x++) //Loop through alll monthly payments for each loan
			{




This post has been edited by edwiprkid: 05 January 2009 - 10:22 PM

Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java Program

Posted 05 January 2009 - 10:40 PM

If you wrote this code, I don't understand your question. If you didn't write it, you'll need to post your code showing your attempt(s) to solve your problems.
Was This Post Helpful? 0
  • +
  • -

#9 edwiprkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 09

Re: Java Program

Posted 05 January 2009 - 10:51 PM

View Postn8wxs, on 5 Jan, 2009 - 09:40 PM, said:

If you wrote this code, I don't understand your question. If you didn't write it, you'll need to post your code showing your attempt(s) to solve your problems.

Quote

I added the lines of code to the program but when I depress the calc button the program quits.


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 all 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 ("calcPymt4".equals(e.getActionCommand()))
		{
			DecimalFormat decimalPlaces = new DecimalFormat("0.00");
			NumberFormat currency = NumberFormat.getCurrencyInstance();

			double pymtAmt = calcPymt4 ();

			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 <=(trmYrs*12); x++) //Loop through all monthly payments for each loan
			{
				Balance = lnAmt;

				//Calculations on monthly payment
				intPd=((intRt/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 double calcPymt4 ()
	{
		//Check for valid numeric imput
		try
		{
			//Perform payment calculations if input is valid
			pymtFld.setText ("");
			double Pymt =(lnAmount() * (intRt/100/12)) / (1 - Math.pow(1/(1 + (intRt/100/12)),trmYrs*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 ("calcPymt4");
		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: 05 January 2009 - 10:55 PM

Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java Program

Posted 05 January 2009 - 11:33 PM

It doesn't calculate because you are using uninitialized variables.
Was This Post Helpful? 0
  • +
  • -

#11 edwiprkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 09

Re: Java Program

Posted 06 January 2009 - 08:00 AM

View Postn8wxs, on 6 Jan, 2009 - 12:33 AM, said:

It doesn't calculate because you are using uninitialized variables.

Quote

Thank you for taking the time on this code.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1