Start New Discussion within our Software Development Community

I need to override String toString method so it woud print my deposits
I dont't know how to override it, since in makeDeposit method I pass two variables
I'm posting just a saple of my code, I'm not sure if you need the whole code

public class Bank 
{
    private Account []a;
    private int count;

    public Bank (int n)//create constructor and initialize a and count 
    {
        if (n<0)
            n=20;
        a = new Account[n];//create array of account references
        count = 0;
    }
public boolean makeDeposit(int n, double amt)
    {
        boolean flag = false;
        if (count<a.length)
            for(int i=0; i<a.length && !flag; i++)
            {
                if (a[i] != null)
                    if (a[i].getId() == n)
                {
                    a[i].Deposit(amt);
                    flag = true;
                }
            }
        return flag;
    }

I tried this

public String toString()
    {
      return "Deposit "+this.makeDeposit()

    }

but of course I need to pass two variables with it

no. never pass any arguments to toString.
if you do that, you don't override the method, you overload it.

your proposal also makes no sense.
in the toString method, you can print the values for your class scope variables, and that is what you should do. don't call the makeDeposit method from within toString, but store the values you pass there as parameters in class instance variables.

later, call the toString method after you call the makeDeposit method. but I can't say I see lots of logic in it.

This question has already been answered. Start a new discussion instead.