Java GUI Mortgage Caluclator

Can't get my menu options to calculate the interest
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.lang.Math;
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Date;
 
//declaring content
public class SteveMortCalcWk4 extends JFrame implements ActionListener {
 
 
//adding initial selection options via RadioButtons
    JPanel row_a1 = new JPanel();
    JRadioButton customOpt = new JRadioButton("Custom Options", true);
    JRadioButton fixedOpt = new JRadioButton("Fixed Options", false);
 
//content Buttons, Panels, Labels, and TextFields
    JPanel row_a = new JPanel();
    JLabel amntLabel = new JLabel();
    JTextField loanField = new JTextField(11);
    JLabel loan2Label = new JLabel();
    JPanel row_b = new JPanel();
    JPanel row_b1 = new JPanel();
    JLabel termLabel = new JLabel(); //term
    JTextField termField = new JTextField(4);
    JPanel row_b2 = new JPanel();
    JLabel entryLabel = new JLabel(); //interest
    JTextField entryField = new JTextField(3);
    JComboBox options = new JComboBox();
    JLabel optionsLabel = new JLabel();
    JPanel row_c = new JPanel();
    JButton calButton = new JButton();
    JButton resetButton = new JButton();
    JLabel nameLabel = new JLabel("Steve's Mortgage Calculator Week 3");
    JTextField paymtField = new JTextField(8);
    JPanel row_e = new JPanel();
    JPanel row_f = new JPanel();
    JTextArea opField = new JTextArea(20, 55);
    JScrollPane scrollPane = new JScrollPane(opField,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    ButtonGroup radioSelect = new ButtonGroup();
 
 
 
//setting format of GUI layout
    public void mortCalcForm() {
 
//main GUI Dimensions
// super(" Loan Calculator with Preselected or Customized Values");
        setSize(800, 450);
        new GridLayout(6, 1, 10, 10);
        FlowLayout flowCenter = new FlowLayout(FlowLayout.CENTER);
        FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
 
        Container pane = getContentPane();
        pane.setLayout(flow);
 
 
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
 
//setting actionListeners for Fields and Buttons
        loanField.addActionListener(this);
        termField.addActionListener(this);
        entryField.addActionListener(this);
        options.addActionListener(this);
        calButton.addActionListener(this);
        resetButton.addActionListener(this);
       // endButton.addActionListener(this);
        customOpt.addActionListener(this);
        fixedOpt.addActionListener(this);
 
 
 
//layout for mortgage amount entry
        row_a.setLayout(flow);
        amntLabel.setText("Enter Loan Amount:");
        row_a.add(amntLabel);
        row_a.add(loanField);
        pane.add(row_a);
 
        row_a1.setLayout(flow);
        radioSelect.add(customOpt);
        radioSelect.add(fixedOpt);
        row_a1.add(customOpt);
        row_a1.add(fixedOpt);
        pane.add(row_a1);
 
 
//layout for options
        options.addItem("7 years at 5.35%");
        options.addItem("15 years at 5.5%");
        options.addItem("30 years at 5.75%");
        options.setEnabled(false);
 
        row_b.setLayout(flow);
        row_b.add(options);
        pane.add(row_b);
 
        row_b1.setLayout(flow);
        termLabel.setText("Term in Years");
        row_b1.add(termLabel);
        row_b1.add(termField);
        pane.add(row_b1);
 
        row_b2.setLayout(flow);
        entryLabel.setText("Interest %");
        row_b2.add(entryLabel);
        row_b2.add(entryField);
        pane.add(row_b2);
 
//layout for buttons to calculate, Reset, and End
        row_c.setLayout(flow);
        calButton.setText("Calculate");
        calButton.setBackground(Color.blue);
        row_c.add(calButton);
        resetButton.setText("Reset");
        resetButton.setBackground(Color.green);
        row_c.add(resetButton);
        row_c.add(nameLabel);
        pane.add(row_c);
        row_f.add(scrollPane);
        pane.add(row_f);
    }
 
public void resetFields(){
 
    loanField.setText("");
    termField.setText("");
    entryField.setText("");
}
//adding actionEvents for Buttons
    public void actionPerformed(ActionEvent event) {
 
        Object source = event.getSource();
 
        if (source == calButton) {
            startCalculations();
        }
        if (source == resetButton) {
            resetFields();
        }
//        if (source == endButton) {
//        }
        if (source == customOpt) {
            options.setEnabled(false);
            termField.setEnabled(true);
            termField.setEditable(true);
            entryField.setEnabled(true);
            entryField.setEditable(true);
        }
 
        if (source == fixedOpt) {
            options.setEnabled(true);
            termField.setEnabled(false);
            termField.setEditable(false);
            entryField.setEnabled(false);
            entryField.setEditable(false);
        }
    }
static String format (double dollars)
 {
      String numString = Double.toString(dollars);
      int dotpos = numString.indexOf('.');
 
      if (dotpos < 0)
           return numString;
 
 
      else if (dotpos < numString.length() - 2)
           return numString.substring(0, dotpos + 3);
 
      else return numString + "0";
 }
//formulas for monthly payment
    void startCalculations() {
 
        Thread thisThread = Thread.currentThread();
        NumberFormat currency = NumberFormat.getCurrencyInstance();
 
        double amt = Double.parseDouble(loanField.getText()); //amount of loan
        double trm = Double.parseDouble(termField.getText()); //variable for term of loan in years
        double intrst = Double.parseDouble(entryField.getText()); //variable for interest amount of loan
        double moIn = 0; //variable for monthly payment for formula
        double prin = 0; // variable for principle
        double paymt = 0; //variable for payment
        double newPrin = amt;
        boolean Exception = false;
 
        amt = Double.parseDouble(loanField.getText());
        if (amt <= 0){
        JOptionPane.showMessageDialog(null, "Loan field needs to be greater than zero",
                    "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                    return;
        }
 
      if (amt <= 0)
           opField.setText("Principal amount missing");
      else if (intrst <= 0)
           opField.setText("Interest rate missing");
      else if (trm <= 0)
           opField.setText("Number of years missing");
      else
      {
 
           double balance = new Double(amt).doubleValue();
           double intyr   = new Double(intrst).doubleValue() / 100;
           short nyears   = (short) trm; //new Integer (trm).intValue();
 
           StringBuffer msg = new StringBuffer();
            msg.append("\nprincipal=" + balance + "  interest=" + intyr
                     + "  years=" + nyears + "\n");
 
            double beginningMortgage = intyr / 12.;
            int npmts = nyears * 12;
            double pmt = balance * (beginningMortgage / (1.- Math.pow(1.+ beginningMortgage,-npmts)));
 
            msg.append("payment \ttotal\tinterest\tprincipal\tbalance\n");
            msg.append("number \tpayment\tpayment\tpayment\n");
            msg.append("\t\t\t\t" + balance + "\n");
 
 
 
          for (short mo = 1; mo <= npmts; ++mo)
          {
            double principalpayment, initialpayment = balance * beginningMortgage;
            if (mo < npmts)
                principalpayment = pmt - initialpayment;
                else principalpayment = balance;
                balance -= principalpayment;
                msg.append(mo + "\t" + format(initialpayment + principalpayment)
 
                    + "\t" + format(initialpayment)
                    + "\t" + format(principalpayment)
                    + "\t" + format(balance) + "\n");
        }
 
            opField.setText(msg.toString());
        }
    }
 
    public static void main(String[] args) {
        SteveMortCalcWk4 myMort = new SteveMortCalcWk4();
        myMort.mortCalcForm();
    }
}

Open in new window

imagekrazyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
you need to set the term and interest rate when an option is selected from fixed combo
you can do that in your action listener, the following shows an example of how to set it the term field. You would also need to do the same for interest rate


    if (source==options) {
          switch (options.getSelectedIndex()) {
          case 0:
                termField.setText("7");
                break;
                
          case 1:
                termField.setText("15");
                break;

          case 2:
                termField.setText("30");
                break;
}

0
imagekrazyAuthor Commented:
Is this right , I think I am confused

public void resetFields(){

    loanField.setText("");
    termField.setText("");
    entryField.setText("");
}
//adding actionEvents for Buttons
    public void actionPerformed(ActionEvent event) {

        Object source = event.getSource();

    if (source==options) {
          switch (options.getSelectedIndex()) {
          case 0:
                termField.setText("7");
                break;

          case 1:
                termField.setText("15");
                break;

          case 2:
                termField.setText("30");
                break;
}


  if (source==options) {
          switch (options.getSelectedIndex()) {
          case 0:
                entryField.setText("5.35");
                break;

          case 1:
                entryField.setText("5.5");
                break;

          case 2:
                entryField.setText("5.75");
                break;
}
0
Mick BarryJava DeveloperCommented:
yes that would work
you could have also put them both in the one switch

    if (source==options) {
          switch (options.getSelectedIndex()) {
          case 0:
                termField.setText("7");
                entryField.setText("5.35");
                break;
...
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

imagekrazyAuthor Commented:
Hello ,again, I did that ,now i got 12 errors, here is my code;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.lang.Math;
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Date;

//declaring content
public class SteveMortCalcWk4 extends JFrame implements ActionListener {


//adding initial selection options via RadioButtons
    JPanel row_a1 = new JPanel();
    JRadioButton customOpt = new JRadioButton("Custom Options", true);
    JRadioButton fixedOpt = new JRadioButton("Fixed Options", false);

//content Buttons, Panels, Labels, and TextFields
    JPanel row_a = new JPanel();
    JLabel amntLabel = new JLabel();
    JTextField loanField = new JTextField(11);
    JLabel loan2Label = new JLabel();
    JPanel row_b = new JPanel();
    JPanel row_b1 = new JPanel();
    JLabel termLabel = new JLabel(); //term
    JTextField termField = new JTextField(4);
    JPanel row_b2 = new JPanel();
    JLabel entryLabel = new JLabel(); //interest
    JTextField entryField = new JTextField(3);
    JComboBox options = new JComboBox();
    JLabel optionsLabel = new JLabel();
    JPanel row_c = new JPanel();
    JButton calButton = new JButton();
    JButton resetButton = new JButton();
    JLabel nameLabel = new JLabel("Steve's Mortgage Calculator Week 3");
    JTextField paymtField = new JTextField(8);
    JPanel row_e = new JPanel();
    JPanel row_f = new JPanel();
    JTextArea opField = new JTextArea(20, 55);
    JScrollPane scrollPane = new JScrollPane(opField,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    ButtonGroup radioSelect = new ButtonGroup();



//setting format of GUI layout
    public void mortCalcForm() {

//main GUI Dimensions
// super(" Loan Calculator with Preselected or Customized Values");
        setSize(800, 450);
        new GridLayout(6, 1, 10, 10);
        FlowLayout flowCenter = new FlowLayout(FlowLayout.CENTER);
        FlowLayout flow = new FlowLayout(FlowLayout.LEFT);

        Container pane = getContentPane();
        pane.setLayout(flow);


        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

//setting actionListeners for Fields and Buttons

        loanField.addActionListener(this);
        termField.addActionListener(this);
        entryField.addActionListener(this);
        options.addActionListener(this);
        calButton.addActionListener(this);
        resetButton.addActionListener(this);
       // endButton.addActionListener(this);
        customOpt.addActionListener(this);
        fixedOpt.addActionListener(this);



//layout for mortgage amount entry
        row_a.setLayout(flow);
        amntLabel.setText("Enter Loan Amount:");
        row_a.add(amntLabel);
        row_a.add(loanField);
        pane.add(row_a);

        row_a1.setLayout(flow);
        radioSelect.add(customOpt);
        radioSelect.add(fixedOpt);
        row_a1.add(customOpt);
        row_a1.add(fixedOpt);
        pane.add(row_a1);


//layout for options
        options.addItem("7 years at 5.35%");
        options.addItem("15 years at 5.5%");
        options.addItem("30 years at 5.75%");
        options.setEnabled(false);

        row_b.setLayout(flow);
        row_b.add(options);
        pane.add(row_b);

        row_b1.setLayout(flow);
        termLabel.setText("Term in Years");
        row_b1.add(termLabel);
        row_b1.add(termField);
        pane.add(row_b1);

        row_b2.setLayout(flow);
        entryLabel.setText("Interest %");
        row_b2.add(entryLabel);
        row_b2.add(entryField);
        pane.add(row_b2);

//layout for buttons to calculate, Reset, and End
        row_c.setLayout(flow);
        calButton.setText("Calculate");
        calButton.setBackground(Color.blue);
        row_c.add(calButton);
        resetButton.setText("Reset");
        resetButton.setBackground(Color.green);
        row_c.add(resetButton);
        row_c.add(nameLabel);
        pane.add(row_c);
        row_f.add(scrollPane);
        pane.add(row_f);
    }

public void resetFields(){

    loanField.setText("");
    termField.setText("");
    entryField.setText("");
}
//adding actionEvents for Buttons
    public void actionPerformed(ActionEvent event) {

        Object source = event.getSource();

if (source==options) {
          switch (options.getSelectedIndex()) {
          case 0:
                termField.setText("7");
                entryField.setText("5.35");
                break;

          case 1:
                 termField.setText("15");
                 entryField.setText("5.5");
                 break;

          case 2:
                 termField.setText("30");
                         entryField.setText("5.75");
                 break;
}

          if (source == calButton) {
            startCalculations();
        }
        if (source == resetButton) {
            resetFields();
        }

        if (source == customOpt) {
            options.setEnabled(false);
            termField.setEnabled(true);
            termField.setEditable(true);
            entryField.setEnabled(true);
            entryField.setEditable(true);
            startCalculations();
        }

        if (source == fixedOpt) {
           options.setEnabled(true);
            termField.setEnabled(false);
            termField.setEditable(false);
            entryField.setEnabled(false);
            entryField.setEditable(false);
            startCalculations();
        }
    }
static String format (double dollars)
 {
      String numString = Double.toString(dollars);
      int dotpos = numString.indexOf('.');

      if (dotpos < 0)
           return numString;


      else if (dotpos < numString.length() - 2)
           return numString.substring(0, dotpos + 3);

      else return numString + "0";


 }
//formulas for monthly payment
    void startCalculations() {

        Thread thisThread = Thread.currentThread();
        NumberFormat currency = NumberFormat.getCurrencyInstance();

        double amt = Double.parseDouble(loanField.getText()); //amount of loan
        double trm = Double.parseDouble(termField.getText()); //variable for term of loan in years
        double intrst = Double.parseDouble(entryField.getText()); //variable for interest amount of loan
        double moIn = 0; //variable for monthly payment for formula
        double prin = 0; // variable for principle
        double paymt = 0; //variable for payment
        double newPrin = amt;
        boolean Exception = false;

        amt = Double.parseDouble(loanField.getText());
        if (amt <= 0){
        JOptionPane.showMessageDialog(null, "Loan field needs to be greater than zero",
                    "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                    return;
        }

      if (amt <= 0)
           opField.setText("Principal amount missing");

      else if (intrst <= 0)
           opField.setText("Interest rate missing");

      else if (trm <= 0)
           opField.setText("Number of years missing");

      else
      {

           double balance = new Double(amt).doubleValue();
           double intyr   = new Double(intrst).doubleValue() / 100;
           short nyears   = (short) trm; //new Integer (trm).intValue();

           StringBuffer msg = new StringBuffer();
            msg.append("\nprincipal=" + balance + "  interest=" + intyr
                     + "  years=" + nyears + "\n");

            double beginningMortgage = intyr / 12.;
            int npmts = nyears * 12;
            double pmt = balance * (beginningMortgage / (1.- Math.pow(1.+ beginningMortgage,-npmts)));

            msg.append("payment \ttotal\tinterest\tprincipal\tbalance\n");
            msg.append("number \tpayment\tpayment\tpayment\n");
            msg.append("\t\t\t\t" + balance + "\n");



          for (short mo = 1; mo <= npmts; ++mo)
          {
            double principalpayment, initialpayment = balance * beginningMortgage;
            if (mo < npmts)
                principalpayment = pmt - initialpayment;
                else principalpayment = balance;
                balance -= principalpayment;
                msg.append(mo + "\t" + format(initialpayment + principalpayment)

                    + "\t" + format(initialpayment)
                    + "\t" + format(principalpayment)
                    + "\t" + format(balance) + "\n");
        }

            opField.setText(msg.toString());
        }
    }

    public static void main(String[] args) {
        SteveMortCalcWk4 myMort = new SteveMortCalcWk4();
        myMort.mortCalcForm();
    }
}
0
Mick BarryJava DeveloperCommented:
you're just missing a brace

if (source==options) {
          switch (options.getSelectedIndex()) {
          case 0:
                termField.setText("7");
                entryField.setText("5.35");
                break;

          case 1:
                 termField.setText("15");
                 entryField.setText("5.5");
                 break;

          case 2:
                 termField.setText("30");
                         entryField.setText("5.75");
                 break;
          }
}


0
imagekrazyAuthor Commented:
Okay getting close , now it  saying one error, illegal character }
0
Mick BarryJava DeveloperCommented:
looks ok, check you haven't got a control character in there

0
imagekrazyAuthor Commented:
Hello again , i can't find any control character, is there anything i can do to find it, i looked but i can't find it
0
Mick BarryJava DeveloperCommented:
try deleting the line, and type it in again

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imagekrazyAuthor Commented:
Hello again , thank you, I just removed the curly brackets and put it back and that worked , thank you very much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.