Can someone please reply before Tuesday..........I know it is late but cos I had login problems and it only just sorted now.

I am having problems with add traveller into Resort class. I can add traveller fine, but I don't know how to add traveller on home world. I am using Arraylist. Traveller class is abstract class.

private ArrayList<Traveller> travellers = new ArrayList<Traveller>();
private World home = new World("Home", 0, 100);

public void addTraveller(Traveller tr)
{
       travellers.add(tr);
}

Also I have problems with finding world that specified traveller is on. It should return world which contains the traveller. I have tried two different code, I bet I done it all wrong. In World class there is traveller ArrayList (private ArrayList<Traveller> traveller = new ArrayList<Traveller>(); ).

private ArrayList<Traveller> travellers = new ArrayList<Traveller>();
private ArrayList<World> worlds = new ArrayList<World>();

public World findTraveller(Traveller tr)
     {
         for(World temp : worlds)
         {
             if(temp.getTraveller() == tr)
             {
                 return temp;
             }
         }
         return null;
     }

     public World findTraveller2(Traveller tr)
     {
         int index = worlds.size();
         return worlds.get(index);
     }

If you can help me before Tuesday I would be very very thankful!!

I know you can't create objects from abstract class.

There got to be way cos they can't give me impossible task.

Traveller class must be abstract.

You must create Traveller collection in Manager.

Method header must be like that cos it was given to me with empty body.

You need to examine the "World" class to see if there is a public method to add a traveler to that world. Since each world contains a private List of travellers, there must be some method to add one to the collection. You'd then just call home.addTraveller(t); To find a traveller, based on your current code that iterates the worlds, take a look at the indexOf() method.

Thanks for that Ezzaral. Now add traveller finally works!!

Now gotta work out how to use indexOf() cos I never used it before. Can you give me example like how it works?

public World findTraveller(Traveller tr)
    {
        for(World temp : worlds)
        {
            return worlds.indexOf(tr);
        }
        return null;
    }
public World findTraveller(Traveller tr)
    {
        for(World temp : worlds)
        {
            worlds.indexOf(tr);
            return World;
        }
        return null;
    }

I don't know how to use indexOf to make it work. :S That java documatation isn't helping cos there no examples.

Read the API link that I posted. It returns the index of the first occurrence of an object in the list or -1 if it doesn't exist. Think about how that would be used with your code that examines each world. There is also a contains() method for ArrayList which just returns true or false, not the index.

public World findTraveller(Traveller tr)
     {
         for(World temp : worlds)
         {
             worlds.indexOf(tr);
             return temp;
         }
         return null;
     }

That code compiles but still doesn't work. Hmmmmm...............I defo need example of this cos I don't know how to. API is way complicated to understand.

You need to search each world's traveller list, not the worlds list itself, which is what you are currently doing.
This means that there needs to be a method on the World class itself, like find(Traveller tr) or contains(Traveller tr) which finds or dertermines if that traveller exists in the travellers list. I don't know if you are supposed to write that method for the World class yourself or if there is already a method for that, since you didn't post the code of the World class.

Additionally, if you intend to use either indexOf() or contains(), you need to examine the return value to see if if that traveller was in the list. That means there needs to be an if() statement to determine the result. You can't just call indexOf() by itself and expect the program to know what to do.

I have to search worlds list cos it won't search on travellers list. Besides travellers list don't have name of worlds, just travellers. There is no way for me to get code working cos I have no idea. Way to advance for me.

In your first post, you say

In World class there is traveller ArrayList (private ArrayList<Traveller> traveller = new ArrayList<Traveller>(); ).

That means you somehow need to search the "traveller" list on each world. Since the list is private, the World class needs to have a method to let you determine if a particular traveller is in that list or a method to get the list itself so you can search it. One way or another, you have to search the traveller list for each world. Searching the "worlds" list itself won't help at all because that list only contains World objects - not Traveller objects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.