/* * CollectAirports.java * * Created on January 30, 2006, 10:19 PM * * 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 hold a collection of airports (such as all of them). */ public class CollectAirports { int currNumAirports; Airport airportList[]; // assume a steady number of airports so don't need flexibility /** * Creates a new instance of CollectAirports. This is an empty array. * None of the references are filled in. */ public CollectAirports() { airportList = new Airport [200]; // default version assumes no more than 200 airports } /** * report how many actual airports are currently stored in the collection */ public int getNumAirports() { return currNumAirports; } /** * displays all current airports to a file. * requires a print writer - which can be obtained using the HandleFile class */ public void displayAllAirports(PrintWriter outpw) { for (int cnt = 0; cnt < currNumAirports; cnt++){ airportList[cnt].displayAirport(outpw); outpw.println(); } } /** * adding airport to the collection */ public boolean addNextAirport (Airport currAirport) { // stick the new Airport in the vector airportList[currNumAirports] = currAirport; // update count of Airports currNumAirports++; return true; } /** * pick out an airport - by position in the collection */ public Airport getNthAirport(int nth) { return airportList[nth]; } /** * change a specific airport in the collection (by position). * Returns false if the position has not been instantiated yet. Use add * method to add a new airport instead of changing */ public boolean setNthAirport (Airport currAirport, int num) { if (num >= currNumAirports){ System.out.println("\n Bad Airport number: " + num + " when only " + currNumAirports + " exist\n" ); // return default return false; } else { airportList[num] = currAirport; return true; } } /** * Get airport corresponding to passed airport code (returns unknown airport if not found) */ public Airport getAirportByCode (String code) { Airport found = new Airport(); // default to unknown airport for (int cnt = 0; cnt < currNumAirports; cnt++) { if (airportList[cnt].getAirportCode().equals(code)) { // found it found = airportList[cnt]; } } return found; } /** * create a collection of airports via reading a file (filename is passed). * Depends on my HandleFile class. If there is a problem with the file, we exit * the application - the file needs to be fixed. This assumes that the file is in the * top folder for the project. */ public int readAirportsFromFile(String filename) { int expectedNumAirports; int success = 1; ////////////////////////////// // set up for reading file ////////////////////////////// try { BufferedReader bReader = HandleFile.startFile(filename); String line = bReader.readLine(); expectedNumAirports = Integer.parseInt(line); // file assumed to have number in first line telling how many airports to come for (int cnt = 0; cnt < expectedNumAirports; cnt++) { // get the next airport, add it to the airport vector Airport newAirport = new Airport(); success = newAirport.readAirportFromFile(bReader); // check if seem to be at end of file if (success < 0) { break; } else { // seems ok airportList[cnt] = newAirport; /* use array instead of vector */ currNumAirports++; // AddNextAirport(newAirport); } } } catch (IOException ex) { System.out.println("Error while reading airport file, exiting"); System.exit(0); } return currNumAirports; /* in case caller wants to know */ } }