Reputation: 125
I cannot see why this code is giving me the error cannot find method print everything looks okay to me, and I've been over the code checking for spelling errors and tried a different version of it that basically did the same thing but instead of things being called account they were called student. Ignore the comments as I have not changed them
import java.util.*;
public class AccountList
{
private ArrayList < Account > accounts;
/**
* Create a LabClass with no limit on number of enrolments.
* All other details are set to default values.
*/
public AccountList()
{
accounts = new ArrayList < Account >();
}
/**
* Add a account to this LabClass.
*/
public void addAccount(Account newAccount)
{
accounts.add(newAccount);
}
/**
* Return the number of accounts currently enrolled in this LabClass.
*/
public int getNumberOfAccounts()
{
return accounts.size();
}
/**
* Print out a class list with other LabClass
* details to the standard terminal.
*
* Method uses a for .. each loop
*/
public void getAllAccounts()
{
for(Account account : accounts)
{
**account.print();**
}
System.out.println("Number of accounts: " + getNumberOfAccounts());
}
/**
* Print out details of a account
* @param accountEntry The entry in the list
*/
public void getAccount(int accountEntry)
{
if(accountEntry < 0)
{
System.out.println("Negative entry :" + accountEntry);
}
else if(accountEntry < getNumberOfAccounts())
{
Account account = accounts.get(accountEntry);
System.out.print(account);
}
else
{
System.out.println("No such entry :" + accountEntry);
}
}
/**
* removes a account from the list
* @param accountEntry The entry in the list
*/
public void removeAccount(int accountEntry)
{
if(accountEntry < 0)
{
System.out.println("Negative entry :" + accountEntry);
}
else if(accountEntry < getNumberOfAccounts())
{
accounts.remove(accountEntry);
}
else
{
System.out.println("No such entry :" + accountEntry);
}
}
/**
* removes a account from the list
*
* @param aAccount the account to remove
*/
public void removeAccount(Account aAccount)
{
accounts.remove(aAccount);
}
}
Upvotes: 0
Views: 3121
Reputation: 121712
You seem to be missing some fundamental concepts here.
First: you have no "default" .print()
method; that is, the base, bare-bones Java class Object
has no .print()
method.
Second: even if it had, what do you expect it to do anyway? Where do you want it to print, what do you want it to print? The first question (where) is answered by classes dedicated to performing output duties (a PrintStream
for instance), the second question (what) is answered by implementing your class' .toString()
.
Since you Account
class is obviously not a class dedicated to output duties, you need to do two things:
Account
class override .toString()
;System.out
, which happens to be a PrintStream
, which implements a .print()
method.Seeing your code it appears that you have a .printAccountDetails()
method; this contradicts the Law of Demeter for starters; and note how it uses System.out
.
Also, the difference between .print()
and .println()
in a PrintStream
(which System.out
is) is that a newline will be appended to the output if you use the "ln version"; custom has it that for most text based output channels, this also triggers an output flush from the underlying OS libraries.
Upvotes: 2