Add Singular Route Adding Function

main
YaFedImYaEatIm 9 years ago
parent 66ee9ced2d
commit c7f033283d

@ -61,8 +61,13 @@ public class App extends Application
}catch (DataException e){
e.printStackTrace();
}/*
}
/*
//testout single route adding
try {
currentDataset.addRoute("D2", "MAC", "WIN", "Y", "0", "NOW");
}catch (DataException e){
e.printStackTrace();
}
//testout single airport adding
try {
@ -97,12 +102,11 @@ public class App extends Application
} catch (DataException e) {
e.printStackTrace();
}
*/
try {
System.out.println(currentDataset.importFlight("res/Samples/NZCH-WSSS.csv"));
} catch (DataException e) {
e.printStackTrace();
}
}*/
}

@ -908,6 +908,82 @@ public class Dataset {
}
}
/**
* Adds one single route to the program.
* @param airline
* @param sourceAirport
* @param destAirport
* @param codeshare
* @param stops
* @param equipment
* @throws DataException
*/
public void addRoute(String airline, String sourceAirport, String destAirport, String codeshare, String stops, String equipment) throws DataException{
int stopsVal = 0;
try{
stopsVal = Integer.parseInt(stops);
}catch (NumberFormatException e){
throw new DataException("Stops must be a greater than or equal to 0.");
}
if (stopsVal < 0){
throw new DataException("Stops must be a greater than or equal to 0.");
}
Route routeToAdd = new Route(airline, sourceAirport, destAirport, codeshare, stopsVal, equipment);
addRoute(routeToAdd);
}
public void addRoute(Route routeToAdd) throws DataException{
if (routeToAdd.getAirline().length() != 2 && routeToAdd.getAirline().length() != 3){
throw new DataException("Airline ICAO code must be 2 or 3 letters.");
}
if (routeToAdd.departsFrom().length() != 3 && routeToAdd.departsFrom().length() != 4){
throw new DataException("Airport Source Airport IATA must be 3 letters or 4 letters if ICAO.");
}
if (routeToAdd.arrivesAt().length() != 3 && routeToAdd.arrivesAt().length() != 4){
throw new DataException("Airport Destination Airport IATA must be 3 letters or 4 letters if ICAO.");
}
if (routeToAdd.getCode().length() != 0 && routeToAdd.getCode().length() != 1){
throw new DataException("Codeshare has to be empty or Y.");
}
for (String key : routeDictionary.keySet()){
routeDictionary.get(key).hasDuplicate(routeToAdd);
}
//checking is done now we add it to the dictionary and the database
//query database.
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db");
//add the airline
stmt = c.createStatement();
String airline = routeToAdd.getAirline().replace("\"", "\"\"");
String sourceAir = routeToAdd.departsFrom().replace("\"", "\"\"");
String destAir = routeToAdd.arrivesAt().replace("\"", "\"\"");
String equipment = routeToAdd.getEquipment().replace("\"", "\"\"");
String insertRouteQuery = "INSERT INTO `" + this.name + "_Routes` (`Airline`, `Source_Airport`, `Destination_Airport`," +
" `Codeshare`, `Stops`, `Equipment`) VALUES (\""+airline+"\", \""+sourceAir+"\", \""+destAir+"\", " +
"\""+routeToAdd.getCode()+"\", "+routeToAdd.getStops()+", \""+equipment+"\")";
stmt.execute(insertRouteQuery);
//get the airline id
stmt = c.createStatement();
String routeIDQuery = "SELECT * FROM `sqlite_sequence` WHERE `name` = \""+this.name+"_Route\" LIMIT 1;";
ResultSet routeIDRes= stmt.executeQuery(routeIDQuery);
int routeID = 0;
while (routeIDRes.next()){
routeID = Integer.parseInt(routeIDRes.getString("seq"));
}
routeToAdd.setID(routeID);
routes.add(routeToAdd);
//routeAirline + routeSourceAirport + routeArrvAirport + routeCodeShare + routeStops + routeEquip
String routeKey = routeToAdd.getAirline() + routeToAdd.departsFrom() + routeToAdd.arrivesAt() + routeToAdd.getCode() + routeToAdd.getStops() + routeToAdd.getEquipment();
routeDictionary.put(routeKey, routeToAdd);
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
public ArrayList<Airline> getAirlines() {
return airlines;
}

@ -309,10 +309,10 @@ public class Airport {
throw new DataException("Airport Name already Exists, Please Choose Another.");
}
if (!airport.getIATA_FFA().equals("") && airport.getIATA_FFA().equals(this.name)){
throw new DataException("Airport Name already Exists, Please Choose Another.");
throw new DataException("Airport IATA/FFA already Exists, Please Choose Another.");
}
if (!airport.getICAO().equals("") && airport.getICAO().equals(this.name)){
throw new DataException("Airport Name already Exists, Please Choose Another.");
throw new DataException("Airport ICAO already Exists, Please Choose Another.");
}
}
/**

@ -158,7 +158,18 @@ public class Route {
public String arrivesAt(){
return arrivalAirport;
}
/**
* Checks is passed route is a duplicate of the current one if so it throws an DataException
*/
public void hasDuplicate(Route route) throws DataException{
//routeAirline + routeSourceAirport + routeArrvAirport + routeCodeShare + routeStops + routeEquip
if (route.getAirline().equals(this.airline) && route.departsFrom().equals(this.departureAirport)
&& route.arrivesAt().equals(this.arrivalAirport) && route.getCode().equals(this.codeShare)
&& route.getStops() == this.stops && route.getEquipment().equals(this.equipment)){
throw new DataException("This Route already exists.");
}
}
@Override
public String toString(){

Loading…
Cancel
Save