/* * Airport.java * * Created on January 24, 2006, 4:05 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 java.util.StringTokenizer; /** * * @author redmond * Used to represent an Airport - identified by the 3 letter codes used by airlines. * Note that readAirportFromFile reads a comma delimited file into an existing Airport * object. Also note the imports of file, io, and StringTokenizer library classes. */ public class Airport { private String airportCode; // must be 3 letter code private String airportName; private String airportCity; private String airportState; // must be 2 char private String airportRegion; private boolean mainAirportForCity = true; // O'Hare true; Midway false /** * Creates a new instance of Airport. * Default version for if no info is available yet. * airportCode of "UNK" indicates that it is not a legitimate airport */ public Airport() { airportCode = "UNK"; airportName = "unknown"; airportCity = "unknown"; airportState = "??"; airportRegion = "unknown"; } /** * Creates a new instance of Airport - want all of this stuff - default to it is the main airport */ public Airport(String code, String name, String city, String state, String region) { airportCode = code; airportName = name; airportCity = city; airportState = state; airportRegion = region; } /** * Creates a new instance of Airport - all info passes */ public Airport(String code, String name, String city, String state, String region, boolean main) { airportCode = code; airportName = name; airportCity = city; airportState = state; airportRegion = region; mainAirportForCity = main; } /// inspectors /** * report airport code */ public String getAirportCode(){ return airportCode; // must be 3 letter code } /** * indicates if the airport is an unknown airport - probably either not initialized yet, or a problem exists * Checks airport code to see if it is "UNK" */ public boolean isUnknownAirport () { if (airportCode.equals("UNK")) { return true; } else { return false; } } /** * report airport name */ public String getAirportName(){ return airportName; } /** * report airport city */ public String getAirportCity(){ return airportCity; } /** * report airport state */ public String getAirportState(){ return airportState; // must be 2 char } /** * report airport region - right now just a string. May become an enumerated type */ public String getAirportRegion(){ return airportRegion; } /** * report whether airport is the main airport for the city */ public boolean getMainAirportForCity(){ return mainAirportForCity; } /// mutators - with mostly strings these won't be very interesting /** * change airport code. * Must be 3 character. * Should also check that it is non-numeric etc */ public boolean setAirportCode(String code){ if (code.length() == 3) { airportCode = code; // must be 3 letter code return true; } else { return false; } } /** * change airport name. * Not validatable */ public void setAirportName(String name){ airportName = name; } /** * change airport city. * not able to be validated. */ public void setAirportCity(String city){ airportCity = city; } /** * change airport state. * For now, only validate that the length is 2 (returning true or false). * Could later do a DB search or other lookup */ public boolean setAirportState(String state){ if (state.length() == 2) { airportState = state; // must be 2 char return true; } else { return false; } } /** * set airport region - later use an enumerated type */ public void setAirportRegion(String region){ airportRegion = region; } /** * change whether airport is main airport for the city */ public void setMainAirportForCity(boolean main){ mainAirportForCity = main; } /** * return a string representation of an airport * with labels on data. * Overrides Object class version */ public String toString() { String res = ""; res += this.airportCode + " City: " + this.airportCity + " State: " + this.airportState + " Name: " + this.airportName + " Region: " + this.airportRegion + " Main Airport?: " + this.mainAirportForCity; return res; } /** * provides more compact summary of the airport */ public String toShortString() { String res = ""; res += this.airportCode + " City: " + this.airportCity + " State: " + this.airportState; return res; } /** * display to a file the string representation of the airport */ public void displayAirport(PrintWriter outpw) { // pretty much nothing for now outpw.print(this.toString()); } /** * read an airport from a file. Doesn't do much more than the method it calls */ public int readAirportFromFile(BufferedReader bReader) throws IOException { readAirport(bReader); return 0; /* dont know what to return right now */ } /** * read airport info from a file, sticking in the invoking Airport object */ public void readAirport (BufferedReader bReader) throws IOException { String line; String junk; StringTokenizer tokenizer; // get all airport info - line = bReader.readLine(); // break it into parts - token by token tokenizer = new StringTokenizer(line,","); String field = tokenizer.nextToken(); setAirportCode(field); field = tokenizer.nextToken(); setAirportName(field); field = tokenizer.nextToken(); setAirportCity(field); field = tokenizer.nextToken(); setAirportState(field); field = tokenizer.nextToken(); setAirportRegion(field); field = tokenizer.nextToken(); if (field.equals("true")) { this.setMainAirportForCity(true); } else if (field.equals("false")) { this.setMainAirportForCity(false); } } }