Monday, December 17, 2012

Calculate Car Loan

When run on my linux system

Here's a simple script for calculating a car loan using java:
package calculateloan;
public class displayFrame extends javax.swing.JFrame {
    //set up variables
    public float interest;
    public float price;
    public float dpayment;
    public int num_months;
    public float results;
    public float results1;
    public double cubed;
    /**
     * Creates new form displayFrame
     */
    public displayFrame() {
        initComponents();
    }
   
     ..........
                                 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
       
        // set default credit rating
        interest = 0.15f;
        //find out credit rating
        if (jRadioButton1.isSelected()){
            interest = 0.05f;
        }
        if (jRadioButton2.isSelected()){
            interest = 0.10f;
        }
        if (jRadioButton3.isSelected()){
            interest = 0.15f;
        }
        //find number of months
        if (jComboBox1.getSelectedItem() == "5 years"){
            num_months = 60;
        }
        if (jComboBox1.getSelectedItem() == "10 years"){
            num_months = 120;
        }
        if (jTextField1.getText().isEmpty()){
            //set default value for price
            price = 15000.0f;
        }
        if (jTextField1.getText().length() != 0){
            //set price from fields if not null
            price = Integer.parseInt(jTextField1.getText());
        }
        if (jTextField2.getText().isEmpty()){
            //set default down payment
            dpayment = 0.0f;
        }
        if (jTextField2.getText().length() != 0){
            //set down payment if field not null
            dpayment = Integer.parseInt(jTextField2.getText());
        }
       
        //Print lines to test variable assignment
        //System.out.println(price);
        //System.out.println(dpayment);
        //System.out.println(num_months);
        //System.out.println(interest);
       
        //at this point fields are set. we can calculate payment
        //the calculation is (payment(rate/12))/(1-(1+rate/12)^-num_months)
        //i broke it down into several steps
        results = interest/12;
        results = price * results;
        results1 = interest/12;
        results1 += 1;
        num_months = -num_months;
        cubed = java.lang.Math.pow(results1, num_months);
        cubed = 1-cubed;
        cubed = results/cubed;
        //cubed is now a double with way too many decimal places
        //let's do a bit of math to round it to the nearest tenth
        //thanks to Obvius for this bit
        int tempInteger;
        cubed *= 100;
        cubed += (double).5;
        tempInteger = (int)cubed;
        cubed = (double)tempInteger;
        cubed /= 100;
        jLabel7.setText("Results: " + cubed);
       
       
    }                                       
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        float[] anArray;
        anArray = new float[3];
        anArray[0] = 0.05f;
        anArray[1] = 0.10f;
        anArray[2] = 0.15f;
       
        .........
       
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new displayFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    .......           
}
Where ever you see "......." I've omitted some of the garbage netbeans adds. Obviously this is a snippet from a jframe project and the important bits are in tact. I set up a number of fields, dropdowns, etc, and a button that takes all of it to calculate the loan.
You should be able to do the same thing fairly easy using the design function within netbeans or a similar IDE.
-Newt

No comments:

Post a Comment