2

I'm programming in java for a class project. In my project I have airplanes, airports and passengers.

The passenger destination airport is randomly created, but then I have to add it to a List with passengers for that destination.

As long as the airports are read from a file thus they can vary, how can I create Lists according to these airports?

What I want to do is something like:

List<Passenger> passengersToJFK = new ArrayList<Passenger>();
.
.
.

if(passenger.destination == "JFK"){
   passengersToJFK.add(passenger);
}

The problem is that as I've said, the number and name of airports can vary, so how can I do a general expression that creates Lists according to the Airports File and then adds passengers to those Lists according to the passenger destination airport?

I can get the number of Airports read from the file and create the same number of Lists, but then how do I give different names to this Lists?

Thanks in advance

3
  • are you trying is to have multiple lists? a list per airplane may be? Commented Dec 7, 2012 at 17:14
  • I want to create new arrayLists according to the number of airports that I have. If I have 3 airports the program would create 3 lists. Then the passengers would be added to those lists according to their destination. Commented Dec 7, 2012 at 17:16
  • Ok, i'll be more specific about what I do have right now. I have 4 classes, Airport, Airplane, Passenger, and Board. The Airport class has a List of Passengers and a List of Airplanes. The Airplane class has a List of Passengers. The Passenger class has only it's origin and destination airports. The Board class is where everything else happens. Commented Dec 7, 2012 at 17:20

5 Answers 5

9

You can keep a registry of the associations between a destination or airport and a list of passengers with a Map, in a particular class that centers this passengers management.

Map<String,List<Passenger>> flights = new HashMap<String,List<Passenger>>();

Then, whenever you want to add a new destination you put a new empty list and

public void addDestination(String newDestination) {
    flights.put(newDestination, new ArrayList<Passenger>());
}

When you want to add a passenger, you obtain the passenger list based on the destination represented by a String.

public void addPassengerToDestination(String destination, Passenger passenger) {
    if(flights.containsKey(destination))
        flights.get(destination).add(passenger);        
}

I suggest you dig a little deeper into some particular multi-purpose Java classes, such as Lists, Maps and Sets.

Sign up to request clarification or add additional context in comments.

7 Comments

Beat me to it. You don't have to store "String" though, think of it as Map<Airport, Map<Airplane, Passenger>>
Rule of thumb: if you need to nest generic types, like Map<String,List<Passenger>> or something, it's time for a class. List<Passenger> should perhaps be Airport or something.
@BillK Perhaps - but then you need a lookup of Airport by callLetter or something.
@BillK Indeed, but I tought it would be a bit of an overkill. Also, coming up with a way to expand this simple concept to an Airport class it's going to be a great excercise for the OP :) (without to mention the incurson of hashcode and equals).
@corsiKa I started by suggesting a particular class to handle this, somehow I deleted that in the edit I made...
|
2

I would probably create a Map of airports with airport name as the key and a List of passengers as the value.

e.g.

Map<String, List<String>> airports = new HashMap<String, List<String>>();

airports.put("JFK", passengersToJFK);

Comments

0

You sound like you're thinking too much in terms of primitives, Strings, and collections and not enough in terms of objects.

Java's an object-oriented language; start thinking about Objects and encapsulation.

You've got a good start with your Passenger class. Keep going with Airport.

Do you add Passengers to Airport? Nope, I think they belong to a Flight.

Do a little thinking about your problem before you write more code.

Comments

0

You shouldn't focus on giving the actual variables of list objects unique names, but instead, create a map from String (destination id) to List (passengers heading to that destination), and add lists on the fly to that map, linking each new list to its relevant destination. Update the lists in that map as needed.

Comments

0

The best way to do it is to create objects for all three.

You might have an Airport object that looks like this:

    class Airport{
        String name;
        List Airplane airplanes;
    }

then you would have an airplane that looked like this:

    class Airplane{
        String name; // ?? or bodyType?  or whatever else you need
        List Passenger passengers;
    }

In this way you compose your objects from each other in a way that ends up being much easier to understand and deal with.

Note that I'm leaving off methods, like Airport probably has a method like "addAirplane" to add another airplane, and the airplane object has an addPassenger method...

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.