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.
When you're doing BankAccounts[maxIndex].printReport();
, you've already swap the values. So you're printing the value in position maxIndex
, and there is already item that was at position i
at the beginning of current step.
So, you need to do BankAccounts[maxIndex].printReport();
before swap, or print the value from the right position — BankAccounts[i].printReport();
About maxValue
— it's updates each step, so if you need this after cycle ends, you can just get it as BankAccounts[0].getBalance()
after sort routine ends.
Moreover, if you need only to sort items, but you're not tied to use selection sort, I'd like to recommend Java built-in methods for sort, so your code should look like this:
Arrays.sort(BankAccounts, 0, BankAccount.getNumberOfAccounts(), new Comparator<BankAccount>() {
@Override
public int compare(BankAccount o1, BankAccount o2) {
if (o1.getBalance() > o2.getBalance()) return -1;
if (o1.getBalance() < o2.getBalance()) return 1;
return 0;
}
}
);
After this sort operation, array BankAccounts
is sorted in descending order of balances, and you can print reports just in a simple loop:
for (i = 0; i < BankAccount.getNumberOfAccounts(); i++) {
BankAccounts[i].printReport;
}