1

我正在创建一个飞行控制器应用程序。我想要的一些功能是能够根据特定航空公司告诉用户下一班航班是什么。我有一个存储字符串和平面的哈希图。在我的飞机类中,我正在实现 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";
    }
}
4

1 回答 1

2

Since HashMap is unordered, you have two ways of going about sorting your planes:

  • Put them into a sorted container, or
  • Put them into an ArrayList<Plane> or an array Plane[], and sort that list or array

The first approach can be achieved with a TreeSet<Plane>: put your planes into the set, and iterate them in the "natural" order (i.e. the order consistent with their compareTo method).

The second approach requires copying the planes into a separate container or an array, and then using the sort method (or the Arrays.sort static method if it is an array) to order your planes in accordance with the order set by their compareTo implementation.

EDIT : (based on a comment) One way to deal with a problem of storing planes in a specific order inside a hash map is to make a hash map of tree sets, like this:

Map<String,TreeSet<Plane>> airlineMap = new HashMap<String,TreeSet<Plane>>();

Once you add the planes to each airline, they would be maintained in the order based on your compareTo implementation. with a TreeSet<Plane> in hand, you can easily find the next or the prior Plane by calling higher or lower.

You should use Math.signum rather than Math.ceil in your compareTo method.

于 2012-12-13T00:16:33.857 回答