Link to home
Start Free TrialLog in
Avatar of richmp95
richmp95

asked on

i have two versions trying to get either one to work. both doing the same thing. getting a square in the answer box. the second version i need help to get the amort to work

First version
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MortgageCalc extends JFrame implements ActionListener
{
      //declare variables
      double rateMo, rateApr, monthPmt, principal, iMonths, iYears;

      //create JLabels for entry
      JLabel Dprincipal = new JLabel("Please enter Principal:      ");
      JLabel RateApr = new JLabel("Please enter APR:      ");
      JLabel Years =      new JLabel("Please enter Term:      ");
      JLabel MonthPmt = new JLabel("Your Monthly Payment will be:      ");

      JTextField loanPrincipal = new JTextField("", 10);
      JTextField loanRate = new JTextField("", 10);
      JTextField loanTerm = new JTextField("", 10);
      JTextField loanPayment = new JTextField("", 10);

      //create buttons
      JButton CalcPayment = new JButton("Calculate Payment");
      JButton exit = new JButton("Exit");
      JButton reset = new JButton("Reset");

      public MortgageCalc()
      {
            //Create Frame
            Container container = getContentPane();
            container.setLayout(new BorderLayout(10,10));

            JPanel inputPanel = new JPanel();
            inputPanel.setLayout(new GridLayout(6,4));

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(8,2));

            JPanel answerPanel = new JPanel();
            answerPanel.setLayout(new FlowLayout());

            //add components to input panel
            inputPanel.add(Dprincipal);
            inputPanel.add(loanPrincipal);
            inputPanel.add(RateApr);
            inputPanel.add(loanRate);
            inputPanel.add(Years);
            inputPanel.add(loanTerm);

            //add components to button panel
            buttonPanel.add(CalcPayment);
            buttonPanel.add(reset);
            buttonPanel.add(exit);

            //add componets to answer
            answerPanel.add(MonthPmt);
            answerPanel.add(loanPayment);

            container.add(inputPanel, BorderLayout.WEST);
            container.add(buttonPanel, BorderLayout.EAST);
            container.add(answerPanel, BorderLayout.SOUTH);

            //add listeners
            CalcPayment.addActionListener(this);
            reset.addActionListener(this);
            exit.addActionListener(this);
            loanPayment.setEditable(false);

      }//End MortgageCalc



      public void actionPerformed(ActionEvent e)
      {
            Object source = e.getSource();
            if(source == CalcPayment)
            {
                  String tPrincipal = loanPrincipal.getText();
                  String rateAPR = loanRate.getText();
                  String years = loanTerm.getText();

                  try
                  {
                        double Dprincipal = Double.parseDouble(tPrincipal);
                        if (Dprincipal <= 0.00) throw new NumberFormatException();
                        double rateApr = Double.parseDouble(rateAPR);
                        if (rateApr <= 0.00) throw new NumberFormatException();
                        double iYears = Double.parseDouble(years);
                        if (iYears <= 0.0) throw new NumberFormatException();
                  }

                  catch(NumberFormatException f)
                  {
                        JOptionPane.showMessageDialog(null, "Invalid Entry, Please Try Again.", "Error", JOptionPane.INFORMATION_MESSAGE);
                  }

                  // calculations
                  rateMo = (rateApr /12)/100;
                  iMonths = (iYears * 12);

                  monthPmt = (principal * rateMo) / (1- Math.pow(1 + rateMo, - iMonths));
                  DecimalFormat twoDigits = new DecimalFormat("$#,###.00");

                  loanPayment.setText(twoDigits.format(monthPmt));
            }

            else if (source == reset)
            {
                  loanPrincipal.setText("");
                  loanRate.setText("");
                  loanTerm.setText("");
                  loanPayment.setText("");
                  loanPrincipal.requestFocus();
            }

            else
            {
                  System.exit(0);
            }
      }//end

      public static void  main(String[] args) throws IOException
      {
            MortgageCalc frame = new MortgageCalc();
            frame.setTitle("Mortgage Payment Calculator V2.0");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(1200,800);
            frame.setVisible(true);
      }//end Main

      public double getResult()
      {
            return monthPmt;
      }


}//end program


second version
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Mortgage extends JFrame implements ActionListener
{
            // declare some variables
            double dRateMo, dRateAPR, dMonthPmt, dPrincipal, iMonths, iYears;

            // create JLabels for entry fields
            JLabel greeting = new JLabel("Good Day! Please enter your name.");
            JLabel Principal = new JLabel("Please enter principal:    ");
            JLabel rateAPR = new JLabel("Please enter loan rate:      ");
            JLabel years = new JLabel("Please enter term:      ");
            JLabel payment = new JLabel("Your monthly payment will be:  ");

            // create textfields for user entries
            JTextField nameField = new JTextField("", 10);
            JTextField loanPrincipal = new JTextField("", 10);
            JTextField loanRate = new JTextField("", 10);
            JTextField loanTerm = new JTextField("", 10);
            JTextField loanPayment = new JTextField("", 20);

            //create buttons for use in panels
            JButton calcPayment = new JButton("Calculate Payment");
            JButton calcAmort = new JButton("Show Amortization Table");
            JButton nameEntry = new JButton("Enter Name");
            JButton exit = new JButton("Exit");
            JButton reset = new JButton("Reset");

      public Mortgage()
      {
            // create frame and panels
            Container container = getContentPane();
            container.setLayout(new BorderLayout(5,5));

            JPanel inputPanel = new JPanel();
            inputPanel.setLayout(new GridLayout(3,2));

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(4,1));

            JPanel greetingPanel = new JPanel();
            greetingPanel.setLayout(new FlowLayout());

            JPanel answerPanel = new JPanel();
            answerPanel.setLayout(new FlowLayout());

            // add compenents to greeting panel
            greetingPanel.add(greeting);
            greetingPanel.add(nameField);
            greetingPanel.add(nameEntry);

            //add components to input panel
            inputPanel.add(Principal);
            inputPanel.add(loanPrincipal);
            inputPanel.add(rateAPR);
            inputPanel.add(loanRate);
            inputPanel.add(years);
            inputPanel.add(loanTerm);

            //add components to button panel
            buttonPanel.add(calcPayment);
            buttonPanel.add(calcAmort);
            buttonPanel.add(reset);
            buttonPanel.add(exit);

            //add components to answer panel
            answerPanel.add(payment);
            answerPanel.add(loanPayment);

            //add panels to frame
            container.add(greetingPanel, BorderLayout.NORTH);
            container.add(inputPanel, BorderLayout.WEST);
            container.add(buttonPanel, BorderLayout.EAST);
            container.add(answerPanel, BorderLayout.SOUTH);

            // add listeners to buttons
            calcPayment.addActionListener(this);
            calcAmort.addActionListener(this);
            reset.addActionListener(this);
            exit.addActionListener(this);
            nameEntry.addActionListener(this);
            loanPayment.setEditable(false);
      }

      public void actionPerformed(ActionEvent e)
      {
            Object source = e.getSource();
            if (source == nameEntry)
            {
                  String name = nameField.getText();
                  remove(nameField);
                  remove(nameEntry);
                  validate();
                  greeting.setText("Welcome, " + name + " !");

            }

            else if (source == calcPayment)
            {
                  String totPrincipal = loanPrincipal.getText();
                  String rateAPR = loanRate.getText();
                  String years = loanTerm.getText();

                  try
                  {
                        double dPrincipal = Double.parseDouble(totPrincipal);
                        if (dPrincipal <= 0.00) throw new NumberFormatException();
                        double dRateAPR = Double.parseDouble(rateAPR);
                        if (dRateAPR <= 0.00) throw new NumberFormatException();
                        double iYears = Double.parseDouble(years);
                        if (iYears <= 0.0) throw new NumberFormatException();

                  }

                  catch(NumberFormatException f)
                  {
                        JOptionPane.showMessageDialog(null, "Invalid Entry, please try again.", "Error", JOptionPane.INFORMATION_MESSAGE);
                  }

                  // declare conversion for APR to monthly rate and years to months

                  iMonths = iYears * 12;
                  dRateMo = dRateAPR / (12*100);

                  dMonthPmt = getPayment(dRateMo, iMonths, dPrincipal);
                  DecimalFormat twoDigits = new DecimalFormat("$###,000.00");

                  loanPayment.setText(twoDigits.format(dMonthPmt));
            }

            else if (source == calcAmort)
            {

                  JOptionPane.showMessageDialog(null, "Under Construction", "Error", JOptionPane.INFORMATION_MESSAGE);
                  /*String Principal = loanPrincipal.getText();
                  String rateAPR = loanRate.getText();
                  String years = loanTerm.getText();

                  try
                  {
                        double dPrincipal = Double.parseDouble(Principal);
                        if (dPrincipal <= 0.00) throw new NumberFormatException();
                        double dRateAPR = Double.parseDouble(rateAPR);
                        if (dRateAPR <= 0.00) throw new NumberFormatException();
                        double iYears = Double.parseDouble(years);
                        if (iYears <= 0.0) throw new NumberFormatException();

                  // declare conversion for APR to monthly rate and years to months
                        iMonths = iYears * 12;
                        dRateMo = dRateAPR / (12*100);
                        dMonthPmt = getPayment(dRateMo, iMonths, dPrincipal);
                        PaymentCalc.Calculation(dPrincipal, dRateMo, dMonthPmt, iMonths);
                        if (dPrincipal <= 0.0 || dRateMo <= 0.0 || dMonthPmt <= 0.0 || iMonths <= 0)
                        throw new NumberFormatException();

                  }

                  catch(NumberFormatException f)
                  {
                        JOptionPane.showMessageDialog(null, "Invalid Entry, please try again.", "Error", JOptionPane.INFORMATION_MESSAGE);
                  }
                  */

            }

            else if (source == reset)
            {
                  loanPrincipal.setText("");
                  loanRate.setText("");
                  loanTerm.setText("");
                  loanPayment.setText("");
                  loanPrincipal.requestFocus();

            }

            else
            {
                  // user clicks exit button
                  System.exit(0);
            }
      }

      public static void main(String[] args) throws IOException
      {
            Mortgage frame = new Mortgage();
                  frame.setTitle("Mortgage Payment Calculator");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setSize(600,400);
                  frame.setVisible(true);


      }//end main


      // The getPayment method accepts Monthly rate, months, and principal variables
      // and returns the monthly payment.
      public static double getPayment(double dRateMo, double iMonths, double dPrincipal)

      {

            return (dRateMo + (dRateMo / ((Math.pow((1+ dRateMo),iMonths)) - 1)))*dPrincipal;
      }//end getpayment

}//end class
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada 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
sorry that was for the second one (Mortgage)
and the first one (MortgageCalc) should be

   double rateApr=0, iYears=0;
   try
   {
            principal = Double.parseDouble(tPrincipal);
            if (principal <= 0.00) throw new NumberFormatException();
            rateApr = Double.parseDouble(rateAPR);
            if (rateApr <= 0.00) throw new NumberFormatException();
            iYears = Double.parseDouble(years);
            if (iYears <= 0.0) throw new NumberFormatException();
   }