Results 1 to 2 of 2
  1. #1
    kalibballer is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default mortgage calculator help

    hello. i am having trouble with my calculator that i need for an assignment. it only works once. it will calculate everything but wont calculate again after i clear it. any help will be appreciated.


    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;

    public class calculator extends Frame implements ActionListener
    {
    private Button keys[];
    private Panel userInterface, buttonInterface, result1Interface, result2Interface;
    private TextField tfMortgagePayment, tfMortgageAmount;
    private TextArea tfMonthlyPayments;
    private Label label1, label2, label3, label4;
    private Choice availableLoans;
    private double mortgagePayment, loanBalance=0;
    private boolean foundKey;
    private DecimalFormat roundDecimal;
    private double [] intRateAnnual = {5.35, 5.50, 5.75};
    private int termMonths, arrayIndex=0;
    private int [] termYears = {7, 15, 30};


    public calculator()
    {

    roundDecimal = new DecimalFormat("########.##");

    // create an instance of the menu
    MenuBar mnuBar = new MenuBar();
    setMenuBar(mnuBar);

    // construct and populate the File menu
    Menu mnuFile = new Menu("File", true);
    mnuBar.add(mnuFile);
    MenuItem mnuFileExit = new MenuItem("Exit");
    mnuFile.add(mnuFileExit);



    // add the ActionListener to menu item
    mnuFileExit.addActionListener(this);


    // assign an ActionCommand to menu item
    mnuFileExit.setActionCommand("Exit");


    // construct components and initialize beginning values
    tfMortgageAmount = new TextField(20);
    tfMortgageAmount.setEditable(true);
    tfMortgagePayment = new TextField(20);
    tfMortgagePayment.setEditable(false);
    tfMonthlyPayments = new TextArea("",5,55);
    tfMonthlyPayments.setEditable(false);

    userInterface = new Panel();
    buttonInterface = new Panel();
    result1Interface = new Panel();
    result2Interface = new Panel();

    //These create the labels for the text fields
    label1 = new Label();
    label2 = new Label();
    label3 = new Label();
    label4 = new Label();

    label1.setText("Mortgage Amount: ");
    label2.setText("Available Loans: ");
    label3.setText("Payment Summary: ");
    label4.setText("Monthly Mortgage Payment: ");

    //This creates the drop down
    availableLoans = new Choice();

    availableLoans.addItem("7 years at 5.35%");
    availableLoans.addItem("15 years at 5.5%");
    availableLoans.addItem("30 years at 5.75%");

    //This is for the two buttons
    keys = new Button[2];

    // construct and assign captions to the Buttons
    keys[0] = new Button("Calculate");
    keys[1] = new Button("Clear");

    for (int i=0; i<keys.length; i++)
    keys[i].addActionListener(this);

    // set Frame and keypad layout to grid layout
    this.setLayout(new FlowLayout());
    userInterface.setLayout(new GridLayout(5,2,5,5));
    buttonInterface.setLayout(new FlowLayout());
    result1Interface.setLayout(new GridLayout(1,2,5,5));
    result2Interface.setLayout(new GridLayout(2,2,5,5));

    userInterface.add(label1);
    userInterface.add(tfMortgageAmount);
    userInterface.add(label2);
    userInterface.add(availableLoans);
    userInterface.add(keys[0]);
    userInterface.add(keys[1]);
    userInterface.add(label4);
    userInterface.add(tfMortgagePayment);
    userInterface.add(label3);
    result2Interface.add(tfMonthlyPayments);
    this.add(userInterface, BorderLayout.NORTH);
    this.add(result2Interface, BorderLayout.CENTER);


    addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );

    } // end of constructor method

    //Method to determine user selections within the Frame
    public void actionPerformed(ActionEvent e)
    {
    // test for menu item clicks
    String arg = e.getActionCommand();
    if (arg == "Exit")
    System.exit(0);




    // search for the clicked key
    for (int i=0; i<keys.length && !foundKey; i++)
    {
    if(e.getSource() == keys[i])
    {
    foundKey=true;
    switch(i)
    {
    case 0:

    if (isDecimal(tfMortgageAmount.getText(),"Mortgage Amount"))
    {
    arrayIndex = availableLoans.getSelectedIndex();
    loanBalance = Double.parseDouble(tfMortgageAmount.getText());
    termMonths = termYears[arrayIndex] * 12;
    tfMortgagePayment.setText("");
    tfMonthlyPayments.setText("");
    mortgagePayment = calcMonthlyPayment(termYears[arrayIndex], intRateAnnual[arrayIndex], loanBalance);
    tfMortgagePayment.setText(roundDecimal.format(mort gagePayment));

    tfMonthlyPayments.append("Month # \t\tInterest Paid \tRemaining Balance\n");
    tfMonthlyPayments.append("------ \t\t------------- \t---------------\n");

    while (termMonths > 0)
    {
    //Call to Method calcMonthlyBalance
    loanBalance = calcMonthlyBalance (loanBalance, intRateAnnual[arrayIndex], mortgagePayment, termMonths);

    // decrement months
    termMonths--;

    } // end while
    } //end if

    break;
    case 1:
    tfMortgageAmount.setText("");
    tfMortgagePayment.setText("");
    tfMonthlyPayments.setText("");
    availableLoans.select(0);
    tfMortgageAmount.requestFocus();
    break;

    }
    }
    }
    }

    //Start of main
    public static void main(String args[])
    {
    // set frame properties
    calculator f = new calculator();
    f.setTitle("Mortgage Calculator");
    f.setBounds(200,200,420,305);
    f.setVisible(true);

    } // end of main



    public static double calcMonthlyPayment(int termYears, double interestRate, double loanAmount)
    {
    //Variable declarations
    double intRateMonthly, mthPayment;
    int termMonths;

    //Determine Monthly Values based on Annual Amounts
    termMonths = termYears * 12;
    intRateMonthly = (interestRate / 12) / 100;

    //Formula to calculate monthly payment
    mthPayment = (loanAmount * (intRateMonthly)) / (1 - Math.pow(1 / (1 + (intRateMonthly)), termMonths));
    return mthPayment;
    } // end of calcMonthlyPayment



    public static boolean isDecimal(String number, String tfName)
    {
    boolean isValid = true;
    try
    {
    Double.parseDouble(number);

    }
    catch (NumberFormatException ex)
    {
    JOptionPane.showMessageDialog(null, "Please enter "+ tfName +" value. Please try again.");
    isValid = false;
    }
    return isValid;
    }

    /*
    Method for validation of an integer.*/

    public static boolean isInteger(String number, String tfName)
    {
    boolean isValid = true;

    try
    {
    Integer.parseInt(number);

    }
    catch (NumberFormatException ex)
    {
    JOptionPane.showMessageDialog(null, "An integer must be entered for the "+ tfName +" value. Please try again.");
    isValid = false;
    }
    return isValid;
    }

    /*
    Method for calculation */
    public double calcMonthlyBalance (double balance, double intRate, double payment, int month)
    {
    double monthlyInterestPayment, monthlyPrincipalPayment, monthlyBalance;
    //Decimal Format
    DecimalFormat roundDecimal = new DecimalFormat ("0.00");

    // calculate interest and principal payments
    monthlyInterestPayment = (balance * (intRate / 1200));
    monthlyPrincipalPayment = (payment - monthlyInterestPayment);
    monthlyBalance = balance - monthlyPrincipalPayment;

    // This will display the month term, interest paid and Loan balance
    tfMonthlyPayments.append(month +
    "\t\t$" + roundDecimal.format(monthlyInterestPayment) +
    "\t\t$" + roundDecimal.format(monthlyBalance) + "\n");

    return (monthlyBalance);
    }

    } //end

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    foundKey = true;
    code under // search for the clicked key
    run once in the program
    if clear press first, no response for calculate

Similar Threads

  1. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 02:43 PM
  2. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 AM
  3. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 06:33 AM
  4. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 04:27 PM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 12:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •