?
Solved

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

Posted on 2011-05-07
6
Medium Priority
?
275 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:marchbaby
  • 4
  • 2
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35712447
What do you want to happen onCraeteButton - you just add strings to ArrayList
but you will not see anyhting when you do it
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35712486
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
 

Author Comment

by:marchbaby
ID: 35712565
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
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!

 
LVL 47

Expert Comment

by:for_yan
ID: 35713202


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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35713216
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
 

Author Closing Comment

by:marchbaby
ID: 35713628
I agree, I am going to change this around.
0

Featured Post

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!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 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