-1

How to make for example list of Plane per each different airport?

I would like to create in this example the airport and when its this particular object(airport) , I would like to add a plane to collection of this airport.

How to make for example list of Plane per each diffrent airport? I would like to create in this example the airport and when its this particual object(airport) i would like to add a plane to collection of this airport.

For example:

public class Airport {
    private Plane plane;
    Queue<Plane> queueOfPlanes = new ArrayDeque<Plane>();

    public Airport(Plane plane) {
        this.plane = plane;
        queueOfPlanes.add(plane);
    }

I am creating an airport, and when I have this specific airport I would like to gather the plane in the Queue for this one airport.

Wendjors
  • 15
  • 2
  • 2
    Can you present your model, wanted output and what you have tried till now? You want us to model the data, or find a solution to group airplanes to airports? – Beri Oct 23 '18 at 07:50
  • Hello, can you show what you have tried ? Perhaps [this](https://stackoverflow.com/a/46908/8810495) can help you too – octano Oct 23 '18 at 07:54
  • I add an example – Wendjors Oct 23 '18 at 08:05

2 Answers2

0

There are many ways to do it but I think HashMaps are the best for your scenario, Let's see an example.

HashMap<String, ArrayList<Plane>> mAirPorts = new HashMap<String, ArrayList<Plane>>();

Now you need to create Object Plane

public class Plane 
{
    private double maxWeight;
    private double emptyWeight;
    private double loadWeight;
    private double travelSpeed;
    private double flyHours;
    private double consumption;
    private double maxFuel;
    private double kerosinStorage;

    public Plane( double maxWeight, double emptyWeight, double loadWeight,
                  double travelSpeed, double flyHours, double consumption,
                  double maxFuel, double kerosinStorage )
    {
        this.maxWeight      = maxWeight;
        this.emptyWeight    = emptyWeight;
        this.loadWeight     = loadWeight;
        this.travelSpeed    = travelSpeed;
        this.flyHours       = flyHours;
        this.consumption    = consumption;
        this.maxFuel        = maxFuel;
        this.kerosinStorage = kerosinStorage < this.maxFuel
                                ? kerosinStorage
                                : this.maxFuel;
    }

    public double getMaxWeight()
    {
        return maxWeight;
    }

    public double getEmptyWeight()
    {
        return emptyWeight;
    }

    public double getLoadWeight()
    {
        return loadWeight;
    }

    public double getTravelSpeed()
    {
        return travelSpeed;
    }

    public double getFlyHours()
    {
        return flyHours;
    }

    public double getConsumption()
    {
        return consumption;
    }

    public double getMaxFuel()
    {
        return maxFuel;
    }

    public double getKerosinStorage()
    {
        return kerosinStorage;
    }

    public void setMaxWeight(double maxWeight)
    {
        this.maxWeight = maxWeight;
    }

    public void setEmptyWeight(double emptyWeight)
    {
        this.emptyWeight = emptyWeight;
    }

    public void setLoadWeight(double loadWeight)
    {
        this.loadWeight = loadWeight;
    }

    public void setTravelSpeed(double travelSpeed)
    {
        this.travelSpeed = travelSpeed;
    }

    public void setFlyHours(double flyHours)
    {
        this.flyHours = flyHours;
    }

    public void setConsumption(double consumption)
    {
        this.consumption = consumption;
    }

    public void setMaxFuel(double maxFuel)
    {
        this.maxFuel = maxFuel;
    }

    public void setKerosinStorage(double kerosinStorage) 
    {
        this.kerosinStorage = this.kerosinStorage + kerosinStorage > maxFuel
                ? maxFuel : this.kerosinStorage + kerosinStorage;
    }

    /*
        Returns the total weight of the plane. Which is: emptyWeight + 
            weight of load + weight of kerosin. 
            Expect 1 liter Kerosin as 0.8 kg.        
    */
    public double getTotalWeight () 
    {
        return emptyWeight + loadWeight
                + (kerosinStorage * 0.8);
    }

    /*
        How far can the plane fly with the current kerosin storage?        
    */

    public double getMaxReach () 
    {        
        return (kerosinStorage / consumption) * travelSpeed;
    }

    /*
        Prevent flying further then possible (with the current kerosin) !
    */
    public boolean fly (double km) 
    {
        if (km <= 0 || getMaxReach() < km || getTotalWeight() > maxWeight)
        {
            return false;
        } 

        flyHours += (km / travelSpeed);
        kerosinStorage -= (km / travelSpeed) * consumption;

        return true;
    }

    /*
        ! The parameter 'liter' can be a negative number.
        Doesn't have to be overfilled.
        Prevent a negative number as value of the 'kerosinStorage' property !
    */
    public void fillUp (double liter) 
    { 
        if ((kerosinStorage + liter) > maxFuel)
        {
            kerosinStorage = maxFuel;
        }
        else if ((kerosinStorage + liter) < 0)
        {
            kerosinStorage = 0;
        }
        else
        {
            kerosinStorage += liter;
        }
    }

    /*
        Prevent illogical value-assignments !
    */
    public boolean load (double kg) 
    {

        if ((loadWeight + emptyWeight + kg) > maxWeight)
        {
            return false;
        }
        else if ((emptyWeight + kg) < 0)
        {
            loadWeight = 0;
            return true;
        }
        else
        {
            loadWeight += kg;
            return true;
        }
    }

    // Display flying hours, kerosin storage & total weight on t. terminal.
    public void info () 
    {
        System.out.println("Flying hours: " + flyHours + ", Kerosin: "
                + kerosinStorage + ", Weight: " + getTotalWeight());
    }
}

Now simply add objects to your HashMap like:

mAirPorts.put("airport_key", ArrayListContainingPlanes);

You can now get planes by your airport key like:

ArrayList<Plane> mPlanes = mAirPorts.get("airport_key");
if (mPlanes != null) {
    ...
} else {
    //No such airport
}
Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16
0

You start by having a different interface for your Airport.

Like:

private Plane plane; ...
public Airport(Plane plane) {

That is already wrong. An Airport doesn't need a specific single plane to be an airport.

Rather go:

class Airport {
  private final List<Plane> currentPlanes = new ArrayList<>();
  private final String name;

  public Airport(String name) { 
    this.name = name;
  }

  public void addPlane(Plane plane) { currentPlanes.add(plane); }
  public void removePlane(Plane plane) { currentPlanes.remove(plane); }

The idea here: an Airport has specific properties that don't change (like its name, location, ...). But the planes come and go. So your airport objects need a way to store which planes are currently associated to it.

GhostCat
  • 137,827
  • 25
  • 176
  • 248