Problem Combo in GUI does not react

Mortgage Calculator CR#6

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 4851 Views - Last Post: 22 March 2009 - 06:41 PM Rate Topic: -----

#1 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Problem Combo in GUI does not react

Post icon  Posted 21 March 2009 - 12:44 PM

Paul and Locke,

Thanks again. I'm really sorry I'm not understanding this. There is just too much on my plate. I'm having a hard time concentrating. I'm praying and it looks like at least I have my Java helpers. Thanks again.

I will work on this all day and all day tomorrow to get it right.

Well I did think the term and termMonths variables were not declared so I added them. But it still is not calculating the monthly payments and putting it in the window.

I cleaned up the code.


/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

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 %, and 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.

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */


import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;


// Instance Variables
JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;
JComboBox cOption;




public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");

	 String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

	amountTf = new JTextField();
	  JComboBox comboBox1 = new JComboBox(Labels);

	cOption = new JComboBox(Labels);




termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");



frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);
frame.add(comboBox1);


/* Remove these frames when action listener is working 
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  */
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
					  
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");

	
	String Labels = (String) cOption.getSelectedItem(); 
   
   if (Labels.equals("7 years at 5.35%")) 
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;
					 double termMonths = months;   
				   double term = 7;	
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			
	 if (Labels.equals("15 years at 5.5%"))
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055; 
					 double termMonths = months;
					 double term = 15;
					  
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
	if (Labels.equals("30 years at 5.75%"))
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;
					 double termMonths = months;
					 double term = 15; 
							
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
/* This is the area for the amortization process */



// displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * interestRateMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0);
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 System.out.println(balance);

/**/
	 
	 /* Put amortization into a Jtextarea */
	 
	 //payments - principal show each payment


}}


This post has been edited by AdamSpeight2008: 08 June 2010 - 12:55 PM
Reason for edit:: Removed personal information


Is This A Good Question/Topic? 0
  • +

Replies To: Problem Combo in GUI does not react

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 01:17 PM

Well, in all of your calculations...you have double payments = ...;. You need to remove the double, or that variable will go out of scope when you try to output it, and you won't output the right variable.

I'll edit or repost when I find more things to correct/adjust.

Another thing. The while line has a semicolon after it. When you remove that and include the output statements in the loop...there doesn't seem to be any output for some reason. I'm still trying to figure this out.

This post has been edited by Locke: 21 March 2009 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 02:00 PM

View PostLocke, on 21 Mar, 2009 - 12:17 PM, said:

Well, in all of your calculations...you have double payments = ...;. You need to remove the double, or that variable will go out of scope when you try to output it, and you won't output the right variable.

I'll edit or repost when I find more things to correct/adjust.

Another thing. The while line has a semicolon after it. When you remove that and include the output statements in the loop...there doesn't seem to be any output for some reason. I'm still trying to figure this out.


I don't see the problem... It looks like it should work. Should we move the double amount get text line before the If's???
Was This Post Helpful? 0
  • +
  • -

#4 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 02:08 PM

View Postgrabowpd, on 21 Mar, 2009 - 01:00 PM, said:

View PostLocke, on 21 Mar, 2009 - 12:17 PM, said:

Well, in all of your calculations...you have double payments = ...;. You need to remove the double, or that variable will go out of scope when you try to output it, and you won't output the right variable.

I'll edit or repost when I find more things to correct/adjust.

Another thing. The while line has a semicolon after it. When you remove that and include the output statements in the loop...there doesn't seem to be any output for some reason. I'm still trying to figure this out.


I don't see the problem... It looks like it should work. Should we move the double amount get text line before the If's???


well moving that at least proves that it is getting the proper input for the amount. But it does not calculate.
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 02:16 PM

Well, ok. Martyr2 gave me the rundown.

The first problem can be resolved by switching all of the if statements with the following.

if (Labels.equalsIgnoreCase("7 years at 5.35%")) -- change that and all of the rest like it.

Now, that was a big problem, because the case-sensitivity made it skip all of the if statements.

You also have some variable naming problems. When you try to output, the variables that you need (and have calculated) have already been eliminated because of scope. You need to remove all of the types from the variables in the if statements and see where that gets you. You need to make them instance variables.

public class MortgageCalculatorCR62
{
    double amount, interest, payments;
    int years, months, termMonths;

    // in the "if"s down below

    if (.......)
    {
        amount = Double.parseDouble(amountTf.getText());
        years = // 7, 15 or 30 depending on what if statement you're in.
        months = years * 12;
        // 'term' is the same as years

        payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

        paymentsTf.setText("" + decimalPlaces.format(payments));

        termMonths = months; // loop controller
    }

    while (termMonths > 0)
    {
        // calculations and outputs here. (the same as before)

        termMonths--;
    }
}


I think that will get us on the right track. :)

This post has been edited by Locke: 21 March 2009 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#6 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 02:56 PM

Ok, it outputs, but it only does it for one calculation if you change the drop down you still get the same answer.

You totally lost me in the posted code. Because I tried to put that in the code and it went all crazy again on compile errors.
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 03:03 PM

The variables in my code are instance variables, just like the swing components you have now. The other parts go in the if statements.

The if statement that I posted is just a template. Tweak it so slightly depending on the condition of the if statement that you're currently in.
Was This Post Helpful? 0
  • +
  • -

#8 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 03:11 PM

View PostLocke, on 21 Mar, 2009 - 02:03 PM, said:

The variables in my code are instance variables, just like the swing components you have now. The other parts go in the if statements.

The if statement that I posted is just a template. Tweak it so slightly depending on the condition of the if statement that you're currently in.



Yes that is what I was trying to do, and I tried it twice... I will try again.
Was This Post Helpful? 0
  • +
  • -

#9 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 03:32 PM

Ok, I believe they are the same. The program is still skipping over the other if statements.

And it does not output the amortize payment in the jTextField.

Is this some come of experiment on my life to measure how much I will take before I snap? lol????




/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

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 %, and 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.

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */


import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount, interest, payments;
double term;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;
int years, months;
  


// Instance Variables
JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;
JComboBox cOption;




public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");

	 String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

	amountTf = new JTextField();
	  JComboBox comboBox1 = new JComboBox(Labels);

	cOption = new JComboBox(Labels);




termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");



frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);
frame.add(comboBox1);


/* Remove these frames when action listener is working 
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  */
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
					  
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");
//  double amount = Double.parseDouble(amountTf.getText());
	
	String Labels = (String) cOption.getSelectedItem(); 
   
   if (Labels.equalsIgnoreCase("7 years at 5.35%")) 

	
/*		 amount = Double.parseDouble(amountTf.getText());
		years = // 7, 15 or 30 depending on what if statement you're in. 
				 months = years * 12;		// 'term' is the same as years 
		 payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12))))); 
				paymentsTf.setText("" + decimalPlaces.format(payments)); 
						 termMonths = months; // loop controller	*/
			{			
			 double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double interest	= .0535;
				double termMonths = months;   
				double term = 7;	
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			 
	 if (Labels.equalsIgnoreCase("15 years at 5.5%"))
			{			
		   double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055; 
					 double termMonths = months;
					 double term = 15;
					  
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
	if (Labels.equalsIgnoreCase("30 years at 5.75%"))
			{			
		 double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;
					 double termMonths = months;
					 double term = 15; 
							
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
		  
		 		  
/* This is the area for the amortization process */



// displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * termMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0)
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateaMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 System.out.println(balance);

/**/
	 
	 /* Put amortization into a Jtextarea */
	 
	 //payments - principal show each payment


}}



This post has been edited by no2pencil: 03 June 2010 - 09:37 PM
Reason for edit:: Removed personal information

Was This Post Helpful? 0
  • +
  • -

#10 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 03:53 PM

Oh, boy I'm loosing my mind on this.

I feel like we are back at where we started from...
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 04:12 PM

Make your structure in the calculate() method into an if-else if structure. That is why you're getting the same value for each calculation.

if (Lables.equalsIgnoreCase("7 years at 5.35%"))
{
    // ...
}

else if (Lables.equalsIgnoreCase("15 years at 5.5%"))
{
    // ...
}

else // no if needed here...since the only other case is the 30 year one.
{
    // ...
}

Was This Post Helpful? 0
  • +
  • -

#12 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 04:42 PM

Oh this is one bad experiment...

I would not recommend anyone to take this class. You are just thrown into a fire pit without a prayer.

I consider myself a more than above average student. But I just do not understand this Java. I do not see how any students can get this kind of course work done. Four programs in five weeks.




/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

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 %, and 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.

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */


import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount, interest, payments;
double term;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;
int years, months;
  


// Instance Variables
JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;
JComboBox cOption;




public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");

	 String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

	amountTf = new JTextField();
	  JComboBox comboBox1 = new JComboBox(Labels);

	cOption = new JComboBox(Labels);




termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");



frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);
frame.add(comboBox1);


/* Remove these frames when action listener is working 
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  */
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
					  
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");
//  double amount = Double.parseDouble(amountTf.getText());
	
	String Labels = (String) cOption.getSelectedItem(); 
   
   if (Labels.equalsIgnoreCase("7 years at 5.35%")) 

	
			{			
			 double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double interest	= .0535;
				double termMonths = months;   
				double term = 7;	
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));
					 
					 amortizeTf.setText("" + decimalPlaces.format(balance));
			}		 
			
			{	 
				
						 
   if	 (Labels.equalsIgnoreCase("15 years at 5.5%"))
			{			
		   double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055; 
					 double termMonths = months;
					 double term = 15;
					  
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
				
				 monthlyInterestPayment = (amount * termMonths);
   monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
				
				 amortizeTf.setText("" + decimalPlaces.format(monthlyInterestPayment));
			}
 else
			{			
		 double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;
					 double termMonths = months;
					 double term = 30; 
							
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
			 amortizeTf.setText("" + decimalPlaces.format(balance));
			
		}
		
		  
		 		  
/* This is the area for the amortization process */



// displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * term);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0)
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
   monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 
amortizeTf.setText("" + decimalPlaces.format(amount-payments));


/**/
	 
	 /* Put amortization into a Jtextarea */
	 
	 //payments - principal show each payment


}}}



This post has been edited by no2pencil: 03 June 2010 - 09:37 PM
Reason for edit:: Removed personal information

Was This Post Helpful? 1
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 05:20 PM

I copied/pasted your code and made a few changes, and it prints out a table now, but the calculations are off. Double check your formulas and your ordering of output of variables.

I've said to do this before. Remove the type names from the variables in the if statements.

if (Labels.equalsIgnoreCase("7 years at 5.35%"))
{
    amount = ...;
    years = ...;
    months = years * 12;
    interest = ...;
}


NO type names. On any of them. That will avoid the scope errors. Repeat for the other if statements.

This post has been edited by Locke: 21 March 2009 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#14 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 21 March 2009 - 07:15 PM

View PostLocke, on 21 Mar, 2009 - 04:20 PM, said:

I copied/pasted your code and made a few changes, and it prints out a table now, but the calculations are off. Double check your formulas and your ordering of output of variables.

I've said to do this before. Remove the type names from the variables in the if statements.

if (Labels.equalsIgnoreCase("7 years at 5.35%"))
{
    amount = ...;
    years = ...;
    months = years * 12;
    interest = ...;
}


NO type names. On any of them. That will avoid the scope errors. Repeat for the other if statements.


You can't be serious.. the amount was already declared in the get text.

amount ...;

What?

Remove the type names...

Oh, I'm so stupid... you win
Was This Post Helpful? 0
  • +
  • -

#15 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: Problem Combo in GUI does not react

Posted 22 March 2009 - 06:48 AM

View PostLocke, on 21 Mar, 2009 - 04:20 PM, said:

I copied/pasted your code and made a few changes, and it prints out a table now, but the calculations are off. Double check your formulas and your ordering of output of variables.

I've said to do this before. Remove the type names from the variables in the if statements.

if (Labels.equalsIgnoreCase("7 years at 5.35%"))
{
    amount = ...;
    years = ...;
    months = years * 12;
    interest = ...;
}


NO type names. On any of them. That will avoid the scope errors. Repeat for the other if statements.


Where did you put the code? You said you copy and pasted it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2