如何制作每个不同机场的飞机列表?
在此示例中,我想创建一个机场,并在其特定对象(机场)时添加一个飞机到该机场的集合中。
例如,如何制作每个不同机场的飞机清单? 在这个示例中,我想创建一个机场,当它是这个特殊对象(机场)时,我想添加一架飞机到该机场的集合中。
例如:
public class Airport {
private Plane plane;
Queue<Plane> queueOfPlanes = new ArrayDeque<Plane>();
public Airport(Plane plane) {
this.plane = plane;
queueOfPlanes.add(plane);
}
我正在创建一个机场,当我有这个特定的机场时,我想将飞机收集到这个机场的队列中。
答案 0 :(得分:0)
有很多方法可以做到,但是我认为HashMaps
最适合您的情况,让我们来看一个例子。
HashMap<String, ArrayList<Plane>> mAirPorts = new HashMap<String, ArrayList<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());
}
}
现在只需将对象添加到您的HashMap中,如:
mAirPorts.put("airport_key", ArrayListContainingPlanes);
您现在可以通过机场钥匙来获得飞机,例如:
ArrayList<Plane> mPlanes = mAirPorts.get("airport_key");
if (mPlanes != null) {
...
} else {
//No such airport
}
答案 1 :(得分:0)
首先要为您的机场使用另一个界面。
赞:
private Plane plane; ...
public Airport(Plane plane) {
这已经错误。机场不需要特定的单架飞机即可成为机场。
宁可去
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); }
这里的想法是:机场具有不变的特定属性(例如其名称,位置等)。但是飞机来来去去。因此,您的机场对象需要一种存储与其当前相关联的飞机的方式。