0

I have my calculator GUI to display as I want it too. When I put in my mortgage amount and choose an option from the drop down box and hit calculate everything works as it should but when I try to manually put in the terms and interest rate and calculate nothing happens. I think I am missing something in the if statements but am not quite sure what. Could someone please look at my code and steer me in the right direction? I really would appreciate any input you could offer.

/**
* SR-mf-003 Change Request 5
* Lisa Condon
* Week 4
* PRG 421 Java Programming II
* Mortgage Calculator with GUI
* Calculate and display mortgage payment amount from user input of amounts
* 7 yrs at 5.35%
* 15 years at 5.5%
* 30 years at 5.75%
* Use an array for the different loans
* Display mortgage payment, loan balance, and interest paid
* Allow user to manually input amount, rate, term if they choose.
* March 29, 2010

* Write a program in java (with GUI)
*/


import javax.swing.*;

import java.awt.*;

import java.text.*;

import java.awt.event.*;



public class MortgageCalculatorWk5A extends JFrame implements ActionListener, ItemListener {

            double userAmount = 0.0;

            double userAmount2 = 0.0;

            double userInterest = 0.0;

            double userTerm = 0.0;

            int months = 0;

            double annualInterestRate [] = {5.35, 5.5, 5.75};

            double monthlyInterest = 0;

            int years [] = {7, 15, 30};

            double loanMonths = 0.0;

            double monthlyPayment = 0.00;

            double loanBalance = 0;

            double currentMonthInterest = 0.00;

            double currentMonthPrinciple = 0.00;

            DecimalFormat eachPayment = new DecimalFormat ("$###,##0.00");;



//Instantiate GUI Component



// row1

JPanel row1 = new JPanel();

JLabel lblAmount = new JLabel(" Mortgage: $", JLabel.LEFT);

JTextField txtAmount = new JTextField(10);



// combo for interest & term

JLabel lblcombo = new JLabel ("Term/Interest");

String cboData[] = {"Select Term","7 years at 5.35%","15 years at 5.50%","30 years at 5.75%"};

JComboBox comboTermInterest = new JComboBox(cboData);


//term

JLabel lblTerm = new JLabel("Term:");

JTextField txtTerm = new JTextField(10);


//interest rate

JLabel lblRate = new JLabel("Interest Rate:");

JTextField txtRate = new JTextField(10);

//payment

JLabel lblPayment = new JLabel("Payment:");

JTextField txtPayment = new JTextField(10);




// row2

JPanel row2= new JPanel(new BorderLayout());

String pad = "                             ";

JLabel lblHeader = new JLabel("Payment #"+pad+"Payment =Amount:"+pad+"Interest Paid:"+pad+"New Balance:");

JTextArea txtResults = new JTextArea(10, 53);

JScrollPane textPane = new JScrollPane(txtResults);





//row 3

JPanel row3 = new JPanel(new GridLayout(1,3,75,1));

JButton calculateButton = new JButton("Calculate");

JButton clearButton = new JButton("Clear");

JButton exitButton = new JButton("Exit");



//Build GUI

public MortgageCalculatorWk5A()            {

            super("Mortgage Calculator");

setBounds(350, 250, 600, 250);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



row1.add(lblAmount);

row1.add(txtAmount);

row1.add(lblcombo);

row1.add(comboTermInterest);

row1.add(lblTerm);

row1.add(txtTerm);

row1.add(lblRate);

row1.add(txtRate);

row1.add(lblPayment);

row1.add(txtPayment);



txtPayment.setEditable(false);

textPane.setPreferredSize(new Dimension(610,200));


row2.add(lblHeader,BorderLayout.NORTH);
           row2.add(textPane,BorderLayout.CENTER);



//Create Calculate button and add listener
           comboTermInterest.addItemListener(this);
           calculateButton.addActionListener(this);
           clearButton.addActionListener(this);
		   exitButton.addActionListener(this);


row3.add(exitButton);

row3.add(clearButton);

row3.add(calculateButton);



getContentPane().add(row1,BorderLayout.NORTH);
            getContentPane().add(row2,BorderLayout.CENTER);
            getContentPane().add(row3,BorderLayout.SOUTH);



            pack(); }

            // Actions for combo box selection



public void itemStateChanged(ItemEvent event) {

   Object select = event.getItem();

               String selection = select.toString();


   if (selection == cboData[0]) {

  }


   if (selection == cboData[1]) {

	   userInterest = annualInterestRate [0];
       userTerm = years [0];


  }

  if (selection == cboData[2]) {

	   userInterest = annualInterestRate [1];
       userTerm = years [1];

  }

 if (selection == cboData[3]) {

	   userInterest = annualInterestRate [2];
       userTerm = years [2];

  }
}


//Define actions performed for buttons

            public void actionPerformed(ActionEvent e) {

            if (e.getSource() == calculateButton)

            {
			   userAmount = Double.parseDouble(txtAmount.getText());
			   //userInterest = Double.parseDouble (txtInterest.getText());
			   //userTerm = Double.parseDouble (txtTerm.getText());

			   CalculateMonthlyPayment() ;

			   txtPayment.setText( eachPayment.format( monthlyPayment)+"    ");

			   repaint();
			   loanMonths = userTerm * 12;
			   loanBalance = userAmount;

			   for (months = 1; months <= loanMonths; months++)
			   {

				   //monthly interest calculation

				   monthlyInterest = userInterest / (1200.00);

				   //current months interest amount calculation

				   currentMonthInterest = loanBalance * monthlyInterest;

				   //current months principale amount calculation

				   currentMonthPrinciple = monthlyPayment - currentMonthInterest;

				   //current months interest minus loan amount
				  //provides new principle amount
				  loanBalance = loanBalance - currentMonthPrinciple;

				  txtResults.append("    " + months + "\t\t     " + eachPayment.format(monthlyPayment)
				  			+ "\t\t" + eachPayment.format(currentMonthInterest)
				  			+ "\t\t"
				  			+ eachPayment.format(loanBalance) + "\n");
		}

     }

            else if (e.getSource() == clearButton)

            {
			//clear fields

			txtAmount.setText ("");
			txtPayment.setText("");
            txtTerm.setText("");
            txtRate.setText("");
			userAmount = 0.0;
			txtPayment.setText("");
			txtResults.setText("");

  			}

            else if (e.getSource() == exitButton)

            {

				  System.exit(0);

            }

}



//Mortgage Payment Calculations

            double CalculateMonthlyPayment()       {

            return monthlyPayment = (userAmount * ((1 + (userInterest*.01)/12)-1) / (1-(Math.pow(1 + (userInterest/1200),-(userTerm*12)))));

			}

public static void main(String[] args)      {



            new MortgageCalculatorWk5A().setVisible(true);

			}

	}
3
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by jwenting
-3

Good luck in finding help as you won't get it here people are too of themselves to help us :(. Try a more helpful forum like dream.in.code :)

Votes + Comments
Yeah, DIC is full of clueless dicks
whinger...
0

We're happy to help anyone who puts in some effort to help themselves (unlike probable dsmush...).

Skimming over your source I see nowhere that you actually read the values from the screen. As a result, what you put into the input boxes and other user interface controls won't get used, only the defaults.
On changing a user interface control, trigger an event handler that updates the variables you use to calculate the result, that should do the trick.

0

We're happy to help anyone who puts in some effort to help themselves (unlike probable dsmush...).

Skimming over your source I see nowhere that you actually read the values from the screen. As a result, what you put into the input boxes and other user interface controls won't get used, only the defaults.
On changing a user interface control, trigger an event handler that updates the variables you use to calculate the result, that should do the trick.

Sorry to disappoint but I spent week myself trying. Coming here was my last option...

0

and so you try to ruin other peoples' threads with your ranting rather than try to help them?
Good show, going to make you a lot of friends here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.