Java Payment Calculator 1.2

Page 1 of 1

1 Replies - 599 Views - Last Post: 11 July 2011 - 12:34 PM Rate Topic: -----

#1 javaconfused  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-July 11

Java Payment Calculator 1.2

Posted 11 July 2011 - 12:26 PM

I have finished this code, it compiles and executes well I just can not get the "calculate" feature to actually calculate. It just throws up "action button" on the DOS screen. Any help would be appreciated...

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.*;

// Option class declaration used to calculate and display the mortgage payment.

public class Option extends JFrame implements ActionListener {

    // Declare the variables
	JLabel titleLabel = new JLabel("Mortgage Payment Calculator");
    JLabel amtLabel = new JLabel("Enter Mortgage Amount: ");
    JLabel termLabel = new JLabel("Enter Term of Loan (yrs): ");
    JLabel rateLabel = new JLabel("Enter Interest Rate(%): ");

    JTextField amtText = new JTextField("0");
    JTextField termText = new JTextField("0");
    JTextField rateText = new JTextField("0");

    JButton buttonCal = new JButton("Calculate");
     JButton buttonClr = new JButton("Reset");

    JTextArea resultArea = new JTextArea(40, 20);

    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    /**
     * Default constructor
     */
    public Option()
    {
        setTitle("Mortgage Payment Calculator - Week 2");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        buildGUI();
    }

    /**
     * Initialize the GUI
     */
    public void buildGUI()
    {
        Container content = getContentPane();
        content.setLayout(null);

        titleLabel.setFont(new Font("tahoma", Font.BOLD, 16));
    	titleLabel.setHorizontalAlignment(JLabel.LEFT);
    	titleLabel.setForeground(Color.BLACK);

        // add to panel
    	content.add(titleLabel);
        content.add(amtLabel);
        content.add(termLabel);
        content.add(rateLabel);
        content.add(amtText);
        content.add(termText);
        content.add(rateText);
        content.add(buttonCal);
        content.add(buttonClr);

        JScrollPane scroll = new JScrollPane(resultArea);
        content.add(scroll);

        // set position
        amtLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        termLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        rateLabel.setHorizontalAlignment(SwingConstants.RIGHT);

        titleLabel.setBounds(120, 20, 240, 20);
        amtLabel.setBounds(50, 60, 150, 30);
        amtText.setBounds(260, 60, 180, 30);
        termLabel.setBounds(50, 100, 150, 30);
        termText.setBounds(260, 100, 180, 30);
        rateLabel.setBounds(50, 140, 150, 30);
        rateText.setBounds(260, 140, 180, 30);

        buttonCal.setBounds(90, 200, 100, 30);
        buttonClr.setBounds(290, 200, 100, 30);
        scroll.setBounds(50, 250, 400, 180);

        // add action listener
        buttonCal.addActionListener(this);
        buttonClr.addActionListener(this);
    }

    /**
     * Handle the action event
     * @param e event
     */
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource()==buttonClr)
        {
            buttonClear();
        }
        else
        	if (e.getSource()==buttonCal)
        	{
        		buttonCalculate();

        	}

    }

    /**
     * Handler for clear button
     */
    public void buttonClear()
    {
        amtText.setText("");
        termText.setText("");
        rateText.setText("");
        resultArea.setText("");
    }

    /**
     * Handler for calculate button
     */
    public void buttonCalculate()
    {
        try
        {
            // get value from the text field
            double amount = Double.parseDouble(amtText.getText());
            int year = Integer.parseInt(termText.getText());
            double rate = Double.parseDouble(rateText.getText());

            // calculate and display the result
            double payment = calcMonthlyPayment(amount, rate / 100, year);
            displayMonthlyPayment(amount, rate / 100, year, payment);
        } catch (Exception e)
        {
        	resultArea.setText("Error: Please try again.\n");
        }
    }

    /**
     * Method to display monthly payment
     * @param principal
     * @param interestYearly
     * @param termYears
     * @param monthlyPayment
     */
    public void displayMonthlyPayment(double principal, double interestYearly, int termYears, double monthlyPayment)
    {
        double interestMonthly = interestYearly / 12.0;
        resultArea.setText("Amount: " + fmt.format(principal) + "\n");
        resultArea.append("Years: " + termYears + "\n");
        resultArea.append(String.format("Interest: %.2f%%\n", interestYearly * 100));
        resultArea.append("Monthly Payment: " + fmt.format(monthlyPayment) + "\n\n");
        resultArea.append("Payment #\tPrinciple\tBalance Paid\tInterest Paid\n");
        for (int num = 1; num <= termYears * 12; num++)
        {
            // calculate interest paid
            double interest = principal * interestMonthly;
            // calculate loan balance paid
            double loan = monthlyPayment - interest;

            // calculate left loan amount
            principal -= loan;

            // print loan balance and interest paid for each payment
            resultArea.append(String.format("%03d\t%s\t%s\t%s\n", num, fmt.format(principal),fmt.format(loan), fmt.format(interest)));

        }
    }

    /**
     * Method to calculate monthly payment
     * @param principal
     * @param interestYearly
     * @param termYears
     * @return monthly payment
     */
    public double calcMonthlyPayment(double principal, double interestYearly, int termYears)
    {
        // calculate monthly interest rate
        double interestMonthly = interestYearly / 12.0;
        // calculate the number of payments
        double numberOfPayments = termYears * 12;

        //  calculate the monthly payment and return the value
        return (principal * interestMonthly) / (1.0 - (Math.pow((1.0 + interestMonthly), -numberOfPayments)));
    }

    /**
     * The main method
     * @param args
     */
    public static void main(String[] args)
    {
        Option window = new Option();
        window.setSize(500, 500);
        window.setLocationRelativeTo(null); // move the window to the center of the screen
        window.setVisible(true);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Payment Calculator 1.2

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12179
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: Java Payment Calculator 1.2

Posted 11 July 2011 - 12:34 PM

Please do not open duplicate threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1