我正在创建一个飞行控制器应用程序。我想要的一些功能是能够根据特定航空公司告诉用户下一班航班是什么。我有一个存储字符串和平面的哈希图。在我的飞机类中,我正在实现 Comparable 并且我有 compareTo 方法。谁能帮我实现使用 compareTo 方法按降序排列飞机以显示下一个航班。我想通过变量过期来安排航班。
这是我必须在 MainApp 上使用 compareTo 的情况
switch (nextChoice)
{
case 1:
airlineMap.printAirline("Aer Lingus");
break;
case 2:
airlineMap.printAirline("Brittish Airways");
break;
case 3:
airlineMap.printAirline("Eithad");
break;
case 4:
airlineMap.printAirline("Iberia");
break;
case 5:
airlineMap.printAirline("Quantas");
break;
我希望将降序添加到航空公司打印中:airlineMap.printAirline("Aer Lingus");
这是我的飞机课:
import java.util.LinkedList;
public class Plane implements Comparable
{
private String flightNumber;
public String airlineName;
private double fuelRemaining;
private int overdue;
private int passengerNumber;
private AIRPLANETYPE planeType;
public enum AIRPLANETYPE
{
AIRBUS("1"), CORPORATE("2"), PRIVATE("3");
private String planeName;
private AIRPLANETYPE(String planeName)
{
this.planeName = planeName;
}
public String getPlaneName()
{
return this.planeName;
}
}
public Plane(String flightNumber, String airlineName, double fuelRemaining, int overdue, int passengerNumber, AIRPLANETYPE planeType) {
this.flightNumber = flightNumber;
this.airlineName = airlineName;
this.fuelRemaining = fuelRemaining;
this.passengerNumber = passengerNumber;
this.overdue = overdue;
this.planeType = planeType;
}
public String getAirlineName() {
return airlineName;
}
public void setAirlineName(String airlineName) {
this.airlineName = airlineName;
}
public void setOverdue(int overdue) {
this.overdue = overdue;
}
public int getOverdue(){
return overdue;
}
public String getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
public double getFuelRemaining() {
return fuelRemaining;
}
public void setFuelRemaining(double fuelRemaining) {
this.fuelRemaining = fuelRemaining;
}
public int getPassengerNumber() {
return passengerNumber;
}
public void setPassengerNumber(int passengerNumber) {
this.passengerNumber = passengerNumber;
}
public AIRPLANETYPE getPlaneType() {
return planeType;
}
public void setPlaneType(AIRPLANETYPE planeType) {
this.planeType = planeType;
}
public int compareTo(Object arg0) {
if((arg0 != null) && (arg0 instanceof Plane))
{
Plane p = (Plane) arg0;
return (int)Math.ceil(this.overdue - p.getOverdue());
}
return 0;
}
public String toString() {
return "Plane: flightNumber=" + flightNumber + "."
+ " airlineName=" + airlineName + "."
+ " fuelRemaining=" + fuelRemaining + " litres."
+ " overdue=" + overdue + " minutes."
+ " passengerNumber="+ passengerNumber + "."
+ " airplaneType=" + planeType + ".\n";
}
}