All pastes #2053352 Raw Edit

Anonymous

public text v1 · immutable
#2053352 ·published 2011-05-03 05:05 UTC
rendered paste body
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class MortgageCalculatorWk3 extends javax.swing.JFrame {
    private DefaultListModel resultsList;
    private JList Schedule;

    /** Creates new form MortgageCalculatorWk3 */
    public MortgageCalculatorWk3() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        loanAmountText = new javax.swing.JTextField();
        sevenYrsButton = new javax.swing.JButton();
        fifteenYrsButton = new javax.swing.JButton();
        thirtyYrsButton = new javax.swing.JButton();
        clearSelectionButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        monthlyPaymentText = new javax.swing.JTextField();
        exitProgramButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(102, 0, 102));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Mortgage Calculator", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Elephant", 1, 18), new java.awt.Color(255, 255, 255))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Elephant", 1, 12));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Original Loan Amount:");

        sevenYrsButton.setBackground(new java.awt.Color(204, 51, 255));
        sevenYrsButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        sevenYrsButton.setText("7 Years, 5.35%");
        sevenYrsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sevenYrsButtonActionPerformed(evt);
            }
        });

        fifteenYrsButton.setBackground(new java.awt.Color(204, 51, 255));
        fifteenYrsButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        fifteenYrsButton.setText("15 Years, 5.5%");
        fifteenYrsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fifteenYrsButtonActionPerformed(evt);
            }
        });

        thirtyYrsButton.setBackground(new java.awt.Color(204, 51, 255));
        thirtyYrsButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        thirtyYrsButton.setText("30 Years, 5.75%");
        thirtyYrsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                thirtyYrsButtonActionPerformed(evt);
            }
        });

        clearSelectionButton.setBackground(new java.awt.Color(204, 51, 255));
        clearSelectionButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        clearSelectionButton.setText("Clear Selection");
        clearSelectionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearSelectionButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Elephant", 1, 12));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Monthly Payment:");

        monthlyPaymentText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                monthlyPaymentTextActionPerformed(evt);
            }
        });

        exitProgramButton.setBackground(new java.awt.Color(204, 51, 255));
        exitProgramButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        exitProgramButton.setText("Exit");
        exitProgramButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitProgramButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(loanAmountText, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(sevenYrsButton)
                        .addGap(18, 18, 18)
                        .addComponent(fifteenYrsButton)
                        .addGap(18, 18, 18)
                        .addComponent(thirtyYrsButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(55, 55, 55)
                        .addComponent(monthlyPaymentText, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(clearSelectionButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 220, Short.MAX_VALUE)
                .addComponent(exitProgramButton)
                .addGap(41, 41, 41))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(loanAmountText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sevenYrsButton)
                    .addComponent(fifteenYrsButton)
                    .addComponent(thirtyYrsButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(monthlyPaymentText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(52, 52, 52)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clearSelectionButton)
                    .addComponent(exitProgramButton))
                .addGap(28, 28, 28))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void monthlyPaymentTextActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
    }                                                  

    private void exitProgramButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        System.exit(0);
    }                                                 

    private void clearSelectionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        loanAmountText.setText("");
        monthlyPaymentText.setText("");
    }                                                    

    private void sevenYrsButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        double mortgageamount = Double.parseDouble(loanAmountText.getText());
        //Mortage amount variable = loanAmountText. This shows that the mortgage
        //amount is associated with the loanAmountText field.
        double rate = 5.35; //Shows that the interest rate for this button is 5.35
        int term = 7; //Shows that the term for this button is 7 years.
        double paymentAmount = (rate/100/12)/(1-Math.pow ((1+rate/100/12),(-term*12)))*mortgageamount;
        paymentAmount = Math.rint(paymentAmount*100)/100;//Formula to calculate the payment
        monthlyPaymentText.setText("Your Payment is: "+"$ "+paymentAmount);
        paymentcalculation(paymentAmount, term, rate, mortgageamount);//Associates the payment
        //amount text with the payment amount variable.
    }                                              

    private void fifteenYrsButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        double mortgageamount = Double.parseDouble(loanAmountText.getText());//Mortgage amount
        //variable = loanAmountText. This shows that the mortgage amount is associated
        //with the loanAmountText field
        double rate = 5.5; //Shows that the interest rate for this button is 5.5
        int term = 15; //Shows that the term for this button is 15 years
        double paymentAmount = (rate/100/12)/(1-Math.pow((1+rate/100/12),(-term*12)))*mortgageamount;
        paymentAmount = Math.rint(paymentAmount*100)/100; //Formula to calculate the payment
        monthlyPaymentText.setText("Your Payment is: "+"$ "+paymentAmount);
        paymentcalculation(paymentAmount, term, rate, mortgageamount);//Associates the payment
        //amount text with the payment amount variable.
    }                                                

    private void thirtyYrsButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        double mortgageamount = Double.parseDouble(loanAmountText.getText());//Mortgage amount
        //variable = loanAmountText. This shows that the mortgage amount is associated
        //with the loanAmountText field.
        double rate = 5.75; //Shows that the interest rate for this button is 5.75
        int term = 30; //Shows that the term for this button is 30 years
        double paymentAmount = (rate/100/12)/(1-Math.pow((1+rate/100/12),(-term*12)))*mortgageamount;
        paymentAmount = Math.rint(paymentAmount*100)/100;//Formula to calculate the payment
        monthlyPaymentText.setText("Your Payment is: "+"$ "+paymentAmount);
        paymentcalculation(paymentAmount, term, rate, mortgageamount);//Associates the payment
        //amount text with the payment amount variable.
    }                                               

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MortgageCalculatorWk3().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton clearSelectionButton;
    private javax.swing.JButton exitProgramButton;
    private javax.swing.JButton fifteenYrsButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField loanAmountText;
    private javax.swing.JTextField monthlyPaymentText;
    private javax.swing.JButton sevenYrsButton;
    private javax.swing.JButton thirtyYrsButton;
    // End of variables declaration                   

private void paymentcalculation(double payments, int terms, double rates, double initialBalance){
    //payment variables are set and initialized
    double balanceRemaining = payments*(terms*12);
    double paymentAmt = payments;
    double interests = (payments*(balanceRemaining/12));
    int numberOfPayments = terms*12;
    Object[] paymentLine = new Object[numberOfPayments];

        resultsList = new DefaultListModel();
        resultsList.addElement(" #    Interest Paid        Remaining Balance ");

        //A loop for the array to display in JTextArea
        for (int e=0; e<numberOfPayments; ++e){
            balanceRemaining = balanceRemaining - paymentAmt;
            interests = balanceRemaining*(rates/100/12);
            interests = Math.rint(interests*100)/100;
            paymentAmt = Math.rint(paymentAmt*100)/100;
            balanceRemaining = Math.rint(balanceRemaining*100)/100;
            paymentLine[e] = (e+1) + "        $" + interests + "        $" + balanceRemaining;
            resultsList.addElement(paymentLine[e]);
    }

        Schedule = new JList(resultsList);
        JScrollPane Scroll = new JScrollPane(Schedule);
        JFrame Jframe = new JFrame("Loan Schedule");

        Jframe.getContentPane().add(Scroll);
        Jframe.setSize(300, 800);
        Jframe.setVisible(true);
        
}
}