I'm currently working on a Bank Account program that takes user input and enters it into an array. It performs actions like deposit, withdraw, search, etc. I'm currently stuck utilizing a selection sort based on the balance amounts in each account. The program should sort the accounts based on balance from highest to lowest and print the results to the screen.
This is my first time using selection sort and one of my first few times using arrays. I understand the concept of selection sort and how to do it with primitive values, but translating that to object values is stumping me. Below is the code I have for the selection sort.
if (out.equals("Sort")) {
int i, j, maxIndex;
double maxValue;
//getNumberOfAccounts is a static counter incremented each time
//a new bank account is created
for (i = 0; i < BankAccount.getNumberOfAccounts(); i++) {
//Sets first value as largest
maxValue = BankAccounts[i].getBalance();
maxIndex = i; //Index of first value
for (j = i; j == BankAccount.getNumberOfAccounts(); j++) {
//Compares subsequent values to initial max value
if (BankAccounts[j].getBalance() > maxValue) {
maxValue = BankAccounts[j].getBalance();
maxIndex = j;
}
}
//Attempts to swap values
BankAccount temp = BankAccounts[i];
BankAccounts[i] = BankAccounts[maxIndex];
BankAccounts[maxIndex] = temp;
//Outputs Bank Account data in descending order based on balance
BankAccounts[maxIndex].printReport();
}
}
Notes:
-This is a portion of the full program so if I'm missing a bracket it's just because I didn't copy the whole thing.
-It seems that when I run the program it does not store the maxValue; the output of maxValue instead outputs whichever value of the loop iteration is next.
-When I run the program it simply prints the bank accounts in the exact order I enter them.
Thank you in advance and if there's any more information I can provide I gladly will.