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