2 Replies - 593 Views - Last Post: 09 April 2011 - 01:18 PM Rate Topic: -----

#1 genlastudio1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 11

I have problem with my if stetement

Posted 09 April 2011 - 01:01 PM

I have problem with my if statemt I need the input for the menu or the text field so the user can pick the way the want to put in their payment.
package javax.awt.event;
/*
 * Programmer:	Glen Lacock 
 */

//Import necessary java libraries
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.text.*;

//Class to create the graphical user interface
@SuppressWarnings("serial")
public class Glenhomwork33 extends JFrame implements ActionListener
{
	//Declare variables
	double dAmount;
	double dInterest[] = { 5.35, 5.5, 5.75 };  
	int iTerm[] = { 7, 15, 30 };
	double dPayment;
	double dBalance;
	double dTotalInterest;
	int iArrayCounter = 0;
	int iPaymentCounter = 1;
	double firstchose1;
	double scondchose2;
    double thridchose3;
    double fourchose;


	//Declare and define numeric format for dollar amounts
	DecimalFormat twoDigits = new DecimalFormat("$###,##0.00");

	//Declare and create input panel
	Panel inputPanel = new Panel();

		//Declare, create, and define labels
		JLabel amountLabel = new JLabel("Amount:");
		JLabel loanLabel = new JLabel("Loan type:");
		JLabel paymentLabel = new JLabel("Payment:");
		JLabel TermLabel = new JLabel("Years of the loan ");
		
		//Declare, create, and define text fields
		JTextField amountField = new JTextField(10);
		JTextField ToalInterest = new JTextField(10);
		JTextField TermText = new JTextField(10);
		JTextField paymentField = new JTextField(10);
        
		//Declare, create, and define option buttons
		JRadioButton firstchose = new JRadioButton("5.35%, 7 years");
		JRadioButton scondchose = new JRadioButton("5.50%, 15 years");
		JRadioButton thridchose = new JRadioButton("5.75%, 30 years");

	//Declare and create button panel
	Panel buttonPanel = new Panel();	
		//Declare Calculate button
		JButton calculateButton = new JButton("Calculate");
		//Declare exit button
		JButton exitbutton = new JButton("exit");
	
		//Declare and create amortization table panel
		Panel tablePanel = new Panel();
        JLabel tableLabel = new JLabel("life of the loan");
	    TextArea tableArea = new TextArea();
	
	//Class to create and add all panels and controls
	public Glenhomwork33()
	{
		this.getContentPane().setLayout(new BorderLayout());
			inputPanel.setLayout(new GridLayout(6,2));
			tablePanel.setLayout(new BorderLayout());
			buttonPanel.setLayout(new FlowLayout());

		//Add controls to input panel
		inputPanel.add(amountLabel);
		inputPanel.add(amountField);
		inputPanel.add(loanLabel);
		inputPanel.add(ToalInterest);
		inputPanel.add(TermLabel);
		inputPanel.add(TermText);
		inputPanel.add(paymentLabel);
		inputPanel.add(paymentField);
		
	    //Disable user editing of the payment field
		paymentField.setEditable(false);
		
		// add button for chose 
	     inputPanel.add(firstchose);
	     inputPanel.add(scondchose);
	     inputPanel.add(thridchose); 
	     
		//Create option group and add option buttons
	ButtonGroup Ops = new ButtonGroup();
		Ops.add(firstchose);
		Ops.add(scondchose);
		Ops.add(thridchose);
		
		//Add controls to amortization table panel
		tablePanel.add(tableLabel, BorderLayout.NORTH);
		tablePanel.add(tableArea, BorderLayout.CENTER);
		tableArea.setEditable(false);
		
		//Add button to button panel and add action listener to the button
		buttonPanel.add(calculateButton);
		calculateButton.addActionListener(this);
		
		//Add panels to frame
		getContentPane().add(inputPanel, BorderLayout.NORTH);
		getContentPane().add(tablePanel, BorderLayout.CENTER);
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		
		//Set action listeners and command values for option buttons
		firstchose.addActionListener(this);
		scondchose.addActionListener(this);
		thridchose.addActionListener(this);
		firstchose.setActionCommand("firstchose1");
		scondchose.setActionCommand("scondchose2");
		thridchose.setActionCommand("thridchose3");
	}

	
	public void actionPerformed(ActionEvent e)
	{
		String arg = e.getActionCommand();
		
		double textoneinterest; 
		double texttworate; 
		
		if (arg == "textoneinterest")
		{
		textoneinterest = Double.parseDouble(ToalInterest.getText());	
		}
		if (arg == "texttworate")
		{
			texttworate = Double.parseDouble(TermText.getText());	
		}
		else 
		if (arg == "firstchose1")
		{
			iArrayCounter = 0;
		}

		if (arg == "scondchose2")
		{
			iArrayCounter = 1;
		}

		if (arg == "thridchose3")
		{
			iArrayCounter = 2;
		
}	//put the in the text box of what people chooise for the buttons
		ToalInterest.setText(dInterest[]+"");
		TermText.setText(iTerm[]+""); 
		if (arg == "Calculate")
		{
			//Try-catch statement to prevent program crashes due to invalid entries
			try
			{
				//Retrieve values from the text fields and convert them to doubles/integers
				dAmount = Double.parseDouble(amountField.getText());

				//Call monthly payment calculation class
				dPayment = PaymentCalc(dAmount, dInterest[0 + iArrayCounter], iTerm[0 + iArrayCounter]);

				//Format and display monthly payment
				paymentField.setText(String.valueOf(twoDigits.format(dPayment)));

				//Reset fields and variables
				tableArea.setText(""); //Clears amortization field table
				dBalance = dAmount; //Reset balance to original principal
				dTotalInterest = 0; //Reset total interest
				iPaymentCounter = 1; //Rest payment counter

				//Amortization loop
				do
				{
					  dTotalInterest = dTotalInterest + dBalance * (dInterest[0 + iArrayCounter] / 1200); //Calculate total interest
					  dBalance = dBalance - (dPayment - dBalance * (dInterest[0 + iArrayCounter] / 1200)); //Calculate remaining balance

					  //Print results
					  tableArea.append(iPaymentCounter + "\t" + String.valueOf(twoDigits.format(dTotalInterest)) + " \t" + String.valueOf(twoDigits.format(dBalance)) + "\n");

					  iPaymentCounter++;
				}
				while (dBalance >= 1); //End amortization loop once balance reaches $1
			}

			//Display error if a non-numeric character is input
			catch (NumberFormatException a)
			{
				paymentField.setText("Invalid entry."); //Error message
			}
		}
	}

	
//Retrieve values from the text fields and convert them to doubles/integers
 dAmount = Double.parseDouble(amountField.getText());
//Format and display monthly payment
 paymentField.setText(String.valueOf(twoDigits.format(PaymentCalc())));
	}
	//Class to calculate the monthly payment
	public double PaymentCalc()
	{
		dRate = dInterest / 1200; //Actual interest rate

		//Calculate and return monthly payment
		dPayment = (dRate + dRate / (Math.pow(1 + dRate, (iTerm * 12)) -1)) * dAmount;
		return dPayment;
	}

	//Main class
	public static void main(String[] args)
	{
		//Create new frame 200px from top and left, 260px wide, 415px tall
		Glenhomwork33 f = new Glenhomwork33();
		f.setBounds(200,200,260,415);
		f.setTitle("Glens Mortgage Calculator");
		f.setVisible(true);

		//Verify program termination on close
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}	

*Edited: code tags fixed

This post has been edited by pbl: 09 April 2011 - 01:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I have problem with my if stetement

#2 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: I have problem with my if stetement

Posted 09 April 2011 - 01:05 PM

PUT CODE IN THE "code" TAGS AND WE WILL THEN LOOK AT IT!!!

This post has been edited by softwareEngineer(): 09 April 2011 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon 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: I have problem with my if stetement

Posted 09 April 2011 - 01:18 PM

In the actionPerformed() you just have to check for the two buttons: the calculate and the exit

If it is calculate:
- you will have to check which RadioButton is selected
- access the JTextField for calculation in double

In the actionPerformed() you just have to check for the two buttons: the calculate and the exit

If it is calculate:
- you will have to check which RadioButton is selected
- access the JTextField for calculation in double
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1