My goal is to search a multi-tree structure to see which item matches the name I am looking for. I am doing this by implementing an interface I made named SearchByName in every class I need to search in.
The way my data is contained is that I have an ArrayList called parties comprised of Party objects that each have a name in an object called the cave, each Party has an ArrayList called members comprised of Creature objects that each have a name, and each creature has an ArrayList called artifacts comprised of Artifact objects that each posses a name;
Every time I search the search returns null even when there should be a match.
This is my code for performing the search:
for ( Party p : SorcerersCave.theCave.parties ){
SearchableByName foundItem = p.searchByName( name );
if ( foundItem != null ) {
GenerateInterface.theGame.printOutput( "\t" + foundItem );
} else {
GenerateInterface.theGame.printOutput( "Item NOT FOUND" );
}
break;
}
This is the interface I am implementing:
interface SearchableByName {
public SearchableByName searchByName (String name );
public String getName();
}
this is the interface being implemented in Party:
public SearchableByName searchByName ( String n ) {
if ( getName() == n ) {
return this;
} else {
for ( Creature c : members ) {
SearchableByName found = c.searchByName( n );
if ( found != null ) {
return found;
}
}
}
return null;
}
This is the interface being implemented in Creature:
public SearchableByName searchByName ( String n ) {
if ( getName() == n ) {
return this;
} else {
for ( Artifact a : artifacts ) {
SearchableByName found = a.searchByName( n );
if ( found !=null ) {
return found;
}
}
}
return null;
}
And finally I implement the interface in Artifact:
public SearchableByName searchByName ( String n ) {
return ( getName() == n ) ? this : null;
}
This is my first time trying to searching into the classes rather than do everything at the top level.