troubleshooting Question

Java Mortgage Calculator GUI

Avatar of kevcodca3
kevcodca3 asked on
Programming Languages-OtherProgrammingJava
3 Comments1 Solution573 ViewsLast Modified:
The calculator will not calculate. It compiles and runs, but that is about it. Here is the code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class DoraMCalc2 extends JFrame implements ActionListener
{
    double rate = 0;  //APR
    double monthlyPayment = 0; //Monthly payment amount
    double principle = 0; //Amount of loan
    int term = 0; //Mortgage term in years
    double interest = 0; //Interest, in months
    int notePeriod = 0; //Mortgage term in months

    String mTerm[] = {"7", "15", "30"};
    String mRate[] = {"5.35%", "5.50%", "5.75%"};

    //Labels and Input Fields

    JPanel row1 = new JPanel();
    JLabel mortgageLabel = new JLabel("DORAS MORTGAGE CALCULATOR", JLabel.CENTER);

    JPanel row2 = new JPanel(new GridLayout(1, 2));
    JLabel principleLabel = new JLabel("Mortgage Principle $",JLabel.LEFT);
    JTextField principleTxt = new JTextField(8);

    JPanel radioPanel = new JPanel();
    JRadioButton buttonA = new JRadioButton("7 Years at 5.35%" , false);
    JRadioButton buttonB = new JRadioButton("15 Years at 5.50%" , false);
    JRadioButton buttonC = new JRadioButton("30 Years at 5.75%", false);

    JPanel row5 = new JPanel(new GridLayout(1, 2));
    JLabel monthlyPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
    JTextField monthlyPaymentTxt = new JTextField(8);

    //Create buttons
    JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 8, 8));
    JButton amortizeButton = new JButton("Amortize Payments");
    JButton clearButton = new JButton("Clear");
    JButton exitButton = new JButton("Exit");
    JButton calculateButton = new JButton("Calculate");

    JTextArea displayArea = new JTextArea(10, 30);
    JScrollPane scroll = new JScrollPane(displayArea);

    public DoraMCalc2()
    {
        super ("DORAS Mortgage Payment Calculator"); //Header
        setSize(450, 400); //GUI frame size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container pane = getContentPane();
        pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));

        Border rowborder = new EmptyBorder( 3, 10, 3, 10 ); //Border around elements inside frame

        //Pane and row properties, GUI layout
        JPanel panel = new JPanel(new GridLayout(5,1));
        panel.add(row1);
        row1.add(mortgageLabel);
        row1.setMaximumSize( new Dimension( 1000, row1.getMinimumSize().height));
        row1.setBorder( rowborder);

        panel.add(row2);
        row2.add(principleLabel);
        row2.add(principleTxt);
        row2.setMaximumSize( new Dimension( 1000, row2.getMinimumSize().height));
        row2.setBorder( rowborder);

        panel.add(radioPanel);
        ButtonGroup bgroup = new ButtonGroup();
        bgroup.add(buttonA);
        bgroup.add(buttonB);
        bgroup.add(buttonC);

        radioPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4 ));
        radioPanel.add(buttonA);
        radioPanel.add(buttonB);
        radioPanel.add(buttonC);

//        pane.add(radioPanel);
        radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height));
        radioPanel.setBorder( rowborder);

        panel.add(row5);
        row5.add(monthlyPaymentLabel);
        row5.add(monthlyPaymentTxt);
        monthlyPaymentTxt.setEnabled(false); //Set payment amount uneditable
        row5.setMaximumSize( new Dimension( 1000, row5.getMinimumSize().height));
        row5.setBorder( rowborder);

        panel.add(button); //Add buttons
        button.add(calculateButton);
        button.add(clearButton);
        button.add(exitButton);
        button.add(amortizeButton);
        pane.add(panel);
        button.setMaximumSize( new Dimension( 1000, button.getMinimumSize().height));

       scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        pane.add(scroll);

        setVisible(true);
        setContentPane(pane);

        amortizeButton.addActionListener(this); //Add listeners
        clearButton.addActionListener(this);
        exitButton.addActionListener(this);
        calculateButton.addActionListener(this);
        buttonA.addActionListener(this);
        buttonB.addActionListener(this);
        buttonC.addActionListener(this);

    }

    public void actionPerformed(ActionEvent event)
    {
        Object command = event.getSource();

           if(command == calculateButton)
      {
          try
          {
              principle = Double.parseDouble(principleTxt.getText());
         
         
          //Select rate and term
          if(buttonA.isSelected() == true)
          {
              rate = 5.35;
              term = 7;
          }
          else if(buttonB.isSelected() == true)
          {
              rate = 5.5;
              term = 15;
          }
          else if (buttonC.isSelected() == true)
          {
              rate = 5.75;
              term = 30;
          }
          else //none of the buttons  are selected,  this is an actual error.  Throw an exception
          {

      //Calculate monthly interest rate
      interest = ((rate / 100) / 12);

      //Calculate total number of payments
      notePeriod = term * 12; // (for example, 5 years means notePeriod = 60)

       //Calculate monthly payment
      if (rate == 0.0)
      {
          monthlyPayment = principle / notePeriod;
      }
      else
          {
              monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
          }

      //Currency formatter
      NumberFormat myCurrencyFormatter;
      myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
      monthlyPaymentTxt.setText(myCurrencyFormatter.format(monthlyPayment));
        }

        if(command == clearButton)
        {
             principleTxt.setText(null);
             monthlyPaymentTxt.setText(null);
             displayArea.setText(null);
        }

        if(command == exitButton)
        {
             System.exit(0);
        }
        if (command == amortizeButton)
        {
      double year = 0;
      double interest = 0;
      double balance = 0;
      double monthlyInterest = 0;
      int x = 1;
      int months = term * 12;
      String titles = "Month\t Principle\tInterest\tbalance\n";

      interest = (rate / 100) /12;
      balance = principle;
     
      displayArea.setText(titles);

      for (x = 1; x <= months; ++x)
      {
          //Calculate Detailed Payment Info
          monthlyInterest = balance * interest;

          if(interest != 0)

          year = monthlyPayment - monthlyInterest;
          balance = principle - year;

          //Format Variables
          DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //Crrency
          DecimalFormat pf = new DecimalFormat("#,##0.00%"); //Percentages
          DecimalFormat mi = new DecimalFormat("#,##0.000%"); //Percentages

          //Positions the Cursor to the Top of the TextArea
          displayArea.setCaretPosition(0);
          displayArea.append((months +1) + ")\t"+df.format(principle)+"\t"+df.format(interest)+"\t"+df.format(balance)+"\n");
      }
   }
}

      catch(NumberFormatException e)
      {
         
              JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
          }
    }
}
      public void Payment()
    {
    //Get user input
    principle = Double.parseDouble(principleTxt.getText());

    //Calculate monthly interest rate
    interest = ((rate / 100) / 12);

    //Calculate total number of payments
    notePeriod = term * 12;

    //Calculate monthly payment
    if (rate == 0.0)
    {
        monthlyPayment = principle / notePeriod;
    }
    else
        {
            monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
        }

    //Currency formatter
    NumberFormat myCurrencyFormatter;
    myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
    monthlyPaymentTxt.setText(myCurrencyFormatter.format(monthlyPayment));
}
    public static void main (String[] arguments) //Main Method
    {
        DoraMCalc2 smc = new DoraMCalc2();
        smc.setVisible(true);
        smc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            }
      }//End of program
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros