0

I'm trying to write a program that will allow the user to create "loan" objects and measure the total amount that they have to pay after putting it through an interest equation. I'm having two problems with it that i've been messing with for a few hours but can't figure out for some reason. The biggest problem is that the class that is supposed to calculate the total payment of the loan after plugging in variables, but it ALWAYS returns zero and I can't figure out why. Is it a problem with syntax? I created a test loan class to test out the program that creates ten random loans with ten random time lengths and both the "totalPay" and the "monthlyPay" (which is dependent on totalPay) is always zero.

The second problem is that I was trying to sort the ten random loans based on the numberOfYears of the loan, but I can't get it to work! I tried a few methods for sorting and the one I included is the most recent and probably the one that's the dumbest hahaha but it's the one that I was playing with most recently. I've included all the code below, any ideas?

Loan class: (it's a little long but the important thing is just the main loan class, totalPayment, and monthlyPayment )

public class Loan {

double annualInterestRate = 2.5;
int numberOfYears = 1;
double loanAmount = 1000;
double base;
double exponent;
double totalPay;
String summary;
double monthly;
String[] list = new String[10];
String s;

/**
 * creates default loan with 2.5 annual interest rate 1 year and 1000$
 */
public Loan(){
}



/**
 *       totalPay = amount(1+rate/12)^(year*12)
 * 
 * @param anualInterestRate
 * @param numberOfYears
 * @param loanAmount
 */
public Loan(double anualInterestRate, int numberOfYears, double loanAmount){

    base = (double) ( loanAmount * (1+anualInterestRate/12));
    exponent = (double) (numberOfYears * 12);
    totalPay = (double) Math.pow(base, exponent); 
}   

/**
 * 
 * @return total payment
 */
public double totalPayment(){
    return this.totalPay;
}


/**
 * 
 * @return Monthly Payment
 */
public double monthlyPayment(){
    monthly = (totalPay/12);
    return monthly;
}


}

TestLoan:

import java.util.Random;


public class TestLoan {

public static void main (String[] args){

    Random r = new Random();


    Loan[] list = new Loan[10];


    for (int i = 0; i < 10; i++){
        Loan x = new Loan();
        list[i] = x;
        System.out.println(list[i].toString());
    }

    System.out.println();
    System.out.println();

    for (int i = 0; i < 9; i++){
        list[i].setNumberOfYears(r.nextInt(30));
        if (list[i].getNumberOfYears() > list[i+1].getNumberOfYears())
            list[i] = list[i+1];
        System.out.println(list[i].toString());
        }


}
}

Thank you for looking!

Edit got rid of the irrelevant code

  • 1
    Please edit your question to remove all code not absolutely essential for answering the question (ie most of it). See [SSCCE](http://sscce.org) – Bohemian Apr 26 '14 at 02:30
  • Here's [a question/answer](http://stackoverflow.com/a/14863539/778118) that explains how to sort arrays of objects. Here's [another one](http://stackoverflow.com/a/17116644/778118)... – jahroy Apr 26 '14 at 02:33
  • One question at a time thanks. – user207421 Apr 26 '14 at 03:03

1 Answers1

1

You set the number of years, but you never calculate the base, exponent or totalPay. That only happens in a constructor you never call.

What you need to do is move functional code out of the constructor and into a calculation method. That constructor should be for constructing the object.

old constructor:

public Loan(double anualInterestRate, int numberOfYears, double loanAmount) {
    base = (double) (loanAmount * (1 + anualInterestRate / 12));
    exponent = (double) (numberOfYears * 12);
    totalPay = (double) Math.pow(base, exponent);
}

better way:

public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
    this.annualInterestRate = annualInterestRate;
    this.numberOfYears = numberOfYears;
    this.loanAmount = loanAmount;
}

public void calculatePay() {
    base = (double) (loanAmount * (1 + annualInterestRate / 12));
    exponent = (double) (numberOfYears * 12);
    totalPay = (double) Math.pow(base, exponent);
}

Also, your method for sorting is flawed. You should decide what a good natural ordering for Loan is, then implement the Comparable interface.

Then you could use an existing sort library like this:

    for (int i = 0; i < 9; i++) {
        list[i].setNumberOfYears(r.nextInt(30));
        list[i].calculatePay();
    }
    Arrays.sort(list);
    for (Loan loan: list) {
        System.out.println(loan);
    }
azurefrog
  • 10,785
  • 7
  • 42
  • 56