I am creating a flight controller application. A bit of functionality that i want is to be able to tell the user what the next flight is according to a specific airline. I have a hash map which stores strings and planes. In my plane class i am implementing Comparable and i have the compareTo method. Could anyone help me achieve using the compareTo method to arrange the planes in descending order to show the next flight. I want to arrange the flights by the variable overdue.
This is the case in the MainApp that i have to use the compareTo on
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;
I hope to add the descending order to a airline print: airlineMap.printAirline(“Aer Lingus”);
Here is my Plane class:
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";
}
}
Since
HashMapis unordered, you have two ways of going about sorting your planes:ArrayList<Plane>or an arrayPlane[], and sort that list or arrayThe 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 theircompareTomethod).The second approach requires copying the planes into a separate container or an array, and then using the
sortmethod (or theArrays.sortstatic method if it is an array) to order your planes in accordance with the order set by theircompareToimplementation.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:
Once you add the planes to each airline, they would be maintained in the order based on your
compareToimplementation. with aTreeSet<Plane>in hand, you can easily find the next or the priorPlaneby callinghigherorlower.You should use
Math.signumrather thanMath.ceilin yourcompareTomethod.