/* * Route.java * * Created on January 30, 2006, 3:29 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 * Info about going from one airport to another - including length of trip. */ public class Route { private String routeID; // maybe later use ID class private Airport departLoc; private Airport arrivLoc; private String lengthCatg = "regional"; // maybe later use enumerated (short, regional, cross country, international) private int miles; // actual crow flies miles /** * Creates a new instance of Route. Default version has no info. Route is * essentially unknown. */ public Route() { routeID = "UNK"; departLoc = new Airport(); // unknown arrivLoc = new Airport(); // unknown miles = -1; } /** * Creates a new instance of Route - main info passed */ public Route(String rt, Airport start, Airport end, String catg, int mile) { routeID = rt; departLoc = start; // reference - exact same airport arrivLoc = end; // reference - exact same airport lengthCatg = catg; miles = mile; } //// need a capability to just pass the airport codes, but would need access to //// a collection of airports too //// inspectors /** * report route ID */ public String getRouteID() { return routeID; } /** * indicates if the route is an unknown route - probably either not initialized yet, or a problem exists * Checks route ID to see if it is "UNK" or miles is negative */ public boolean isUnknownRoute () { if (routeID.equals("UNK") || (miles < 0)) { return true; } else { return false; } } /** * reports airport (object) for departing aiport */ public Airport getDepartLoc() { return departLoc; } /** * reports airport (object) for arriving aiport */ public Airport getArrivLoc() { return arrivLoc; } /** * reports length category for route. Should be enumerated type later. */ public String getLengthCatg () { return lengthCatg; } // maybe later use enumerated (short, regional, cross country, international) /** * reports distance in miles for the route */ public int getMiles() { return miles; } // actual crow flies miles //// mutators /** * change route id. Probably should use ID class instead of string */ public boolean setRouteID(String id) { routeID = id; return true; // potentially able to be validated if we establish rules } /** * change departing airport. * potentially able to be validated if we have a list of known airports. */ public boolean setDepartLoc(Airport depart) { departLoc = depart; return true; // potentially able to be validated if we have a list of known airports } /** * change arriving airport. * potentially able to be validated if we have a list of known airports. */ public boolean setArrivLoc(Airport arriv) { arrivLoc = arriv; return true; // potentially able to be validated if we have a list of known airports } /** * change length category. Should be enumerated type later */ public boolean setLengthCatg (String catg) { if ( catg.equals("short") || catg.equals("regional") || catg.equals("cross country") || catg.equals("international") ) { lengthCatg = catg; return true; } else { return false; } } // maybe later use enumerated (short, regional, cross country, international) /** * change distance in miles for the route. * Returns false if miles not greater than 30. Does anybody fly from JFK to La Guardia? * I hope not. */ public boolean setMiles(int mil) { // I'm unwilling to believe that a commercial airliner has a flight of less than 30 miles if (mil > 30) { miles = mil; return true; } else { return false; } } // actual crow flies miles /** * create string represntation for the route. Short versions of airports used * to make more compact. */ public String toString() { String res = ""; res += "RouteID: " + this.routeID + " From: " + this.departLoc.toShortString() + " To: " + this.arrivLoc.toShortString() + " Catg: " + this.lengthCatg + " Miles: " + this.miles; return res; } /** * display to file info about a route. * Depends on having a PrintWriter which can be obtained using my HandleFile class */ public void displayRoute(PrintWriter outpw) { // pretty much nothing for now outpw.print(this.toString()); } /** * read an route from a file. Needs a collection of airports so only have to get * code, not everything about the airport. */ public int readRouteFromFile(BufferedReader bReader, CollectAirports allAirports) throws IOException { readRoute(bReader, allAirports); return 0; /* dont know what to return right now */ } /** * read route info from a file, sticking in the invoking Route object. If exception * is thrown, just give up. The file probably needs to be fixed. */ public void readRoute (BufferedReader bReader, CollectAirports allAirports) throws IOException { String line; String junk; int milesCrow; StringTokenizer tokenizer; // get all route info - line = bReader.readLine(); // break it into parts - token by token tokenizer = new StringTokenizer(line,","); String field = tokenizer.nextToken(); setRouteID(field); field = tokenizer.nextToken(); Airport from = allAirports.getAirportByCode(field); setDepartLoc(from); field = tokenizer.nextToken(); Airport to = allAirports.getAirportByCode(field); setArrivLoc(to); field = tokenizer.nextToken(); setLengthCatg(field); field = tokenizer.nextToken(); try { milesCrow = Integer.parseInt(field); setMiles(milesCrow); } catch (Exception ex) { // if get here - nothing we can do - the file must be fixed System.out.println("Problem with reading miles from route file, please fix"); } } }