This assignment requires that I allow for user input in a GUI for principal, interest rate, and term in years. I also have to provide a pulldown with 3 preset rates that will also load a cooresponding term length. All of that plus an amoritization table as well.

I have a good start just can't get it to call my user entry now.

/*
  Mortgage Calculator Application
  joeweek4_IA.java
  Programer: Joe Klink
  PRG/421  University of Phonenix

  Purpose:This project calculates the monthly payment of a mortgage and displays a amortization table.
  			 The user can select a loan from a drop down menu.
  version: 1.00 2008/12/13
 */

//imports required packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;


public class joeweek4 extends JPanel implements ActionListener {

  //global variable
    private JComboBox IntRates;
    private JLabel prinlb, rateLb, termLb, paymtLb;
    private JButton Calc, Reset;
    private JTextField prinTxFld, rateTxFld, termTxFld, monthlyPayment;
    private TextArea PayAmort;
    
    String currentInt;

    public joeweek4()
    {
        String[] comboItems={"5.35","5.50","5.75"};
        currentInt = comboItems [0];
        //remove later IntRates = new JComboBox(comboItems);
        
        //creates labels
        prinlb = new JLabel("Principal Amount:");
        rateLb = new JLabel("Interest Rate:");
        termLb = new JLabel("Term:");
        paymtLb = new JLabel("Monthly Payment:");

        //creates textfields
        prinTxFld = new JTextField("",10);
        rateTxFld = new JTextField("",5);
        termTxFld = new JTextField("",5);
        monthlyPayment = new JTextField("",10);

        //combobox pulldown
        JComboBox IntRates = new JComboBox(comboItems);
        IntRates.setEditable(true);
        IntRates.addActionListener(this);
        PayAmort = new TextArea(20,70);

        //create buttons
        Calc = new JButton("Calculate");
        Calc.setActionCommand ("GO");

        Reset = new JButton("Reset");
        Reset.setActionCommand("Reset");

        //add action to button
        Calc.addActionListener(this);
        Reset.addActionListener(this);

        //add labels and fields
        add(prinlb);
        add(prinTxFld);
        add(rateLb);
        add(IntRates);
        add(rateTxFld);
        add(termLb);
        add(termTxFld);
        add(paymtLb);
        add(monthlyPayment);
        add(PayAmort);
        add(Calc);
        add(Reset);
    }
   	    public void actionPerformed(ActionEvent e)
    {
        if ("GO".equals(e.getActionCommand()))
        {
            CalculateMortgage();
        }
        else
        {
            prinTxFld.setText("");
            rateTxFld.setText("");
            termTxFld.setText("");
            monthlyPayment.setText("");
            PayAmort.setText ("");
        }
    }

            public void CalculateMortgage()
    {

      //string array to accept which terms to use
        rateTxFld.setText("" + (String)IntRates.getSelectedItem());
        double principle=Double.parseDouble (prinTxFld.getText());
        double dblrateTxFld=Double.parseDouble(rateTxFld.getText());

           if (dblrateTxFld == 5.35)
            termTxFld.setText("7");

            	else if (dblrateTxFld == 5.50)
            		termTxFld.setText("15");

            		else if (dblrateTxFld == 5.75)
            			termTxFld.setText("30");

        int inttermTxFld=Integer.parseInt(termTxFld.getText());

        // Formats the output of dollar figures
    	   DecimalFormat two_decimal=new DecimalFormat("$0,000.00");

        	double Payment;
        	double InterestPaid;
        	double prinTxFldBal;
        	double temp;
        	int Months;
       		double intMonthlyRate = dblrateTxFld/100;
       	 	int MonthTerm = inttermTxFld * 12;
        	Months=MonthTerm;

        //formulas
        	Payment=(principle * (intMonthlyRate/12)) / (1-1/Math.pow((1+intMonthlyRate/12), MonthTerm));

        //converts monthly payment to decimal format
       		monthlyPayment.setText("" + (two_decimal.format(Payment)));

       		PayAmort.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");

        	for (int counter=0; counter < MonthTerm; counter++)
        {
            temp=(1-1/Math.pow((1+intMonthlyRate/12), Months));
            InterestPaid = Payment * temp;
            principle = (principle - Payment + InterestPaid);
            MonthTerm = MonthTerm --;
            Months--;

            PayAmort.append((counter + 1) + "\t\t" + (two_decimal.format(Payment) + "\t\t" + (two_decimal.format(principle)
           + "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));

       }

    }

    private static void createAndShowGui()
    {
        JFrame frame = new JFrame("Mortgage Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        joeweek4 myCalculator = new joeweek4();
        myCalculator.setOpaque(true);
        frame.setContentPane(myCalculator);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(
            new Runnable()
            {
                public void run()
                {
                    createAndShowGui();
                }
        }

        );

    }



}

Recommended Answers

All 3 Replies

I made few changes so have look at it

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;


public class MorgageCalculator extends JPanel{

	//global variable
	private JComboBox IntRates;
	private JLabel prinlb, rateLb, termLb, paymtLb;
	private JButton calBtn, resetBtn;
	private JTextField prinTxFld, rateTxFld, termTxFld, monthlyPayment;
	private TextArea payAmortTA;
	
	String currentInt;
	
	public MorgageCalculator()
	{
		String[] comboItems={"5.35","5.50","5.75"};
		//currentInt = comboItems [0];
		//remove later IntRates = new JComboBox(comboItems);
		
		//creates labels
		prinlb = new JLabel("Principal Amount:");
		rateLb = new JLabel("Interest Rate:");
		termLb = new JLabel("Term:");
		paymtLb = new JLabel("Monthly Payment:");
		
		//creates textfields
		prinTxFld = new JTextField("",10);
		rateTxFld = new JTextField("",5);
		rateTxFld.setEditable(false);
		termTxFld = new JTextField("",5);
		monthlyPayment = new JTextField("",10);
		monthlyPayment.setEditable(false);
		
		//combobox pulldown
		IntRates = new JComboBox(comboItems);
		IntRates.setEditable(false);
		/*ComboBox doesn't need it
		 IntRates.addActionListener(this);*/
		payAmortTA = new TextArea(20,50);
		
		//create buttons
		calBtn = new JButton("Calculate");
		calBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{
				calculateMortgage();
			}
		});
		
		resetBtn = new JButton("Reset");
		resetBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{
				prinTxFld.setText("");
				rateTxFld.setText("");
				termTxFld.setText("");
				monthlyPayment.setText("");
				payAmortTA.setText ("");
			}
		});
		
		//add labels and fields
		add(prinlb);
		add(prinTxFld);
		add(rateLb);
		add(IntRates);
		add(rateTxFld);
		add(termLb);
		add(termTxFld);
		add(paymtLb);
		add(monthlyPayment);
		add(payAmortTA);
		add(calBtn);
		add(resetBtn);
	}
	
	public void calculateMortgage()
	{
	
		//string array to accept which terms to use
		rateTxFld.setText(IntRates.getSelectedItem().toString());
		double principle=Double.parseDouble (prinTxFld.getText());
		double dblrateTxFld=Double.parseDouble(rateTxFld.getText());
		
		if (dblrateTxFld == 5.35)
		termTxFld.setText("7");
		
		else if (dblrateTxFld == 5.50)
		termTxFld.setText("15");
		
		else if (dblrateTxFld == 5.75)
		termTxFld.setText("30");
		
		int inttermTxFld=Integer.parseInt(termTxFld.getText());
		
		// Formats the output of dollar figures
		DecimalFormat two_decimal=new DecimalFormat("$0,000.00");
		
		double Payment;
		double InterestPaid;
		double prinTxFldBal;
		double temp;
		int Months;
		double intMonthlyRate = dblrateTxFld/100;
		int MonthTerm = inttermTxFld * 12;
		Months=MonthTerm;
		
		//formulas
		Payment=(principle * (intMonthlyRate/12)) / (1-1/Math.pow((1+intMonthlyRate/12), MonthTerm));
		
		//converts monthly payment to decimal format
		monthlyPayment.setText("" + (two_decimal.format(Payment)));
		
		payAmortTA.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");
		
		for (int counter=0; counter < MonthTerm; counter++)
		{
			temp=(1-1/Math.pow((1+intMonthlyRate/12), Months));
			InterestPaid = Payment * temp;
			principle = (principle - Payment + InterestPaid);
			MonthTerm = MonthTerm --;
			Months--;
			
			payAmortTA.append((counter + 1) + "\t\t" + (two_decimal.format(Payment) + "\t\t" + (two_decimal.format(principle)
			+ "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));
			
		}
	
	}
	
	private static void createAndShowGui()
	{
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		MorgageCalculator calculator = new MorgageCalculator();
		calculator.setOpaque(true);
		frame.setContentPane(calculator);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		javax.swing.SwingUtilities.invokeLater(
		new Runnable()
		{
			public void run()
			{
				createAndShowGui();
			}
		}
		
		);	
	}
}

Few recommendations

  • you should use some layout
  • before calculation you should run check() method that will check if the fields been fill in (left for you to implement)
  • start follow naming convention class name start with capital letter any any other world should starts with capital too(example MorgageCalculator), methods names starts with lower letter but every new word starts with capital (example calculateMorgage() ), variable names same as method names just in case of constants you should use all capitals words separated by underscore (example INTEREST_RATE)
  • next time when you post code please use code tags [code] YOUR CODE HERE [/code] or [code=Java]YOUR CODE HERE [/code]

Thanks for the help. I will implement your suggestions. The program runs now, except I need to be able to have user entered intereset rate and term as well as picking from the drop down. That is why I used the editable combobox.

Disregard my last post. If I made the editable statement true it works as I need.

Thanks a million for you help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.