-5
BankAccount b0, b1, b2, b3;
    b1=new BankAccount();
    b2=new BankAccount();
    b3=new BankAccount();
    for (int i=0; i<3; i++)
    {
        if (i==0)
            b0=b1;
        else if (i==1)
            b0=b2;
        else
            b0=b3;

and (what you just saw was part of the demo, below is part of the class stuff)

public String toString(double deposit, double withdraw, double fee, double total) {
    String str=name+"'s bank account statement:" + "\nInitial balance: " + balance + 
        "\nDeposit amount: " + deposit + "\nWithdraw amount: " + withdraw + "\nNumber of transactions: "
        + transac + "\nTotal bank fees: " + fee + "Final monthly balance: " + total; 
        return str;

and (demo, I haven't included all the code, just because this is still an open assignment.

System.out.println(b0); 

I really have no clue why it's not printing the string stuff :( all of the class (will remove later)

public class BankAccount {
private String name;
private double balance;
private int transac;

public BankAccount() {
    balance=0;
}
public void setName(String accountName) {
    name=accountName;
}
public void setBalance(double accountBalance) {
    balance=accountBalance;
}
public void setTransac(int accountTransac) {
    transac=accountTransac;
}
public String getName() {
    return name;
}
public double getBalance() {
    return balance;
}
public int getTransac() {
    return transac;
}
public double getDeposit(double deposit) {
    return balance+deposit;
}
public double getWithdraw(double deposit, double withdraw) {
    return balance+deposit-withdraw;
}
public double getFee(double fee, int accountTransac, double deposit, double withdraw) {
    fee=10;
    if (transac<20)
        fee+=transac*0.5;
    else if (20<=transac && accountTransac<40)
        fee+=transac*0.25;
    else if (40<=transac && transac<60)
        fee+=transac*0.2;
    else
        fee+=transac*0.1;
    if (balance+deposit-withdraw<400)
        fee+=15;
    return fee;
}
public double finalBalance(double fee, int accountTransac, double deposit, double withdraw) {
    double total=balance+deposit-withdraw-fee;
    return total;
}
public String toString(double deposit, double withdraw, double fee, double total) {
    toString(this.deposit, this.withdraw, this.fee, this.total);
    String str=name+"'s bank account statement:" + "\nInitial balance: " + balance + 
        "\nDeposit amount: " + deposit + "\nWithdraw amount: " + withdraw + "\nNumber of transactions: "
        + transac + "\nTotal bank fees: " + fee + "Final monthly balance: " + total; 
        return str;

}

}

Froblinkin
  • 374
  • 3
  • 14

3 Answers3

3

you should override public String toString() method (without arguments):

if you already have method public String toString(double deposit, double withdraw, double fee, double total) then use this:

class BankAccount {

 public String toString() {
  toString(this.deposit, this.withdraw, this.fee, this.total);
}
/* .. */
zacheusz
  • 8,750
  • 3
  • 36
  • 60
  • so the problem I had with my class section was that when I used fee, total, withdraw, and deposit without args, it couldn't find the symbol also I don't really understand the this.deposit function thingy :O – Froblinkin Jun 23 '13 at 20:26
  • @user2514022 could you post the whole class source? – zacheusz Jun 23 '13 at 20:28
1

You've overloaded the toString() method, providing a sibling with a different set of arguments.

You want to override the toString() method without arguments.

For example, if all the arguments to your other toString() method were member fields:

public String toString() {
    String str=name+"'s bank account statement:" + "\nInitial balance: " + balance + 
        "\nDeposit amount: " + deposit + "\nWithdraw amount: " + withdraw + "\nNumber of transactions: "
        + transac + "\nTotal bank fees: " + fee + "Final monthly balance: " + total; 
    return str;
}
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • the problem is deposit/withdraw/fee/total aren't defined as private fields in the class so java can't find them for me – Froblinkin Jun 23 '13 at 20:31
  • You may be trying to represent more than one object in the same class. It has an initial balance and a final balance; these can be class members. But an account doesn't have exactly one deposit or withdrawal -- it has been affected by a set of those. – Andy Thomas Jun 23 '13 at 20:39
0

The piece of information that the other answers are missing that's important is that when you call System.out.println(b0) the java compiler is automatically inserting a call to .toString() - it's part of the language definition explained here.

Therefore, as said elsewhere, to print out an object you need to override this method.

selig
  • 4,834
  • 1
  • 20
  • 37