FlightTracker{ public void executeQuery(int index) { switch (index) { case 1: queryFromAirportToAirport(TORONTO, VANCOUVER, true); break; case 2: queryFromAirportToAirport(SYDNEY, HONGKONG, true); break; case 3: findAllFlightsFrom(TORONTO); break; case 4: findAllFlightsFrom(SYDNEY); break; case 5: flightFromXtoYandStopsInZ(BUENOSAIRES, TORONTO, SANTIAGO); break; case 6: flightFromXtoYandStopsInZ(CALGARY, STJOHNS, OTTAWA); break; case 7: flightFromXtoYandStopsInZ(HALIFAX, VANCOUVER, TORONTO); break; case 8: flightFromXtoYandStopsInZ(TORONTO, VANCOUVER, HALIFAX); break; case 9: flightFromXtoYandStopsInZ(TORONTO, VANCOUVER, HALIFAX); break; case 10: findFlightsFromXToYOnDay(TORONTO, OTTAWA, "6"); break; case 11: queryFromAirportToAirport(TORONTO, OTTAWA, true); break; case 12: findFlightsFromXToYOnDay(VANCOUVER, TORONTO, "5"); break; case 13: queryFromAirportToAirport(VANCOUVER, TORONTO, true); break; case 14: findFlightsFromXToYOnDay(VANCOUVER, TORONTO, "56"); break; default: break; } } public void findFlightsFromXToYOnDay(String origin, String destination, String dayCode) { Collection possibleLegs = queryFromAirportToAirport(origin, destination, false); int counter = 0; Airport originAirport = getAirport(origin); Airport destAirport = getAirport(destination); if (originAirport == null) return; if (destAirport == null) return; System.out.print("Flights from " + originAirport + " to " + destAirport + " which fly on "); if (dayCode.indexOf('1') != -1) { System.out.print("Monday "); } if (dayCode.indexOf('2') != -1) { System.out.print("Tuesday "); } if (dayCode.indexOf('3') != -1) { System.out.print("Wednesday "); } if (dayCode.indexOf('4') != -1) { System.out.print("Thursday "); } if (dayCode.indexOf('5') != -1) { System.out.print("Friday "); } if (dayCode.indexOf('6') != -1) { System.out.print("Saturday "); } if (dayCode.indexOf('7') != -1) { System.out.print("Sunday "); } System.out.println("."); for (Object obj : possibleLegs) { if (hasScheduleInFrequency((RegularLeg) obj, dayCode)) { System.out.println(obj.toString()); counter++; } } System.out.println(counter + " flights in total."); } public Airport getAirport(String code) { for (RegularFlight regFlight : getRegularFlights().values()) { for (RegularLeg regLeg : regFlight.getRegularLegs()) { if (regLeg.getOrigin().getCode().equals(code)) return regLeg.getOrigin(); else if (regLeg.getDestination().getCode().equals(code)) return regLeg.getDestination(); } } System.out.println("Airport code " + code + " was invalid."); return null; } public void flightFromXtoYandStopsInZ(String origin, String destination, String stop) { Airport originAirport = getAirport(origin); if (originAirport == null) return; Airport destAirport = getAirport(destination); if (destAirport == null) return; Airport stopAirport = getAirport(stop); if (stopAirport == null) return; System.out.println("All flights from " + originAirport + " to " + destAirport + ", stopping in " + stopAirport); Collection fromAndTo = queryFromAirportToAirport(origin, destination, false); for (Object obj : fromAndTo) { RegularFlight regFlight = ((RegularLeg) obj).getRegularFlight(); if (hasFlightFromXtoY(origin, stop, regFlight) && hasFlightFromXtoY(stop, destination, regFlight)) System.out.println("Flight " + regFlight.getFlightNo()); } } public boolean hasFlightFromXtoY(String origin, String destination, RegularFlight regFlight) { Collection legs = regFlight.getRegularLegs(); for (RegularLeg leg : legs) { if (leg.getOrigin().getCode().equals(origin) && leg.getDestination().getCode().equals(destination)) return true; } return false; } public boolean hasScheduleInFrequency(RegularLeg regLeg, String freq) { Collection legSchedules = regLeg.getRegularLegSchedules(); String code = ""; if (freq.indexOf('1') != -1) { code += "1"; } if (freq.indexOf('2') != -1) { code += "2"; } if (freq.indexOf('3') != -1) { code += "3"; } if (freq.indexOf('4') != -1) { code += "4"; } if (freq.indexOf('5') != -1) { code += "5"; } if (freq.indexOf('6') != -1) { code += "6"; } if (freq.indexOf('7') != -1) { code += "7"; } for (RegularLegSchedule legSchedule : legSchedules) { if (legSchedule.getRegsched().toString().contains(freq)) return true; } return false; } public void findAllFlightsFrom(String code) { System.out.println("All flights from " + code); Airport airport = getAirport(code); if (airport == null) return; for (RegularLeg leg : airport.getFlightsFroms()) { System.out.println("Flight " + leg.getRegularFlight().getFlightNo() + " going to " + leg.getDestination().getName()); } } public Collection queryFromAirportToAirport(String originCode, String destCode, Boolean print) { int answerCounter = 0; // Collection allObjects = RuntimeRegistry.getInstance().getObjectSet(); Collection finalAnswer = new ArrayList(); Airport originAirport = getAirport(originCode); if (originAirport == null) return null; Airport destAirport = getAirport(destCode); if (destAirport == null) return null; if (print) System.out.println("All flights from " + originAirport + " to " + destAirport); for (RegularFlight regFlight : this.getRegularFlights().values()) { for (RegularLeg obj : regFlight.getRegularLegs()) { if (((RegularLeg) obj).getOrigin().getCode().equals(originCode) && ((RegularLeg) obj).getDestination().getCode().equals(destCode)) { RegularLeg regLeg = ((RegularLeg) obj); if (print) System.out.println("Flight number:" + regLeg.getRegularFlight().getFlightNo()); finalAnswer.add(regLeg); Collection regLegSchedules = regLeg.getRegularLegSchedules(); int counter = 0; for (RegularLegSchedule regLegSched : regLegSchedules) { counter++; int duration = Integer.parseInt(regLegSched.getArrTime()) + (24 * regLegSched.getMidnightCrossings()) - Integer.parseInt(regLegSched.getDepTime()); if (print) System.out.println(" Schedule " + counter + ", Flight duration " + (duration) + ", Frequency " // + regLegSched.getRegsched() // + " - " + regLegSched.getRegsched()); } answerCounter++; } } } if (print) System.out.println(answerCounter + " flights in total."); return finalAnswer; } }