Java Mortgage Calc buttons to create&save and the use of next

What am I doing wrong? I cannot get the create button to add the info and save, and I cannot get my next button to display anything.

I feel like I've read so much code trying to work around this that it's all starting to look funny!

Thanks!!
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;
    ArrayList mortgage;
    String fname;
    String lname;
    String phone;
    double loanAmount;
    double termYears;
    double interestRate;


    /**
     *
     * @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();
        }
        
     
    }
        public String fname(){
            return fname;
        }
        
        public String lname(){
            return lname;
        }
        
        public String phone(){
            return phone;
        }



//Gui
    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
        mortgage = new ArrayList();
        mortgage.add(fname);
        mortgage.add(lname);
        mortgage.add(phone);
       mortgage.add(loanAmountField);
        mortgage.add(termYearsField);
       mortgage.add(interestRateField);
       mortgage.add(monthlyPaymentField);

//getters
  



        //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);
        fnameTextField.getText();
        fname = fnameTextField.getText();
        jfrMain.add(new JLabel("Last Name"));
        jfrMain.add(lnameTextField);
         lnameTextField.getText();
         lname = lnameTextField.getText();
        jfrMain.add(new JLabel("Phone"));
        jfrMain.add(phoneTextField);
        phoneTextField.getText();
        phone = phoneTextField.getText();
        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){

                
        fnameTextField.getText();
        fname  = fnameTextField.getText();
        mortgage.add(fname);
        
        lnameTextField.getText();
        lname = lnameTextField.getText();
        mortgage.add(lname);

        phoneTextField.getText();
        phone = phoneTextField.getText();
        mortgage.add(phone);

        return;

    }

    public void onnextButtonClick (ActionEvent e){

fnameTextField.setText(fname);
lnameTextField.setText(lname);
phoneTextField.setText(phone);


    }

    public void onCalcButtonClick(ActionEvent e) {


        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

marchbabyAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
In general  decisin to lump them all together in one arraylist is not a good one.

You should rather come up with the class

class MortgageCase {
String fname;
String lname;
String phone;
....

MortgageCase (String fname, String lname, ...){
this.fname = fname;
this.lname = lname;

}


}


And then after you read these values from fields
you should be constructying instance of MortgageCase
and adding this instance to your ArrayList



Then you'll not need to incremenet currentIndex by seven but rather by one
and everyrhing should be more normal and easeir to progrm

You'll of course need to add some getter methods to the class,
i.e. public String getFirstName() { return fnname; }, etc,
0
 
for_yanCommented:
What do you want to happen onCraeteButton - you just add strings to ArrayList
but you will not see anyhting when you do it
0
 
for_yanCommented:
Why do you have so many boxes?

In your method onCreateButton you just read
textfields and then add the values to ArrayList - nothing gets displayed

When you click Next is sets on TextFiled some values which are presumably the same as it reads from the same boxes - so nothing  will be sen
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
marchbabyAuthor Commented:
The boxes are to keep the placement looking ok of the text labels and fields. I know there is a better way, but that's just not that important.
How can I fix the next button to go to the next set of data in the arrays?
0
 
for_yanCommented:


You do somthing like that:
 public void onnextButtonClick (ActionEvent e){

                    currentIndex += 7;
        if((currentIndex+7) > mortgage.size()-1) return;
  
              fname = (String) mortgage.get(currentIndex);
               lname = (String) mortgage.get(currentIndex+1);
               phone = (String) mortgage.get(currentIndex+2);
fnameTextField.setText(fname);
lnameTextField.setText(lname);
phoneTextField.setText(phone);


    }

Open in new window


You should define currentIndex as your integer instance variable
0
 
marchbabyAuthor Commented:
I agree, I am going to change this around.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.