/* * @topic T02415 Assignment a8 -- Airline Flight Schedule demo * @brief Flight Schedule which can produce sorted ArrayList of departures */ package airlines; import java.util.HashMap; import java.util.Map; import java.util.ArrayList; import java.util.Collections; public class FlightSchedule { HashMap< String, Flight > flightMap; // constructors public FlightSchedule() { flightMap = new HashMap< String, Flight >(); } // operations public void addFlight( String airlineCode, int flightNumber ) { String externalflightNumber = airlineCode + flightNumber; // "DL1234" -> flight: flightMap.put( externalflightNumber, //DL1234 new Flight( airlineCode, flightNumber) // Flight object ); } /* * Returns true if flight number is not already on file: */ public boolean isExistingFlightNumber( String airlineCode, int flightNumber ) { String externalflightNumber = airlineCode + flightNumber; if ( flightMap.containsKey( externalflightNumber ) ) { // the flight is already on file, so it is no // longer a valid combination: return true; } return false; } public void print() { //System.out.println( flightMap ); for ( Map.Entry entry : flightMap.entrySet() ) { System.out.println( entry.getKey() ); } } public void updateFlightStatus( String externalflightNumber, char status ) { Flight flight = flightMap.get( externalflightNumber ); if ( flight == null ) { System.out.println( "Flight " + externalflightNumber + " is not valid!" ); return; } flight.updateStatus(status); } public ArrayList<Flight> getSortedDepartures(/*airport, day-of-week*/) { ArrayList<Flight> departures = new ArrayList<Flight>(); for ( Map.Entry<String, Flight> entry : flightMap.entrySet() ) { Flight aFlight = entry.getValue(); // TODO: check the airport code and departure day departures.add( aFlight ); } // TODO: Flight.setSortOrder( Flight.SCHEDULE_SORT_ORDER_DEPARTURE_TIME ); Collections.sort(departures); return departures; } }//class FlightSchedule