2 回答
TA贡献1830条经验 获得超3个赞
您首先为您的机场拥有一个不同的界面。
喜欢:
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); }
这里的想法是:机场具有不会改变的特定属性(例如它的名称、位置等)。但是飞机来来去去。因此,您的机场对象需要一种方法来存储当前与其关联的飞机。
TA贡献1794条经验 获得超7个赞
有很多方法可以做到,但我认为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
}
添加回答
举报