Link to home
Start Free TrialLog in
Avatar of marchbaby
marchbaby

asked on

Java string arrayList accessing through program

Hi,

I have created the arraylist mortgage that needs to be added to with the create button, display the added info with the next button. I have constructed the array list but I can't get the buttons to access the array. The errors I am getting are "Cannot find sysmbol; variable mortgage" in the next button and create button area. Do I need to move my arraylist? I've tried and it doesn't reconize it then either.

Thank you in advance!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package may1st;

/**
 *
 * @author Heather
 */
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.lang.reflect.InvocationTargetException;
import javax.swing.ImageIcon;
import java.lang.String;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Main implements Runnable {


    //The main container for this application
    public JFrame jfrMain;
    public JTextField fnameTextField;
    public JTextField lnameTextField;
    public JTextField phoneTextField;
    public JTextField loanAmountField;
    public JTextField termYearsField;
    public JTextField interestRateField;
    public JTextField monthlyPaymentField;
    public JButton calcButton;
    public JButton nextButton;
    public JButton exitButton;
    public JButton createButton;

    /**
     *
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Main());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * Setup the GUI and start the program.
     */
    public void run() {
        jfrMain = new JFrame("Mortgage Calculator");
        jfrMain.setLayout(new GridLayout(8, 2));
        jfrMain.setSize(new Dimension(900, 500));

        //Construct the fields

        fnameTextField = new JTextField (20);
        lnameTextField = new JTextField(20);
        phoneTextField = new JTextField(20);
        loanAmountField = new JTextField(10);
        termYearsField = new JTextField(4);
        interestRateField = new JTextField(2);
        monthlyPaymentField = new JTextField(10);
        monthlyPaymentField.setEditable(false);

  //array construct
        ArrayList mortgage = new ArrayList();
        mortgage.add(fnameTextField);
        mortgage.add(lnameTextField);
        mortgage.add(phoneTextField);
        mortgage.add(loanAmountField);
        mortgage.add(termYearsField);
        mortgage.add(interestRateField);
        mortgage.add(monthlyPaymentField);





        //adding the labels
        jfrMain.add(new JLabel(new ImageIcon("C:/Users/Heather/Documents/UoP/Java_IT215/Week 7/may1st/src/may1st/home.jpg"), JLabel.LEFT));
        jfrMain.add(new JLabel("Heather's Mortage Calculator"));
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel("First Name"));
        jfrMain.add(fnameTextField);
       
        jfrMain.add(new JLabel("Last Name"));
        jfrMain.add(lnameTextField);
     
        jfrMain.add(new JLabel("Phone"));
        jfrMain.add(phoneTextField);
        
        jfrMain.add(new JLabel("Loan Amount"));
        jfrMain.add(loanAmountField);
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel(" Loan Terms in months"));
        jfrMain.add(termYearsField);
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel("Interest Rate"));
        jfrMain.add(interestRateField);
        
        jfrMain.add(new JLabel("Monthly payment"));
        jfrMain.add(monthlyPaymentField);
        jfrMain.add(new JLabel(""));
  
    

       

        //create button
        createButton = new JButton("Create");
        createButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    oncreateButtonClick(e);
                }
            });
        jfrMain.add(createButton);
        jfrMain.setVisible(true);

        //calculate button
        calcButton = new JButton("Calculate");
        calcButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCalcButtonClick(e);
                }
            });

        jfrMain.add(calcButton);
        jfrMain.setVisible(true);

        // next button 
        nextButton = new JButton("Next");
        nextButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onnextButtonClick(e);
                }
            });
        jfrMain.add(nextButton);
        jfrMain.setVisible(true);

        //exit button 
        exitButton = new JButton ("Exit");
        exitButton.addActionListener (new ActionListener() {
        public void actionPerformed(ActionEvent e)
           {
             System.exit(0);
              }
              });
         jfrMain.add(exitButton);
         jfrMain.setVisible(true);
    }

    public void oncreateButtonClick(ActionEvent e){

        mortgage.add(fnameTextField);
        mortgage.add(lnameTextField);
        mortgage.add(phoneTextField);
        mortgage.add(loanAmountField);
        mortgage.add(termYearsField);
        mortgage.add(interestRateField);
        mortgage.add(monthlyPaymentField);
    

    }

    public void onnextButtonClick (ActionEvent e){

        nextButton.setActionCommand ("next");

        fnameTextField.setText(mortgage.getfnameTextField);
	lnameTextField.setText(mortgage.getlnameTextField());
        phoneTextField.setText(mortgage.getphoneTextField());
        loanAmountField.setText(mortgage.getloanAmountField());
        termYearsField.setText(mortgage.termYearsField());
        interestRateField.setText(mortgage.getinterestRate());
        monthlyPaymentField.setText(mortgage.getmonthlyPaymentField());
    }

    public void onCalcButtonClick(ActionEvent e) {


        double loanAmount;
        double termYears;
        double interestRate;

        try {
            loanAmount = Double.parseDouble(loanAmountField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter loan amount");

            return;
        }

        try {
            termYears = Double.parseDouble(termYearsField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain,
                "Please enter mortgage term length");

            return;
        }

        try {
            interestRate = Double.parseDouble(interestRateField.getText()) / 100;
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter interest rate");

            return;
        }

        //Calculate monthly payment
        double payment = calculatePayment(loanAmount, termYears, interestRate);
        monthlyPaymentField.setText(new Double(payment).toString());
    }

    public double calculatePayment(double loanAmount, double termYears,
        double interestRate) {
        double monthlyInterest = interestRate / 12;
        double monthlyPayment = (double) (loanAmount * (monthlyInterest / (1 -
            Math.pow(1 + monthlyInterest, termYears * -1))));
        monthlyPayment = Math.ceil(monthlyPayment * 100) / 100;

        return monthlyPayment;
    }
}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

Try to make your arraylist called mortgage instance variablke, declare it just wiuthin your class and then only initialize and fill in inside method run().
Otherwise, what I think happens yoiu create it as inside method and then try to access it in another method
This is what I see from your snippet; if you don't know how to change I'll work more with you when I reach my compouter
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marchbaby
marchbaby

ASKER

I agree that I'm doing this in a really weird way. I'm trying to rework it now.