Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:07:23+00:00 2026-06-16T01:07:23+00:00

I am creating a flight controller application. A bit of functionality that i want

  • 0

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";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:07:24+00:00Added an answer on June 16, 2026 at 1:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating an application that requires BOTH gesture(swipe) support as well as simple touch events.
creating a very simple scheduling app I am asking the user to tell me
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a google map with store locations within 50 miles of user entered address.
I creating a web application using JSF,Hibernate,Spring. I have added a filter for checking
I'm creating a system that uses two web services and a client to provide
I am creating a survey form that needs to have each question and set
so I am creating an oracle database for my university coursework on an airline.
So what im doing is creating a form that bitmaps move around and when
Creating an Iphone application, I used to perform INSERT QUERY on different databases. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.