else if statement

Mortgage Calculator CR # 6

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 3277 Views - Last Post: 21 March 2009 - 12:47 PM Rate Topic: -----

#16 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: else if statement

Posted 21 March 2009 - 11:30 AM

For the first and second errors...

// you don't need this loop at all.
// remove it from the calculate() method.

for (int x = 0; x < Labels.length; x++)
    cOption.addItem(Labels[i]);


You provided the JComboBox constructor with an array ob things you wanted in the combobox originally, meaning they're already there. That's why you don't need that loop.

To take care of the third error...don't confuse Lables and Labels. Change it in your last if statement. :)

This post has been edited by Locke: 21 March 2009 - 12:48 PM

Was This Post Helpful? 1
  • +
  • -

#17 grabowpd   User is offline

  • D.I.C Head

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

Re: else if statement

Posted 21 March 2009 - 12:03 PM

Ok, that compiles, but when I run it, it does not make the calculation and place it in the Monthly payments window.

I was thinking the formula was wrong because of the term variable. So I tried to just add the double term = 30; and etc to other areas, but it still doesn't do the calculation. It displays zero. And nothing in the payments window.

Then I have to figure out how to display all the payments for that mortgage in a JtextArea window...

Oh boy...

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

Author Phil Grabowski

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.");



// public static void MortgageCalculatorCR62(String args[]) {
   
	 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);

//  Choice cOption = new Choice();

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");
// Labels cOption = new Labels();


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

//Added JcomboBox
  frame.add(comboBox1);

//  frame.add(jComboBox);

/* 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");


/* Moved for loop into constructor after intiating cOption */
//  for(int i = 0; i < Labels.length; i++)
	   //			 cOption.add(Labels[i]);

// double amount = Double.parseDouble(amountTf.getText());

/*  Go to if statements below
double term = Double.parseDouble(termTf.getText());
double interest = Double.parseDouble(intrateTf.getText());
double interestRateMonths = interest/12;


/* Next two lines commented out to accomadate formula in else if 
  double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
	paymentsTf.setText("" + decimalPlaces.format(payments)); */
	 
//  double amortize = amount - payments;
//  amortizeTf.setText("" + decimalPlaces.format(amortize)); 



/*  This part of the code is for selecting the loan amount and plugging them into the formula. */


//exit application when user presses the exit button
//		Object source = e.getSource();
//		if (source == exitButton)
//		{
//			System.exit(0);
//		}
//calculate payment amount when user presses submit

//  else if (source == calcBtn)
//		{

{
   // String Lables = cOption.getSelectedItem();// this is wrong replace with String Labels = (Sting) cOption.getSelectedItem
	//
	
	String Labels = (String) cOption.getSelectedItem(); // gotta typecast here!!!
  /*  
	   for(int i = 0; i < Labels.length; i++) 
		 
		 addItem(Labels[i]); */
		 
//		  cOption.add(Labels[i]);

   
   if (Labels.equals("7 years at 5.35%")) 
	

// else if (Lables == "7 years at 5.35%") // this is wrong, but I'll get to that
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;
							
			
			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 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 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


}}}



Was This Post Helpful? 0
  • +
  • -

#18 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: else if statement

Posted 21 March 2009 - 12:08 PM

We are far away from your original request with "if else"
don't think anybody will read all the replies (and 10 times your code posted) until they reach your GUI issue
better to open a new Topic: "Problem Combo in GUI does not react"
and post your ceanest code... you can probably just cut & paste your last post
Was This Post Helpful? 0
  • +
  • -

#19 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: else if statement

Posted 21 March 2009 - 12:19 PM

You've never used the term and termMonths variables, which are what your calculation and while relies on.

Try seeing where those fit in.
Was This Post Helpful? 0
  • +
  • -

#20 grabowpd   User is offline

  • D.I.C Head

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

Re: else if statement

Posted 21 March 2009 - 12:47 PM

View PostLocke, on 21 Mar, 2009 - 11:19 AM, said:

You've never used the term and termMonths variables, which are what your calculation and while relies on.

Try seeing where those fit in.



I thought they belonged after each if statement where you declare the double for the variable.

I went ahead and created a new thread since it is a new problem like PBL suggested and cleaned up the code.

Thanks,
Phil
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2