0

I try to print the list of accounts but they appear as @164552... I tried to call the toString method like Arrays.toString(accounts) but it doesnt compile.

It complains about static and the that it can not resolve method toString

import java.util.ArrayList;
import java.util.Arrays;

public class Customer {
    private String name;
    private String surname;
    private String personalNumber;
    private ArrayList<Account> accounts;

    public Customer(String customerName, String customerSurname, 
                    String customerPersonalNumber) {
        this.name = customerName;
        this.surname = customerSurname;
        this.personalNumber = customerPersonalNumber;
        this.accounts = new ArrayList<Account>();
    }

    public Customer() {
    }

    public String getCustomerInfo() {
        return name + " " + surname + " " + personalNumber + accounts;
    }

    public ArrayList<Account> getAllAccounts() {
        return accounts;
    }

    public static void main(String[] arg) {
        Customer p = new Customer("Anna", "Larsson", "112323");
        p.addAccounts(new SavingsAccount());
        p.addAccounts(new SavingsAccount());
        System.out.println(Arrays.toString(accounts));
    }
}
2
  • 3
    1. You have no arrays in your code (beside arg). 2. Arrays.toString takes an array, not an ArrayList. 3. If you override Object.toString in Account, you would be able to see meaningful information by just printing using System.out.println(accounts); Commented Feb 20, 2020 at 14:35
  • Arrays.toString Commented Feb 20, 2020 at 14:35

1 Answer 1

1

You need to implement the toString method in your Account class, then you can do :

System.out.println(accounts);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.