/* * A class to implement finding whether or not you can fly direct from some city * to PHL. * Using both linear search and binary search. * * @author ggtowell * Created Dec 2023 */ import java.io.File; import java.util.Scanner; public class FlightFinder { Flight[] flights; public FlightFinder(String filename) { try { Scanner sc = new Scanner(new File(filename)); int size = sc.nextInt(); flights = new Flight[size]; for (int i = 0; i < size; i++) { int y = sc.nextInt(); int m = sc.nextInt(); int d = sc.nextInt(); String fn = sc.next(); int ad = sc.nextInt(); int dd = sc.nextInt(); String oc = sc.next(); String dc = sc.next(); Flight f = new Flight(y, m, d, fn, ad, dd, oc, dc); flights[i] = f; } } catch (Exception ee) { System.err.println("Problem " + ee); flights = new Flight[0]; } } public boolean findOrigin(String dest) { for (int i = 0; i < flights.length; i++) { //System.out.println(flights[i].getOriginCity() + " " + dest + " " + flights[i].getDestCity().equals(dest)); if (flights[i].getOriginCity().equals(dest)) { return true; } } return false; } // Uses selection sort public void sortByOrigin() { for (int i = 0; i < flights.length; i++) { int best = 0; for (int j = 1; j < (flights.length - i); j++) { if (flights[best].getOriginCity().compareTo(flights[j].getOriginCity()) < 0) { best = j; } } Flight temp = flights[best]; flights[best] = flights[flights.length - i - 1]; flights[flights.length - i - 1] = temp; } //for (int i = 0; i < 50; i++) { // System.out.println(flights[i]); //} } /** * Binary Search -- iterative * @param dest the thing to search for * @return true or false */ public boolean findOriginBinary(String dest) { int lo = 0; int hi = flights.length - 1; while (lo < hi) { int mid = (lo + hi) / 2; int ii = flights[mid].getOriginCity().compareTo(dest); if (ii == 0) { return true; } if (ii < 0) { // mid < dest so can eliminate the bottom lo = mid + 1; } else { hi = mid - 1; } } return false; } /** * Find a flight object with the given origin city * @param orig the origin city to look for * @return a flight object or null */ public Flight findFlightWithOrigin(String orig) { return ffwoHelper(orig, 0, flights.length - 1); } /** * Private recursive helper of Find a flight object with the given origin city * @param orig the origin city to look for * @param lo the low end of the array to consider * @param hi the high end of the array to consider * @return a flight object or null */ private Flight ffwoHelper(String orig, int lo, int hi) { if (lo > hi) { return null; } int mid = (lo + hi) / 2; int ii = flights[mid].getOriginCity().compareTo(orig); if (ii == 0) { return flights[mid]; } if (ii < 0) { // mid < dest so can eliminate the bottom return ffwoHelper(orig, mid + 1, hi); } else { return ffwoHelper(orig, lo, mid - 1); } } public static void main(String[] args) { mainRB(args); } // do the task using linear search public static void mainL(String[] args) { FlightFinder ff = new FlightFinder("phl5001.txt"); for (int i = 0; i < args.length; i++) { System.out.println("Can I fly direct from " + args[i] + "? " + ff.findOrigin(args[i])); } } // do the task using binary search. public static void mainB(String[] args) { FlightFinder ff = new FlightFinder("phl5001.txt"); ff.sortByOrigin(); for (int i = 0; i < args.length; i++) { System.out.println("Can I fly direct from " + args[i] + "? " + ff.findOriginBinary(args[i])); } } public static void mainRB(String[] args) { FlightFinder ff = new FlightFinder("phl5001.txt"); ff.sortByOrigin(); for (int i = 0; i < args.length; i++) { Flight f = ff.findFlightWithOrigin(args[i]); if (f == null) { System.out.println("No flights from " + args[i]); } else { System.out.println("here is a flight from " + args[i] + " " + f); } } } }