SungKeun Hong
SungKeun Hong

Reputation: 11

JAVA adding total

I would like to have this function used inside of another function. what this "addTotal" function does is it returns the total balance of each bankAccount that a certain client has. I know by inserting the "return total;" statement, it only gets the first bankAccount balance only. Any suggestion on this one?

private int addTotal(ClientList clientList, String name)
    {
        for(int i = 0 ; i<clientList.getClientList().size();i++)
        {
            if(name.compareTo(clientList.getClientList().get(i).getName())==0)
            {
                for(int j =0;j<clientList.getClientList().get(i).getBankAccList().size();j++)
                {
                    int total = clientList.getClientList().get(i).getBankAccList().get(j).showBalance();
                    total+=total;   
                     return total; // i know here is the problem. any suggestion on this?
                }
            }
        }
        return -2;
    }

Upvotes: 0

Views: 2971

Answers (4)

Eran
Eran

Reputation: 394146

You should declare the total variable at the start of the method and return it at the end of the loops :

private int addTotal(ClientList clientList, String name)
{
    int total = 0;
    for(int i = 0 ; i<clientList.getClientList().size();i++) {
        if(name.compareTo(clientList.getClientList().get(i).getName())==0) {
            for(int j =0;j<clientList.getClientList().get(i).getBankAccList().size();j++) {
                total+=clientList.getClientList().get(i).getBankAccList().get(j).showBalance();
            }
        }
    }
    return total;
}

Edit :

you can make your code more readable by introducing a local variable :

private int addTotal(ClientList clientList, String name)
{
    int total = 0;
    for(int i = 0 ; i<clientList.getClientList().size();i++) {
        Client client = clientList.getClientList().get(i);
        if(name.compareTo(client.getName())==0) {
            for(int j =0;j<client.getBankAccList().size();j++) {
                total+=client.getBankAccList().get(j).showBalance();
            }
        }
    }
    return total;
}

or even better, by using the enhanced for loop (I made some guesses regarding the names of your classes) :

private int addTotal(ClientList clientList, String name)
{
    int total = 0;
    for(Client client : clientList.getClientList()) {
        if(name.compareTo(client.getName())==0) {
            for(BankAccount acct : client.getBankAccList()) {
                total+=acct.showBalance();
            }
        }
    }
    return total;
}

By using Java 8 Streams you can make it even shorter :

private int addTotal(ClientList clientList, String name)
{
    return clientList.getClientList()
                     .stream()
                     .filter(c -> name.compareTo(c.getName())==0)
                     .flatMap(c -> c.getBankAccList().stream())
                     .mapToDouble(BankAccount::showBalance)
                     .sum();
}

Upvotes: 6

Rajesh
Rajesh

Reputation: 2155

You need to take total declaration and return statement outside of inner for loop.

Replace you inner for loop with below lines:

client_BankAccList = clientList.getClientList().get(i).getBankAccList();
int total = 0;

for(int j = 0; j < client_BankAccList.size(); j++)
{
    total += client_BankAccList.get(j).showBalance();
}
return total;

Upvotes: 0

Eran
Eran

Reputation: 135

First of all, Eran solves your original problem. Your problem was that you didnt sum up the amounts, you did:

total = total;
return total;

My suggestion for you is when you have code like this:

clientList.getClientList().get(i).getName()

make a function in ClientList which gets an integer i and returns the name as follows:

public String getClientName(int i)

and instead having the long code above, all you have to use is:

clientList.getClientName(i)

Upvotes: 0

Tom Jonckheere
Tom Jonckheere

Reputation: 1648

This solutions returns the total outside of the loop. I don't know why you wanted to return the -2, but if it's still needed you can adjust the code.

 private int addTotal(ClientList clientList, String name)
                {
                    int total =0;
                    for(int i = 0 ; i<clientList.getClientList().size();i++)
                    {
                        if(name.compareTo(clientList.getClientList().get(i).getName())==0)
                        {
                            for(int j =0;j<clientList.getClientList().get(i).getBankAccList().size();j++)
                            {
                                total+= clientList.getClientList().get(i).getBankAccList().get(j).showBalance();suggesti
                            }
                        }
                    }
                    return total;
                }

Upvotes: 1

Related Questions