[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

relating programs/calls

Posted on 2006-05-30
8
Medium Priority
?
272 Views
Last Modified: 2010-03-31
I am trying to relate 3 programs i have completed instead of creating an entire new program. The reason is the user is supposed to select whether they want to create a custom calculated mortgage or if they would like to select from 3 preset options. I have added the additional listeners to the gui program and calls to to the other 2 programs as well. I compile and  get through the first 2 fine and the compiler gets stuck on the last one (program 3 below). I have it coded similar to the 2nd program but i get error msg that my void section is illegal. I have listed them below. i have added comments in the 3rd program below to show where it is getting errrors. Can someone else look to see what i have wrong? thx

1st program:
import javax.swing.*;
import java.awt.*;

public class ThursdayMortgageWeek4GUI extends JFrame {

ThursdaySupportWeek4 mortgageSupport=new ThursdaySupportWeek4(this);
MortgageGui4 mortgageSupport2=new MortgageGui4(this);


//sets up rows for gui

JPanel row1 = new JPanel();
JComboBox typeMtg =new JComboBox();
JLabel mtgType= new JLabel("Select Mortgage Type");

//public JButton customButton = new JButton("Custom");
//public JButton presetButton = new JButton("Preset Term/APR");


JPanel row10 = new JPanel();
JLabel amntLabel = new JLabel("Mortgage Amount: $", JLabel.LEFT);
public JTextField loanField = new JTextField(10);


JPanel row20 =new JPanel();
JComboBox options = new JComboBox();
JLabel optionsLabel = new JLabel();
JLabel lbltype= new JLabel("Term and APR for Mortgage",JLabel.LEFT);
JTextField years                  = new JTextField(2);

JPanel row30 = new JPanel();
public JButton calButton = new JButton("Calculate");
public JButton resetButton = new JButton("Reset");
public JButton endButton = new JButton("End");

JPanel row40 = new JPanel();
JLabel paymtLabel = new JLabel("Monthly Payment Amount:");
public JTextField paymtField = new JTextField();

JPanel row50 = new JPanel();
JLabel lblPaymentNum= new JLabel("Payment Number"+ "\t",JLabel.LEFT);
JLabel lblInterest= new JLabel("Interest"+ "\t",JLabel.LEFT);
JLabel lblPrinciple= new JLabel("Principal"+ "\t",JLabel.LEFT);
JLabel lblBalance= new JLabel("Balance"+ "\t",JLabel.LEFT);

JPanel row60 = new JPanel();
JTextArea textField = new JTextArea(8, 40);


public ThursdayMortgageWeek4GUI() {

super("Week 3 - Mortgage GUI");
setSize(650,500);
setLocation(250,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font f = new Font ("Arial", Font.PLAIN, 30);
Font b = new Font ("Arial", Font.BOLD, 16);
FlowLayout layout = new FlowLayout();
Container pane = getContentPane();
pane.setLayout(layout);


mtgType.addItemListener(mortgageSupport2);
loanField.addActionListener(mortgageSupport);
options.addItemListener(mortgageSupport);
calButton.addActionListener(mortgageSupport);
resetButton.addActionListener(mortgageSupport);
endButton.addActionListener(mortgageSupport);

FlowLayout flow1 = new FlowLayout();
row1.add(mtgType);
row1.add(typeMtg);
typeMtg.addItem("Custom");
typeMtg.addItem("Preset Term/APR");
pane.add(row1);

FlowLayout flow10 = new FlowLayout();
row10.setLayout(flow10);
row10.add(amntLabel);
row10.add(loanField);
pane.add(row10);


GridLayout flow20 = new GridLayout(1,4,5,1);
row20.setLayout(flow20);
row20.add(optionsLabel);
row20.add(options);
row20.add(lbltype);
options.setEnabled(true);
options.addItem("Select Option");
options.addItem("7 year at 5.35%");
options.addItem("15 year at 5.5%");
options.addItem("30 year at 5.75%");
pane.add(row20);


GridLayout flow30 = new GridLayout (1,4,75,1);
row30.add(calButton);
row30.add(resetButton);
row30.add(endButton);
pane.add(row30);

FlowLayout flow40 = new FlowLayout();
row40.setLayout(flow40);
row40.add(paymtLabel);
row40.add(paymtField);
pane.add(row40);

GridLayout flow50= new GridLayout(1,4,5,1);
row50.setLayout(flow50);
row50.add(lblPaymentNum);
row50.add(lblInterest);
row50.add(lblPrinciple);
row50.add(lblBalance);
pane.add(row50);      

FlowLayout flow60 = new FlowLayout();
textField.setLineWrap(true);
row60.setLayout(flow60);
JScrollPane textpane= new JScrollPane(textField,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
row60.add(textField);
pane.add(row60);


}

public static void main(String[] args) {
      ThursdayMortgageWeek4GUI guiFrame= new ThursdayMortgageWeek4GUI();
      guiFrame.setVisible(true);
      }
}
      
_________________________________________________________________________
2nd program:
import java.awt.event.*;
import java.text.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;


public class ThursdaySupportWeek4 implements      ItemListener,ActionListener
{

ThursdayMortgageWeek4GUI gui;
double amount = 0;
double term      = 0;
double intrst = 0;
double moIn      = 0;
double moTrm =      0;
double prin      = 0;
double payment      = 0;
double termArray[] =      { 7, 15,      30      };
double intrstArray[]      = { 5.35, 5.50, 5.75      };

/*custome variables
int val = 0;
String cusAmount;
String rate;
String cusTerm;
int response = 0;
boolean valid = true;
double termValue;
double amountValue;
double rateValue;
*/


int array;
int c = 1;
int index = 1;

//to format      payment amounts
DecimalFormat df = new DecimalFormat("$#,###.00");

public ThursdaySupportWeek4(ThursdayMortgageWeek4GUI      in)
{
gui=in;
}
public static void main(String []  args)
{

}

public void      itemStateChanged(ItemEvent      arg0)      {

      Object button = gui.options.getSelectedItem();
      String buttonPress =      button.toString();

      Object source = arg0.getSource();

   System.out.println(" == > item = " + buttonPress );
      
      if      (buttonPress == gui.calButton.getActionCommand()) {
              array =1;
      }
      if      (buttonPress == gui.resetButton.getActionCommand()) {
             array = 2;
             
      }
      if      (buttonPress == gui.endButton.getActionCommand()) {
             array = 3;

            
      }
}

public void      actionPerformed(ActionEvent event)
{
      String buttonClicked      = event.getActionCommand();
      
System.out.println(" == > action = " + buttonClicked );      
      if      (buttonClicked      ==      "Reset") {
         setNull();
   }
   if      (buttonClicked      ==      "End"){

            clearResults();
            System.exit(0);
      }
   if      (buttonClicked      =="Calculate"){
      
        
    clearResults();
    startCalculations();

    }
}

      
void startCalculations() {


 try {
               amount =      Double.parseDouble(gui.loanField.getText());
 } catch (NumberFormatException e) {
                  clearResults();
                  gui.textField.append("Invalid Entry");
                  gui.loanField.setText(null);
 }

if      (amount < 0) {
JOptionPane.showMessageDialog(null,      "Please Enter Valid Amount",
"Message      Dialog",      JOptionPane.PLAIN_MESSAGE);
gui.loanField.setText(null);
}


int index =      gui.options.getSelectedIndex();
term = termArray[index];
intrst =      intrstArray[index];

if      (amount > 0) {
amount =      Double.parseDouble(gui.loanField.getText());
moIn = (intrst      / 12)      / 100;
moTrm      = term *      12;
payment = amount * (moIn /      (1      - java.lang.Math.pow((1      + moIn),      (-moTrm))));

gui.paymtField.setText("" +      df.format(payment));

}

 double newPrin      = amount;      
 
 for (int      i = 0; i      <= moTrm ;      i++) {
 
 double newIn =      moIn * newPrin;
 double reduction = payment      - newIn;
 newPrin = newPrin      - reduction;

        gui.textField.append("");
              gui.textField.append(" "      + + i);
        gui.textField.append(" "      + "\t" + df.format(newIn));
        gui.textField.append(" "      + "\t" +df.format(reduction));
        gui.textField.append(" "      + "\t" + df.format(newPrin) + "\n");                            
 }
        
}

void reset() {

gui.loanField.setText(null);
gui.paymtField.setText(null);
gui.textField.setText(null);

}

void end() {
System.exit(0);
}

      /*
       * Sets GUI & Declarations to Null
       */
      void setNull()  
      {
            gui.textField.setText (null);                  //Sets value to null
            gui.options.setSelectedItem("Select Option");      //Sets value to "Select Option"
            gui.paymtField.setText(null);               //Sets value to null
            index = 1;                                                      //Sets Loop-Counter to 0                          
      }
      /*
       * Sets GUI & Declarations to Null
       */
      void clearResults() //Sets GUI & Declarations to null
      {
            gui.textField.setText(null);      //Sets value to null
      }
      
      void setComboBoxNull()
      {
            gui.options.setEnabled(false);      //Disables Drop Down Menu
                                                                        
                                                                              
      }      

}

__________________________________________________________________
3rd-
import javax.swing.*;
import java.text.*;
import java.math.*;
import java.lang.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;



public class MortgageGui4  implements      ItemListener //,ActionListener
{
      
  ThursdayMortgageWeek4GUI gui;
 //declaring variables
 int val = 0;
 String amount;
 String rate;
 String term;
 int response = 0;
 boolean valid = true;
 double termValue;
 double amountValue;
 double rateValue;
 
 public MortgageGui4 (ThursdayMortgageWeek4GUI      in)
{
gui=in;
}
 
 
 public static void main(String[] args) throws Exception
 {
  }

public void      actionPerformed(ActionEvent event)
{
      String buttonClicked      = event.getActionCommand();
      
      System.out.println(" == > action = " + buttonClicked );
      if      (buttonClicked      =="Custom"){
      
        
    //clearResults();
    startCalculations2();
      }
      
void startCalculations2() {     // ERROR SECTION illegal start of expression      
  do{    
     // The showInoutDialog is a method of JOptionPane and its return value is a string.
        // Since the return value is a string, it is necessary that the string be converted
        // to double so input can be used for mortgage calculation below.
                        
        amount = JOptionPane.showInputDialog(null,"Please enter amount to be financed:", " ", JOptionPane.QUESTION_MESSAGE);
         
                  try {
                  // parseDouble is that converts a string into an double value
                  amountValue = Double.parseDouble(amount);            
                                                                                                             
                         if (amountValue < 0.0){
                        JOptionPane.showMessageDialog(null, "Amount cannot be less than 0. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);       
                         continue;
                                    }
                                    else if (amountValue > 2000000.00) {
                                       JOptionPane.showMessageDialog(null, "Amount cannot exceed $10,000,000.Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;                  
                                    }
                                    }
                              
                        //error handling to not accept character/letter input from user
                        
                        catch(NumberFormatException e) {

                         // Dialog box to show error with the amount and prompt user to start over
                         JOptionPane.showMessageDialog(null, "Error, invalid entry. Amount must be numeric. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                         continue;
                              }      
                              
                         rate = JOptionPane.showInputDialog(null,"Please enter annual finance rate:", " ", JOptionPane.QUESTION_MESSAGE);
                                    
                        try {
                        // parseDouble is that converts a string into an double value
                        rateValue = Double.parseDouble(rate);
      
                              if (rateValue <0){
                              JOptionPane.showMessageDialog(null, "Annual Finance rate cannot be less than 0. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);       
                           continue;
                                    }
                                    else if (rateValue > 14.0) {
                                       JOptionPane.showMessageDialog(null, "Annual Finance Rate cannot exceed 14 percent. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                                    }
                                    }
                        //error handling to not accept character/letter input from user
                          catch(NumberFormatException e){
 
                          // Dialog box to show error with the amount and prompt user to start over
                          JOptionPane.showMessageDialog(null, "Error, invalid entry. Finance rate must be numeric. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                          continue;
                              }
                              
                        term = JOptionPane.showInputDialog(null,"Please enter months/term of loan:", " ", JOptionPane.QUESTION_MESSAGE);
                  

                        try {
                        // parseDouble is that converts a string into an double value
                        termValue = Double.parseDouble(term);      
                              if (termValue <0){
                              JOptionPane.showMessageDialog(null, "Months/Term cannot be less than 0. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);       
                              continue;
                                    }
                                    else if (termValue > 480) {
                                       JOptionPane.showMessageDialog(null, "Months/Term cannot exceed 480 months. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                        }
                        }
                        //error handling to not accept character/letter input from user
                        catch(NumberFormatException e) {

                            // Dialog box to show error with the amount and prompt user to start over
                               JOptionPane.showMessageDialog(null, "Error, invalid entry. Months/Term must be numeric. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                               continue;
                        
                        }
                  

                  
                  
                        
                        //calculation of mortgage
                        //rateValue -interest is entered by user above
                        //amountValue- loan amount is entered by user above
                        //termValue - term of loan is entered by user above
                        //J = rateValue/12; //*Monthy interest to be used in calculation
                        double newRate=rateValue/100;
                        double J=newRate/12;
                        
                        // to display amount in correct format
                        DecimalFormat df = new DecimalFormat("$#,###.00");                        
                        
                        double mortgage = (Math.pow((1 + J), termValue) - 1)
                        / (J * Math.pow((1 + J), termValue));
                        double payment = amountValue / mortgage; //*Rate Calculation

                  //create new variable for the calculated mortgage payment
                  //String endAmount = "Your monthly mortgage payment is " + df.format(payment);
                  gui.paymtField.setText("" +      df.format(payment));

            // Dialog box dialog box that will show user the payment amount
            JOptionPane.showMessageDialog(null,endAmount,"Information",JOptionPane.INFORMATION_MESSAGE);
            
            // Dialog box dialog box that prompts user to perform another calculation
            response = JOptionPane.showConfirmDialog(null,"Would you like to perform another calculation?",
            "Try Again?",JOptionPane.YES_NO_OPTION);
            }
            while (response == 0);
      }

0
Comment
Question by:pgmtkl
  • 4
  • 4
8 Comments
 
LVL 2

Expert Comment

by:appxpete
ID: 16792301
Looks like you are missing a } in the method just above your startCalculation2() method.  Try adding one more } to that method to close it.
0
 

Author Comment

by:pgmtkl
ID: 16792696
thanks. do i need to change the JOption message boxes in program 3 to Jpanel and text fields as in the 2nd program?
0
 
LVL 2

Expert Comment

by:appxpete
ID: 16792826
Hmm...  That's a completely different question.  Give it a try.  If it works the way you want it the way it is written now then I guess you can leave it.  If not rewrite it to use JPanel.  You may want to consider changing it anyway just so that you are using the same classes and approach for both.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pgmtkl
ID: 16792917
Thanks. I did change it and all three programs are compiling.. The calculation for the 3rd program is off, but that must be a different error. i have listed the code below. it compiles but doesnt work for custom choice.
Does it mean i have something missing in the 2nd program??

pgm 1
import javax.swing.*;
import java.awt.*;

public class ThursdayMortgageWeek4GUI extends JFrame {

ThursdaySupportWeek4 mortgageSupport=new ThursdaySupportWeek4(this);
MortgageGui4 mortgageSupport2=new MortgageGui4(this);


//sets up rows for gui

JPanel row1 = new JPanel();
public JComboBox typeMtg =new JComboBox();
JLabel mtgType= new JLabel("Select Mortgage Type");

JPanel row2=new JPanel();
JLabel cusAmt = new JLabel("Custom Mortgage Amount: $", JLabel.LEFT);
public JTextField cusField = new JTextField(10);
JLabel cusTerm = new JLabel("Term/months", JLabel.LEFT);
public JTextField cusTermField = new JTextField(10);
JLabel cusApr = new JLabel("APR", JLabel.LEFT);
public JTextField cusAprField = new JTextField(10);


JPanel row10 = new JPanel();
JLabel amntLabel = new JLabel("Mortgage Amount for Preset Term/APR: $", JLabel.LEFT);
public JTextField loanField = new JTextField(10);


JPanel row20 =new JPanel();
JComboBox options = new JComboBox();
JLabel optionsLabel = new JLabel();
JLabel lbltype= new JLabel("Term and APR for Mortgage",JLabel.LEFT);
JTextField years                  = new JTextField(2);

JPanel row30 = new JPanel();
public JButton calButton = new JButton("Calculate");
public JButton resetButton = new JButton("Reset");
public JButton endButton = new JButton("End");

JPanel row40 = new JPanel();
JLabel paymtLabel = new JLabel("Monthly Payment Amount:");
public JTextField paymtField = new JTextField();

JPanel row50 = new JPanel();
JLabel lblPaymentNum= new JLabel("Payment Number"+ "\t",JLabel.LEFT);
JLabel lblInterest= new JLabel("Interest"+ "\t",JLabel.LEFT);
JLabel lblPrinciple= new JLabel("Principal"+ "\t",JLabel.LEFT);
JLabel lblBalance= new JLabel("Balance"+ "\t",JLabel.LEFT);

JPanel row60 = new JPanel();
JTextArea textField = new JTextArea(8, 40);


public ThursdayMortgageWeek4GUI() {

super("Week 3 - Mortgage GUI");
setSize(650,500);
setLocation(250,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font f = new Font ("Arial", Font.PLAIN, 30);
Font b = new Font ("Arial", Font.BOLD, 16);
FlowLayout layout = new FlowLayout();
Container pane = getContentPane();
pane.setLayout(layout);


typeMtg.addActionListener(mortgageSupport2);
//typeMtg.addItemListener(mortgageSupport2);
loanField.addActionListener(mortgageSupport);
options.addItemListener(mortgageSupport);
calButton.addActionListener(mortgageSupport);
resetButton.addActionListener(mortgageSupport);
endButton.addActionListener(mortgageSupport);

FlowLayout flow1 = new FlowLayout();
row1.add(mtgType);
row1.add(typeMtg);
typeMtg.addItem("Custom");
typeMtg.addItem("Preset Term/APR");
pane.add(row1);

FlowLayout flow2 = new FlowLayout();
row2.add(cusAmt);
row2.add(cusField);
row2.add(cusTerm);
row2.add(cusTermField);
row2.add(cusApr);
row2.add(cusAprField);
pane.add(row2);


FlowLayout flow10 = new FlowLayout();
row10.setLayout(flow10);
row10.add(amntLabel);
row10.add(loanField);
pane.add(row10);


GridLayout flow20 = new GridLayout(1,4,5,1);
row20.setLayout(flow20);
row20.add(optionsLabel);
row20.add(options);
row20.add(lbltype);
options.setEnabled(true);
options.addItem("Select Option");
options.addItem("7 year at 5.35%");
options.addItem("15 year at 5.5%");
options.addItem("30 year at 5.75%");
pane.add(row20);


GridLayout flow30 = new GridLayout (1,4,75,1);
row30.add(calButton);
row30.add(resetButton);
row30.add(endButton);
pane.add(row30);

FlowLayout flow40 = new FlowLayout();
row40.setLayout(flow40);
row40.add(paymtLabel);
row40.add(paymtField);
pane.add(row40);

GridLayout flow50= new GridLayout(1,4,5,1);
row50.setLayout(flow50);
row50.add(lblPaymentNum);
row50.add(lblInterest);
row50.add(lblPrinciple);
row50.add(lblBalance);
pane.add(row50);      

FlowLayout flow60 = new FlowLayout();
textField.setLineWrap(true);
row60.setLayout(flow60);
JScrollPane textpane= new JScrollPane(textField,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
row60.add(textField);
pane.add(row60);


}

public static void main(String[] args) {
      ThursdayMortgageWeek4GUI guiFrame= new ThursdayMortgageWeek4GUI();
      guiFrame.setVisible(true);
      }
}
      
_______________________________________________________________________
pgm 2
import java.awt.event.*;
import java.text.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;


public class ThursdaySupportWeek4 implements      ItemListener,ActionListener
{

ThursdayMortgageWeek4GUI gui;
double amount = 0;
double term      = 0;
double intrst = 0;
double moIn      = 0;
double moTrm =      0;
double prin      = 0;
double payment      = 0;
double termArray[] =      { 7, 15,      30      };
double intrstArray[]      = { 5.35, 5.50, 5.75      };

/*custome variables
int val = 0;
String cusAmount;
String rate;
String cusTerm;
int response = 0;
boolean valid = true;
double termValue;
double amountValue;
double rateValue;
*/


int array;
int c = 1;
int index = 1;

//to format      payment amounts
DecimalFormat df = new DecimalFormat("$#,###.00");

public ThursdaySupportWeek4(ThursdayMortgageWeek4GUI      in)
{
gui=in;
}
public static void main(String []  args)
{

}

public void      itemStateChanged(ItemEvent      arg0)      {

      Object button = gui.options.getSelectedItem();
      String buttonPress =      button.toString();

      Object source = arg0.getSource();

   System.out.println(" == > item = " + buttonPress );
      
      if      (buttonPress == gui.calButton.getActionCommand()) {
              array =1;
      }
      if      (buttonPress == gui.resetButton.getActionCommand()) {
             array = 2;
             
      }
      if      (buttonPress == gui.endButton.getActionCommand()) {
             array = 3;

            
      }
}

public void      actionPerformed(ActionEvent event)
{
      String buttonClicked      = event.getActionCommand();
      
System.out.println(" == > action = " + buttonClicked );      
      if      (buttonClicked      ==      "Reset") {
         setNull();
   }
   if      (buttonClicked      ==      "End"){

            clearResults();
            System.exit(0);
      }
   if      (buttonClicked      =="Calculate"){
      
        
    clearResults();
    startCalculations();

    }
}

      
void startCalculations() {


 try {
               amount =      Double.parseDouble(gui.loanField.getText());
 } catch (NumberFormatException e) {
                  clearResults();
                  gui.textField.append("Invalid Entry");
                  gui.loanField.setText(null);
 }

if      (amount < 0) {
JOptionPane.showMessageDialog(null,      "Please Enter Valid Amount",
"Message      Dialog",      JOptionPane.PLAIN_MESSAGE);
gui.loanField.setText(null);
}


int index =      gui.options.getSelectedIndex();
term = termArray[index];
intrst =      intrstArray[index];

if      (amount > 0) {
amount =      Double.parseDouble(gui.loanField.getText());
moIn = (intrst      / 12)      / 100;
moTrm      = term *      12;
payment = amount * (moIn /      (1      - java.lang.Math.pow((1      + moIn),      (-moTrm))));

gui.paymtField.setText("" +      df.format(payment));

}

 double newPrin      = amount;      
 
 for (int      i = 0; i      <= moTrm ;      i++) {
 
 double newIn =      moIn * newPrin;
 double reduction = payment      - newIn;
 newPrin = newPrin      - reduction;

        gui.textField.append("");
              gui.textField.append(" "      + + i);
        gui.textField.append(" "      + "\t" + df.format(newIn));
        gui.textField.append(" "      + "\t" +df.format(reduction));
        gui.textField.append(" "      + "\t" + df.format(newPrin) + "\n");                            
 }
        
}

void reset() {

gui.loanField.setText(null);
gui.paymtField.setText(null);
gui.textField.setText(null);

}

void end() {
System.exit(0);
}

      /*
       * Sets GUI & Declarations to Null
       */
      void setNull()  
      {
            gui.textField.setText (null);                  //Sets value to null
            gui.options.setSelectedItem("Select Option");      //Sets value to "Select Option"
            gui.paymtField.setText(null);               //Sets value to null
            index = 1;                                                      //Sets Loop-Counter to 0                          
      }
      /*
       * Sets GUI & Declarations to Null
       */
      void clearResults() //Sets GUI & Declarations to null
      {
            gui.textField.setText(null);      //Sets value to null
      }
      
      void setComboBoxNull()
      {
            gui.options.setEnabled(false);      //Disables Drop Down Menu
                                                                        
                                                                              
      }      

}

_________________________________________________________________________
program 3
import javax.swing.*;
import java.text.*;
import java.math.*;
import java.lang.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;



public class MortgageGui4  implements      ActionListener //ItemListener ,
{
      
  ThursdayMortgageWeek4GUI gui;
 //declaring variables
 int val = 0;
 double amount;
 double rate;
 double term;
 //String amount;
 //String rate;
 //String term;
 int response = 0;
 boolean valid = true;
 double termValue;
 double amountValue;
 double rateValue;
 
 public MortgageGui4 (ThursdayMortgageWeek4GUI      in)
{
gui=in;
}
 
 
 public static void main(String[] args) throws Exception
 {
  }

/*public void      itemStateChanged(ItemEvent      arg0)      {

      Object button = gui.typeMtg.getSelectedItem();
      String buttonPress =      button.toString();

      Object source = arg0.getSource();

   System.out.println(" == > item = " + buttonPress );
      
      if      (buttonPress == gui.typeMtg.getActionCommand()) {
              response =0;
      }

}*/

public void      actionPerformed(ActionEvent event)
{
      String buttonClicked      = event.getActionCommand();
      
      System.out.println(" == > action = " + buttonClicked );
      if      (buttonClicked      =="Custom"){
      
        
    //clearResults();
    startCalculations2();
      }
}
      
void startCalculations2() {     // ERROR SECTION illegal start of expression      
  do{    
     // The showInoutDialog is a method of JOptionPane and its return value is a string.
        // Since the return value is a string, it is necessary that the string be converted
        // to double so input can be used for mortgage calculation below.
                        
//        amount = JOptionPane.showInputDialog(null,"Please enter amount to be financed:"," ", JOptionPane.QUESTION_MESSAGE);
        amount= Double.parseDouble(gui.cusField.getText());
                   
                  try {
                  // parseDouble is that converts a string into an double value
                  amountValue = (amount);            
                                                                                                             
                         if (amountValue < 0.0){
                        JOptionPane.showMessageDialog(null, "Amount cannot be less than 0. Please start over.",
                         "Error", JOptionPane.ERROR_MESSAGE);       
                        
                         continue;
                                    }
                                    else if (amountValue > 2000000.00) {
                                       JOptionPane.showMessageDialog(null, "Amount cannot exceed $10,000,000. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;                  
                                    }
                                    }
                              
                        //error handling to not accept character/letter input from user
                        
                        catch(NumberFormatException e) {

                         // Dialog box to show error with the amount and prompt user to start over
                         JOptionPane.showMessageDialog(null, "Error, invalid entry. Amount must be numeric. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                         continue;
                              }      
                              
                        // rate = JOptionPane.showInputDialog(null,"Please enter annual finance rate:",
                        // " ", JOptionPane.QUESTION_MESSAGE);
                                    
                              rate=      Double.parseDouble(gui.cusTermField.getText());
                                    
                        try {
                        // parseDouble is that converts a string into an double value
                        //rateValue = Double.parseDouble(rate);
                        rateValue = (rate);
      
                              if (rateValue <0){
                              JOptionPane.showMessageDialog(null, "Annual Finance rate cannot be less than 0. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);       
                           continue;
                                    }
                                    else if (rateValue > 14.0) {
                                       JOptionPane.showMessageDialog(null, "Annual Finance Rate cannot exceed 14 percent.Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                                    }
                                    }
                        //error handling to not accept character/letter input from user
                          catch(NumberFormatException e){
 
                          // Dialog box to show error with the amount and prompt user to start over
                          JOptionPane.showMessageDialog(null, "Error, invalid entry.Finance rate must be numeric. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                          continue;
                              }
                              
                        //term = JOptionPane.showInputDialog(null,"Please enter months/term of loan:", " ", JOptionPane.QUESTION_MESSAGE);
                      term=Double.parseDouble(gui.cusAprField.getText());
                              
                        try {
                        // parseDouble is that converts a string into an double value
                        //termValue = Double.parseDouble(term);      
                        termValue = (term);      
                              if (termValue <0){
                              JOptionPane.showMessageDialog(null, "Months/Term cannot be less than 0. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);       
                              continue;
                                    }
                                    else if (termValue > 480) {
                                       JOptionPane.showMessageDialog(null, "Months/Term cannot exceed 480 months. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                        }
                        }
                        //error handling to not accept character/letter input from user
                        catch(NumberFormatException e) {

                            // Dialog box to show error with the amount and prompt user to start over
                               JOptionPane.showMessageDialog(null, "Error, invalid entry. Months/Term must be numeric. Please start over.", "Error", JOptionPane.ERROR_MESSAGE);
                               continue;
                        
                        }
                  
                        //calculation of mortgage
                        //rateValue -interest is entered by user above
                        //amountValue- loan amount is entered by user above
                        //termValue - term of loan is entered by user above
                        //J = rateValue/12; //*Monthy interest to be used in calculation
                        double newRate=rateValue/100;
                        double J=newRate/12;
                        
                        // to display amount in correct format
                        DecimalFormat df = new DecimalFormat("$#,###.00");                        
                        
                        double mortgage = (Math.pow((1 + J), termValue) - 1)
                        / (J * Math.pow((1 + J), termValue));
                        double payment = amountValue / mortgage; //*Rate Calculation

                  //create new variable for the calculated mortgage payment
                  //String endAmount = "Your monthly mortgage payment is " + df.format(payment);
                  gui.paymtField.setText("" +      df.format(payment));

            // Dialog box dialog box that will show user the payment amount
            //JOptionPane.showMessageDialog(null,endAmount,"Information",JOptionPane.INFORMATION_MESSAGE);
            
            // Dialog box dialog box that prompts user to perform another calculation
            response = JOptionPane.showConfirmDialog(null,"Would you like to perform another calculation?",
            "Try Again?",JOptionPane.YES_NO_OPTION);
            }
            while (response == 0);
      }
}


      
      

0
 
LVL 2

Assisted Solution

by:appxpete
appxpete earned 2000 total points
ID: 16793271
You've got quite a number of problems there.

In program 3, the actionListener is never going to return an ActionCommand of "Custom".  That is the Selected Item value, not the action command.

Clicking the "Calculate" button fires the actionPerformed method in program 2.  So, in the actionPerformed for program2 you would need to test for the Selected Item value of the typeMtg combo box and test that for a value of "Custom" then run the startCalculations2() routine over in program3.

Or rework the whole thing so it is all one program, or combine programs 2 and 3 together as a single action listener.

0
 

Author Comment

by:pgmtkl
ID: 16793517
so i would need to add the custom into program 2 as a item selected?
0
 
LVL 2

Accepted Solution

by:
appxpete earned 2000 total points
ID: 16793793
OK,

This seems to work for me and do something useful.

-------------------------- program 1 ---------------------------------------------------

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

public class ThursdayMortgageWeek4GUI extends JFrame
{

      ThursdaySupportWeek4 mortgageSupport = new ThursdaySupportWeek4(this);

      // sets up rows for gui

      JPanel row1 = new JPanel();

      public JComboBox typeMtg = new JComboBox();

      JLabel mtgType = new JLabel("Select Mortgage Type");

      JPanel row2 = new JPanel();

      JLabel cusAmt = new JLabel("Custom Mortgage Amount: $", JLabel.LEFT);

      public JTextField cusField = new JTextField(10);

      JLabel cusTerm = new JLabel("Term/months", JLabel.LEFT);

      public JTextField cusTermField = new JTextField(10);

      JLabel cusApr = new JLabel("APR", JLabel.LEFT);

      public JTextField cusAprField = new JTextField(10);

      JPanel row10 = new JPanel();

      JLabel amntLabel = new JLabel("Mortgage Amount for Preset Term/APR: $",
                  JLabel.LEFT);

      public JTextField loanField = new JTextField(10);

      JPanel row20 = new JPanel();

      JComboBox options = new JComboBox();

      JLabel optionsLabel = new JLabel();

      JLabel lbltype = new JLabel("Term and APR for Mortgage", JLabel.LEFT);

      JTextField years = new JTextField(2);

      JPanel row30 = new JPanel();

      public JButton calButton = new JButton("Calculate");

      public JButton resetButton = new JButton("Reset");

      public JButton endButton = new JButton("End");

      JPanel row40 = new JPanel();

      JLabel paymtLabel = new JLabel("Monthly Payment Amount:");

      public JTextField paymtField = new JTextField(10);

      JPanel row50 = new JPanel();

      JLabel lblPaymentNum = new JLabel("Payment Number" + "\t", JLabel.LEFT);

      JLabel lblInterest = new JLabel("Interest" + "\t", JLabel.LEFT);

      JLabel lblPrinciple = new JLabel("Principal" + "\t", JLabel.LEFT);

      JLabel lblBalance = new JLabel("Balance" + "\t", JLabel.LEFT);

      JPanel row60 = new JPanel();

      JTextArea textField = new JTextArea(8, 40);

      public ThursdayMortgageWeek4GUI()
      {

            super("Week 3 - Mortgage GUI");
            setSize(650, 500);
            setLocation(250, 60);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Font f = new Font("Arial", Font.PLAIN, 30);
            Font b = new Font("Arial", Font.BOLD, 16);
            FlowLayout layout = new FlowLayout();
            Container pane = getContentPane();
            pane.setLayout(layout);

            calButton.addActionListener(mortgageSupport);
            resetButton.addActionListener(mortgageSupport);
            endButton.addActionListener(mortgageSupport);

            FlowLayout flow1 = new FlowLayout();
            row1.add(mtgType);
            row1.add(typeMtg);
            typeMtg.addItem("Custom");
            typeMtg.addItem("Preset Term/APR");
            pane.add(row1);

            FlowLayout flow2 = new FlowLayout();
            row2.add(cusAmt);
            row2.add(cusField);
            row2.add(cusTerm);
            row2.add(cusTermField);
            row2.add(cusApr);
            row2.add(cusAprField);
            pane.add(row2);

            FlowLayout flow10 = new FlowLayout();
            row10.setLayout(flow10);
            row10.add(amntLabel);
            row10.add(loanField);
            pane.add(row10);

            GridLayout flow20 = new GridLayout(1, 4, 5, 1);
            row20.setLayout(flow20);
            row20.add(optionsLabel);
            row20.add(options);
            row20.add(lbltype);
            options.setEnabled(true);
            options.addItem("Select Option");
            options.addItem("7 year at 5.35%");
            options.addItem("15 year at 5.5%");
            options.addItem("30 year at 5.75%");
            pane.add(row20);

            GridLayout flow30 = new GridLayout(1, 4, 75, 1);
            row30.add(calButton);
            row30.add(resetButton);
            row30.add(endButton);
            pane.add(row30);

            FlowLayout flow40 = new FlowLayout();
            row40.setLayout(flow40);
            row40.add(paymtLabel);
            row40.add(paymtField);
            pane.add(row40);

            GridLayout flow50 = new GridLayout(1, 4, 5, 1);
            row50.setLayout(flow50);
            row50.add(lblPaymentNum);
            row50.add(lblInterest);
            row50.add(lblPrinciple);
            row50.add(lblBalance);
            pane.add(row50);

            FlowLayout flow60 = new FlowLayout();
            textField.setLineWrap(true);
            row60.setLayout(flow60);
            JScrollPane textpane = new JScrollPane(textField,
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            row60.add(textField);
            pane.add(row60);

      }

      public String getCalcType()
      {
            if (typeMtg == null)
                  return "";
            return (String) "" + typeMtg.getSelectedItem();
      }

      public static void main(String[] args)
      {
            ThursdayMortgageWeek4GUI guiFrame = new ThursdayMortgageWeek4GUI();
            guiFrame.setVisible(true);
      }
}

-------------------------------------------program 2 with startCalculations2() rolled in -------------------------------------------

import java.awt.event.*;
import java.text.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class ThursdaySupportWeek4 implements ItemListener, ActionListener
{

      ThursdayMortgageWeek4GUI gui;

      double amount = 0;

      double term = 0;

      double intrst = 0;

      double moIn = 0;

      double moTrm = 0;

      double prin = 0;

      double payment = 0;

      double termArray[] =
      { 7, 15, 30 };

      double intrstArray[] =
      { 5.35, 5.50, 5.75 };

      /*
       * custome variables int val = 0; String cusAmount; String rate; String
       * cusTerm; int response = 0; boolean valid = true; double termValue; double
       * amountValue; double rateValue;
       */

      int array;

      int c = 1;

      int index = 1;

      // to format payment amounts
      DecimalFormat df = new DecimalFormat("$#,###.00");

      public ThursdaySupportWeek4(ThursdayMortgageWeek4GUI in)
      {
            gui = in;
      }

      public static void main(String[] args)
      {

      }

      public void itemStateChanged(ItemEvent arg0)
      {

            Object button = gui.options.getSelectedItem();
            String buttonPress = button.toString();

            Object source = arg0.getSource();

            System.out.println(" == > item = " + buttonPress);

            if (buttonPress == gui.calButton.getActionCommand())
            {
                  array = 1;
            }
            if (buttonPress == gui.resetButton.getActionCommand())
            {
                  array = 2;

            }
            if (buttonPress == gui.endButton.getActionCommand())
            {
                  array = 3;

            }
      }

      public void actionPerformed(ActionEvent event)
      {
            String buttonClicked = event.getActionCommand();

            System.out.println(" == > action = " + buttonClicked);
            if (buttonClicked == "Reset")
            {
                  setNull();
            }
            if (buttonClicked == "End")
            {

                  clearResults();
                  System.exit(0);
            }
            if (buttonClicked == "Calculate")
            {

                  clearResults();
                  if( ((String)(gui.typeMtg.getSelectedItem())).equals("Custom") )
                        startCalculations2();
                  else
                        startCalculations();

            }
      }

      void startCalculations()
      {

            try
            {
                  amount = Double.parseDouble(gui.loanField.getText());
            } catch (NumberFormatException e)
            {
                  clearResults();
                  gui.textField.append("Invalid Entry");
                  gui.loanField.setText(null);
            }

            if (amount < 0)
            {
                  JOptionPane.showMessageDialog(null, "Please Enter Valid Amount",
                              "Message     Dialog", JOptionPane.PLAIN_MESSAGE);
                  gui.loanField.setText(null);
            }

            int index = gui.options.getSelectedIndex();
            term = termArray[index];
            intrst = intrstArray[index];

            if (amount > 0)
            {
                  amount = Double.parseDouble(gui.loanField.getText());
                  moIn = (intrst / 12) / 100;
                  moTrm = term * 12;
                  payment = amount
                              * (moIn / (1 - java.lang.Math.pow((1 + moIn), (-moTrm))));

                  gui.paymtField.setText("" + df.format(payment));

            }

            double newPrin = amount;

            for (int i = 0; i <= moTrm; i++)
            {

                  double newIn = moIn * newPrin;
                  double reduction = payment - newIn;
                  newPrin = newPrin - reduction;

                  gui.textField.append("");
                  gui.textField.append(" " + +i);
                  gui.textField.append(" " + "\t" + df.format(newIn));
                  gui.textField.append(" " + "\t" + df.format(reduction));
                  gui.textField.append(" " + "\t" + df.format(newPrin) + "\n");
            }

      }

      void reset()
      {

            gui.loanField.setText(null);
            gui.paymtField.setText(null);
            gui.textField.setText(null);

      }

      void end()
      {
            System.exit(0);
      }

      /*
       * Sets GUI & Declarations to Null
       */
      void setNull()
      {
            gui.textField.setText(null); // Sets value to null
            gui.options.setSelectedItem("Select Option"); // Sets value to "Select
                                                                                    // Option"
            gui.paymtField.setText(null); // Sets value to null
            index = 1; // Sets Loop-Counter to 0
      }

      /*
       * Sets GUI & Declarations to Null
       */
      void clearResults() // Sets GUI & Declarations to null
      {
            gui.textField.setText(null); // Sets value to null
      }

      void setComboBoxNull()
      {
            gui.options.setEnabled(false); // Disables Drop Down Menu

      }

      int response = 0;

      double rate;

      double termValue;

      double amountValue;

      double rateValue;

      void startCalculations2()
      {
                  // The showInoutDialog is a method of JOptionPane and its return
                  // value is a string.
                  // Since the return value is a string, it is necessary that the
                  // string be converted
                  // to double so input can be used for mortgage calculation below.

                  amount = Double.parseDouble(gui.cusField.getText());

                  try
                  {
                        // parseDouble is that converts a string into an double value
                        amountValue = (amount);

                        if (amountValue < 0.0)
                        {
                              JOptionPane.showMessageDialog(null,
                                          "Amount cannot be less than 0. Please start over.",
                                          "Error", JOptionPane.ERROR_MESSAGE);

                              return;
                        } else if (amountValue > 2000000.00)
                        {
                              JOptionPane
                                          .showMessageDialog(
                                                      null,
                                                      "Amount cannot exceed $10,000,000. Please start over.",
                                                      "Error", JOptionPane.ERROR_MESSAGE);
                              return;
                        }
                  }

                  // error handling to not accept character/letter input from user

                  catch (NumberFormatException e)
                  {

                        // Dialog box to show error with the amount and prompt user to
                        // start over
                        JOptionPane
                                    .showMessageDialog(
                                                null,
                                                "Error, invalid entry. Amount must be numeric. Please start over.",
                                                "Error", JOptionPane.ERROR_MESSAGE);
                        return;
                  }

                  rate = Double.parseDouble(gui.cusTermField.getText());

                  try
                  {
                        // parseDouble is that converts a string into an double value
                        // rateValue = Double.parseDouble(rate);
                        rateValue = (rate);

                        if (rateValue < 0)
                        {
                              JOptionPane
                                          .showMessageDialog(
                                                      null,
                                                      "Annual Finance rate cannot be less than 0. Please start over.",
                                                      "Error", JOptionPane.ERROR_MESSAGE);
                              return;
                        } else if (rateValue > 14.0)
                        {
                              JOptionPane
                                          .showMessageDialog(
                                                      null,
                                                      "Annual Finance Rate cannot exceed 14 percent.Please start over.",
                                                      "Error", JOptionPane.ERROR_MESSAGE);
                              return;
                        }
                  }
                  // error handling to not accept character/letter input from user
                  catch (NumberFormatException e)
                  {

                        // Dialog box to show error with the amount and prompt user to
                        // start over
                        JOptionPane
                                    .showMessageDialog(
                                                null,
                                                "Error, invalid entry.Finance rate must be numeric. Please start over.",
                                                "Error", JOptionPane.ERROR_MESSAGE);
                        return;
                  }

                  // term = JOptionPane.showInputDialog(null,"Please enter months/term
                  // of loan:", " ", JOptionPane.QUESTION_MESSAGE);
                  term = Double.parseDouble(gui.cusAprField.getText());

                  try
                  {
                        // parseDouble is that converts a string into an double value
                        // termValue = Double.parseDouble(term);
                        termValue = (term);
                        if (termValue < 0)
                        {
                              JOptionPane
                                          .showMessageDialog(
                                                      null,
                                                      "Months/Term cannot be less than 0. Please start over.",
                                                      "Error", JOptionPane.ERROR_MESSAGE);
                              return;
                        } else if (termValue > 480)
                        {
                              JOptionPane
                                          .showMessageDialog(
                                                      null,
                                                      "Months/Term cannot exceed 480 months. Please start over.",
                                                      "Error", JOptionPane.ERROR_MESSAGE);
                              return;
                        }
                  }
                  // error handling to not accept character/letter input from user
                  catch (NumberFormatException e)
                  {

                        // Dialog box to show error with the amount and prompt user to
                        // start over
                        JOptionPane
                                    .showMessageDialog(
                                                null,
                                                "Error, invalid entry. Months/Term must be numeric. Please start over.",
                                                "Error", JOptionPane.ERROR_MESSAGE);
                        return;

                  }

                  // calculation of mortgage
                  // rateValue -interest is entered by user above
                  // amountValue- loan amount is entered by user above
                  // termValue - term of loan is entered by user above
                  // J = rateValue/12; //*Monthy interest to be used in calculation
                  double newRate = rateValue / 100;
                  double J = newRate / 12;

                  // to display amount in correct format
                  DecimalFormat df = new DecimalFormat("$#,###.00");

                  double mortgage = (Math.pow((1 + J), termValue) - 1)
                              / (J * Math.pow((1 + J), termValue));
                  double payment = amountValue / mortgage; // *Rate Calculation

                  // create new variable for the calculated mortgage payment
                  // String endAmount = "Your monthly mortgage payment is " +
                  // df.format(payment);
                  gui.paymtField.setText("" + df.format(payment));

      }
}

------------------------ program3 no longer needed, removed  --------------------------
0
 

Author Comment

by:pgmtkl
ID: 16794749
thanks for all the help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question