3

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.

2
  • 1
    Thanks for the input I removed the first sentence. Commented Nov 29, 2015 at 17:01
  • 1
    Pretty good first post, clear question, with code. Commented Nov 29, 2015 at 17:02

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input. Neither moving the printReport() method nor changing the indexed variable changed my output unfortunately. The sort method you mentioned however actually worked perfectly. I was hoping to use selection sort but my professor said we could also use the sort method. We just haven't covered the sort method in class yet so I don't fully understand the code.
1

First you need to change this line

for (j = i; j == BankAccount.getNumberOfAccounts(); j++) {

to

for (j = i; j < BankAccount.getNumberOfAccounts(); j++) {

The way you have it now you're saying "loop until j is equal to BankAccount.getNumberOfAccounts()" and that’s never gonna happen in this code.

Another performance issue that you have.

BankAccount temp = BankAccounts[i];
BankAccounts[i] = BankAccounts[maxIndex];
BankAccounts[maxIndex] = temp;

Imagen that the current index is already in the right spot. You will make an unnecessary "switch".

For example: current intex ( i == 5 ) biggest balance position ( maxIndex == 5 ).

You will have:

BankAccount temp = BankAccounts[5];
BankAccounts[5] = BankAccounts[5];
BankAccounts[5] = temp;

So you can change this part to this:

if(i != maxIndex) {
    //Attempts to swap values
    BankAccount temp = BankAccounts[i];
    BankAccounts[i] = BankAccounts[maxIndex];
    BankAccounts[maxIndex] = temp;
}

3 Comments

Thanks for the response. I initially had j < BankAccount.getNumberOfAccounts() but it would skip an input value so I changed it to ==. getNumberOfAccounts is a static int so it actually does eventually evaluate to false. It's basically a counter for the number of bank accounts, not to be confused with the length of the array.
Can you please put the implementation of getNumberOfAccounts() in your question ?
Sorry I didn't include it before. I found the solution to this problem. But earlier in the program I create bank accounts with a constructor, and within the constructor method numberOfAccounts increments by 1 each time an account is created. getNumberOfAccounts just calls that number. I used it because there are empty spots in the array and I (think) it was the only way to avoid null pointer exceptions.
1

After following a couple of old related threads I stumbled upon a thread that perfectly answered my question. While the Array.sort method mentioned above worked perfectly, I was uncomfortable using it simply because I have not yet learned about it.

            if (out.equals("Sort")){


            for (int i = 0; i < BankAccount.getNumberOfAccounts(); i++){
                for(int j = i+1; j < BankAccount.getNumberOfAccounts(); j++){
                    if(BankAccounts[j].getBalance() > BankAccounts[i].getBalance()){
                        BankAccount [] temp = new BankAccount [BankAccounts.length];
                        temp [j] = BankAccounts [j];
                        BankAccounts [j] = BankAccounts [i];
                        BankAccounts [i] = temp [j]; 
                    }
                }
            }

            for (int i = 0; i < BankAccount.getNumberOfAccounts(); i++){
                BankAccounts[i].printReport();
                System.out.println();
            }

Here's the code that printed the results I've been looking for. I realized my comparison in the inner loop was off and I wasn't creating a new array for the swap, only new objects. Thanks for all the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.