/* * CollectRoutes.java * * Created on January 31, 2006, 8:14 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package airline; import java.io.File; import java.io.*; import redmondfile.*; /** * * @author Mike * Used to handle a collection of Routes */ public class CollectRoutes { int currNumRoutes; Route routeList[]; // assume a steady number of routes so don't need flexibility /** * Creates a new instance of CollectRoutes. All references are empty (none instantiated) */ public CollectRoutes() { routeList = new Route [1000]; // default version assumes no more than 1000 routes } /** * report how many actual routes are in the collection */ public int getNumRoutes() { return currNumRoutes; } /** * displays to a file all current routes. Needs a PrintWriter, which can be obtained * using the HandleFile class */ public void displayAllRoutes(PrintWriter outpw) { for (int cnt = 0; cnt < currNumRoutes; cnt++){ routeList[cnt].displayRoute(outpw); outpw.println(); } } /** * adding route to the collection */ public boolean addNextRoute (Route currRoute) { // stick the new Route in the vector routeList[currNumRoutes] = currRoute; // update count of Routes currNumRoutes++; return true; } /** * report a particular route (by position in the collection) */ public Route getNthRoute(int nth) { return routeList[nth]; } /** * change a particular route (by position in the collection. * Returns false if the route has not already been instantiated. * Use add method to add a completely new route */ public boolean setNthRoute (Route currRoute, int num) { if (num >= currNumRoutes){ System.out.println("\n Bad Route number: " + num + " when only " + currNumRoutes + " exist\n" ); // return default return false; } else { routeList[num] = currRoute; return true; } } /** * Get route corresponding to passed airport codes (returns unknown route if not found) */ public Route getRouteByCode (String depart, String arriv) { Route found = new Route(); // default to unknown route for (int cnt = 0; cnt < currNumRoutes; cnt++) { if (routeList[cnt].getDepartLoc().getAirportCode().equals(depart) && routeList[cnt].getArrivLoc().getAirportCode().equals(arriv)) { // found it found = routeList[cnt]; } } return found; } /** * create a collection of routes via reading a file (filename is passed). * Also needs collection of airports, so route can be constructed from airports * while the input only includes codes. * Needs my HandleFile class and File library. * If there is a problem with the file we ditch the application; the file needs * to be fixed. */ public int readRoutesFromFile(String filename, CollectAirports allAirports) { int expectedNumRoutes; int success = 1; ////////////////////////////// // set up for reading file ////////////////////////////// try { BufferedReader bReader = HandleFile.startFile(filename); String line = bReader.readLine(); expectedNumRoutes = Integer.parseInt(line); // file assumed to have number in first line telling how many routes to come for (int cnt = 0; cnt < expectedNumRoutes; cnt++) { // get the next route, add it to the route vector Route newRoute = new Route(); success = newRoute.readRouteFromFile(bReader, allAirports); // check if seem to be at end of file if (success < 0) { break; } else { // seems ok routeList[cnt] = newRoute; /* use array instead of vector */ currNumRoutes++; // AddNextRoute(newRoute); } } } catch (IOException ex) { System.out.println("Error while reading route file, exiting"); System.exit(0); } return currNumRoutes; /* in case caller wants to know */ } }