Java GUI Mortgage Program, Output Incorrect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaidalin79
    New Member
    • Nov 2006
    • 59

    Java GUI Mortgage Program, Output Incorrect

    I have finally gotten my GUI to look like I want it to, but I am having trouble getting the calculations right. No matter what I put in there, it seems to calculate a large payment, and a very wrong amortization schedule... Here is what I have so far...


    Code:
    package guiweek3;
    
    //imports necessary tools
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import javax.swing.text.*;
    import java.text.*;
    import java.util.*;
    
    //creates the different fields, labels for the fields, and the panel and buttons
    public class guiweek3 extends JFrame implements ActionListener 
    {
        //creates gui panel and compnents including text fields and labels
        private JPanel panelTop = new JPanel(new GridLayout (3,3));
        private JPanel panelMid = new JPanel(new FlowLayout());
        private JPanel panelBottom = new JPanel();
        private JTextField amountField = new JTextField();
        private JTextField paymentField = new JTextField();
        private JLabel paymentLabel = new JLabel("Monthly Payment:");
        private JLabel amountLabel = new JLabel("Enter Your Loan Amount: $");
        private JLabel loanLabel = new JLabel("Choose Your Loan Terms:");
        private JComboBox loanMenu = new JComboBox();
        private JTextArea amortizationSchedule = new JTextArea(25,50);
        private JButton calculateButton = new JButton("Calculate Payment");
        private JButton clearButton = new JButton("Clear Fields");
        private JButton quitButton = new JButton("Quit");
        static DecimalFormat usDollar = new DecimalFormat("$###,##0.00");
          
        //create arrays for the three loans
        int[] yearsArray = {7, 15, 30};
        double[] interestArray = {5.35, 5.50, 5.75};
        
        //exit action
        public guiweek3() 
        {
            initComponents();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            pack();
    
            //adds the listen action to the calculate, clear, and quit buttons
            calculateButton.addActionListener(this);
            quitButton.addActionListener(this); 
            clearButton.addActionListener(this);
        }
    
        //adds the individual components (panel, fields, labels, buttons)to the 
        //panel, sets column sizes for the text fields. Makes payment field static.
        public void initComponents() 
        {
            //creates the object attributes
            amountField.setColumns(8);
            paymentField.setColumns(8);
            paymentField.setEditable(false);
    
            Container pane = getContentPane();
            pane.setLayout(new FlowLayout());
            JScrollPane amortizationPanel = new JScrollPane(amortizationSchedule);
            amortizationSchedule.setEditable(false);
    
            //creates the components and adds them to the panel
            panelTop.add(amountLabel);
            panelTop.add(amountField);
            panelTop.add(loanLabel);
            panelTop.add(loanMenu);
            panelTop.add(paymentLabel);
            panelTop.add(paymentField);
            panelMid.add(calculateButton);  
            panelMid.add(clearButton);
            panelMid.add(quitButton); 
            panelBottom.add(amortizationPanel);
            pane.add(panelTop);
            pane.add(panelMid);
            pane.add(panelBottom);
          
            for(int i=0; i<3; i++)
            {
                loanMenu.addItem(interestArray[i] + "% interest for " + yearsArray[i] + " years");
            } //end for     
        }//end initComponents
    
        //creates frame called guiweek2
        public static void main(String[] args) throws IOException
        {
            guiweek3 frame = new guiweek3();
            frame.setTitle("Calculator Your New Mortgage Payment");
            frame.setBounds(200, 200, 600, 600);
        }//end main
        
        //if the calculate button is pressed, set the amount
        public void actionPerformed(ActionEvent event) 
        {       
            if (event.getSource() == calculateButton) 
            {       
                //error handling try/catch, and cases for the different loans avaialble
                try
                {
                    double term;
                    double rate; 
                    
                    switch (loanMenu.getSelectedIndex())
                    {
                        case 0:
                        rate = interestArray[0];
                        term = yearsArray[0];
                        break;
                        
                        case 1:
                        rate = interestArray[1];
                        term = yearsArray[1];
                        break;
                        
                        case 2:
                        rate = interestArray[2];
                        term = yearsArray[2];
                        break;
                    }//end switch       
    
                    double amount = Double.parseDouble (amountField.getText());                        
                       
                    //formatting for currencies and percentages
                    NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);                      
                                     
                    for (int i = 0; i<3; ++i)
                    {                                        
                        double monthlyInterest = interestArray[i] / 12;
                        double months = yearsArray[i] * 12;            
                        double payment = (amount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), months * -1));                   
                        double remainingPrincipal = (amount - payment);
                               
                        paymentField.setText(usDollar.format(payment));                                                      
                
                        //calculations for amortization results
                        while (remainingPrincipal > 0)     
                        {
                            double interestPayment = (remainingPrincipal * (interestArray[i] / 1200) * 1);                          
                                          
                            //Display loan balance and interest paid
                            amortizationSchedule.append ("Balance:" + usDollar.format(remainingPrincipal) 
                            + "\tInterest Paid:" + usFormat.format(interestPayment) +"\n");
    
                            // Subtract amount paid to principal from loan balance
                            remainingPrincipal = remainingPrincipal - payment;
                        }//end while
                    }//end for
                }//end try
                
                //error message to user if loan is not entered as a number
                catch(NumberFormatException i)
                {
                    JOptionPane.showMessageDialog(null, "You must enter the loan " +
                            "amount as a number.", "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);
                }//end catch
            }//end if calculate
            
            //if the clear button is pressed, clear the fields
            if (event.getSource() == clearButton) 
            {
                amountField.setText("");
                paymentField.setText("");
                amortizationSchedule.setText("");
            }//end if clear statement
                
            //if the quit button is pressed, exit the application
            if (event.getSource() == quitButton) 
            {
                System.exit(0);
            }//end if quit statement
        }//end actionPerformed
    }//end class guiweek3
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Please assume an input and also the expected output,

    Sukatoa,

    Comment

    • zaidalin79
      New Member
      • Nov 2006
      • 59

      #3
      Originally posted by sukatoa
      Please assume an input and also the expected output,

      Sukatoa,
      The user could put in 200000 as the loan amount. The program should display the monthly payment amount based on the loan they chose from the drop down, and then display the amortization schedule in nthe larger text area; showing the remaining balance and payment to interest for each payment through the life of the loan. I am able to do what I need without the gui, but when I try to move over my code and calculations, I cannot seem to get it right....

      Please help!!

      Thanks!!

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by zaidalin79
        The user could put in 200000 as the loan amount. The program should display the monthly payment amount based on the loan they chose from the drop down, and then display the amortization schedule in nthe larger text area; showing the remaining balance and payment to interest for each payment through the life of the loan. I am able to do what I need without the gui, but when I try to move over my code and calculations, I cannot seem to get it right....

        Please help!!

        Thanks!!
        I would like to help you... but it needs time to analyze if you just show the input and without the expected output... And also without the formula...

        I have no experience in accounting...


        If you assume that the loan amount is 200000,

        and the loan terms is 5.35% interest for 7 years ( if i am right, total interests for 7 years to pay...)

        And im going to pay it in installment for 7 years,

        5.35% of 200000 is 10700 right?

        The total payment i must pay before or in 7 years is 210700? am i right?

        84 months in 7 years right?

        210700 divide by 84 months is 2508.33 right?

        That is the monthly payment i should pay...


        If the assumptions above is correct, then maybe there is something you must fix in the calculations...
        At your program,

        i assume 200000 in loan amount,
        Monthly payments is 93.8 thousand dollars...

        It should be 2508.33 dollars per month...

        At the textArea, it is confusing,

        Why you put the balance first before the interest paid?
        Is that mean that when i pay 494.13, my new balance is 110833.33?

        Can you tell me what is the correct output of your calculations?
        if 200000,
        and assume 5.35%

        What is the monthly payment? if my assumptions above is incorrect....
        What is my new balance if i pay that amount?
        What's the next?

        Sorry for asking you that way,
        Because i need a specific question and answer...

        In calculation, if A and B = C and all other variables is dependent on C, and if C got the wrong answer, then all computation will also lead to a wrong answer.

        Correct me if im wrong,
        Sukatoa

        Comment

        Working...