diff --git a/res/userdb.db b/res/userdb.db index 9e3da2a..1af3ac3 100644 Binary files a/res/userdb.db and b/res/userdb.db differ diff --git a/src/main/java/seng202/group9/Controller/App.java b/src/main/java/seng202/group9/Controller/App.java index 8e803df..94ca7b5 100644 --- a/src/main/java/seng202/group9/Controller/App.java +++ b/src/main/java/seng202/group9/Controller/App.java @@ -60,8 +60,14 @@ public class App extends Application }catch (DataException e){ e.printStackTrace(); } - /* //testout single airport adding + try { + currentDataset.addAirport("Windows 10", "PC", "Windows", "WIN", "WIND", "0.0", "0.0", "0.0", "0.0", "U", "PC/Windows"); + }catch (DataException e){ + e.printStackTrace(); + } + /* + //testout single airline adding try { currentDataset.addAirline("Dota2", "Valve", "D2", "DOT", "Defence of the Ancients", "Steam", "Y"); }catch (DataException e){ diff --git a/src/main/java/seng202/group9/Controller/Dataset.java b/src/main/java/seng202/group9/Controller/Dataset.java index f31b912..0072e78 100644 --- a/src/main/java/seng202/group9/Controller/Dataset.java +++ b/src/main/java/seng202/group9/Controller/Dataset.java @@ -738,6 +738,17 @@ public class Dataset { } + /** + * Addes Single Airline to Program and Database. + * @param name + * @param alias + * @param IATA + * @param ICAO + * @param callsign + * @param country + * @param active + * @throws DataException + */ public void addAirline(String name, String alias, String IATA, String ICAO, String callsign, String country, String active) throws DataException{ Airline airlineToAdd = new Airline(name, alias, IATA, ICAO, callsign, country, active); addAirline(airlineToAdd); @@ -780,12 +791,121 @@ public class Dataset { } airlineToAdd.setID(airlineID); airlines.add(airlineToAdd); + airlineDictionary.put(airlineToAdd.getName(), airlineToAdd); } catch ( Exception e ) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); System.exit(0); } } + public void addAirport(String name, String city, String country, String IATA_FFA, String ICAO, String latitude, String longitude, + String altitude, String timezone, String DST, String olsonTz) throws DataException{ + try{ + double latitudeVal = Double.parseDouble(latitude); + double longitudeVal = Double.parseDouble(longitude); + double altitudeVal = Double.parseDouble(altitude); + double timezoneVal = Double.parseDouble(timezone); + Airport airportToAdd = new Airport(name, city, country, IATA_FFA, ICAO, latitudeVal, longitudeVal, altitudeVal); + City cityToAdd = new City(city, country, timezoneVal, olsonTz); + Country countryToAdd = new Country(DST, country); + addAirport(airportToAdd); + addCity(cityToAdd); + addCountry(countryToAdd); + }catch (NumberFormatException e){ + throw new DataException("Latitude, Longitude, Altitude and Timezone must be numbers"); + } + } + + public void addAirport(Airport airportToAdd) throws DataException{ + if (airportToAdd.getIATA_FFA() != "" && airportToAdd.getIATA_FFA().length() != 3){ + throw new DataException("IATA/FFA either empty or 3 letters"); + } + if (airportToAdd.getICAO() != "" && airportToAdd.getICAO().length() != 4){ + throw new DataException("ICAO either empty or 4 letters"); + } + for (String key : airportDictionary.keySet()){ + airportDictionary.get(key).hasDuplicate(airportToAdd); + } + //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 airport + stmt = c.createStatement(); + String insertAirportQuery = "INSERT INTO `" + this.name + "_Airport` (`Name`, `City`, `Country`, `IATA/FFA`, " + + "`ICAO`, `Latitude`, `Longitude`, `Altitude`) VALUES (\""+airportToAdd.getName()+"\", \""+airportToAdd.getCity()+"\", " + + "\""+airportToAdd.getCountry()+"\", \""+airportToAdd.getIATA_FFA()+"\", \""+airportToAdd.getICAO()+"\", " + + ""+airportToAdd.getLatitude()+", "+airportToAdd.getLongitude()+", "+airportToAdd.getAltitude()+");"; + stmt.execute(insertAirportQuery); + //get the airport id + stmt = c.createStatement(); + String airportIDQuery = "SELECT * FROM `sqlite_sequence` WHERE `name` = \""+this.name+"_Airport\" LIMIT 1;"; + ResultSet airportIDRes= stmt.executeQuery(airportIDQuery); + int airportID = 0; + while (airportIDRes.next()){ + airportID = Integer.parseInt(airportIDRes.getString("seq")); + } + airportToAdd.setID(airportID); + airports.add(airportToAdd); + airportDictionary.put(airportToAdd.getName(), airportToAdd); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + } + + public void addCity(City city){ + if (!cityDictionary.containsKey(city.getName())){ + Connection c = null; + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db"); + //add the city + stmt = c.createStatement(); + String cityName = city.getName().replace("\"", "\"\""); + String countryName = city.getCountry().replace("\"", "\"\""); + String olson = city.getTimeOlson().replace("\"", "\"\""); + String insertCityQuery = "INSERT INTO `" + this.name + "_City` (`City_Name`, `Country_Name`, `Timezone`, " + + "`Olson_Timezone`) VALUES (\""+cityName+"\", \""+countryName+"\", "+city.getTimezone() + ", \""+olson+"\");"; + stmt.execute(insertCityQuery); + stmt.close(); + cityDictionary.put(city.getName(), city); + cities.add(city); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + } + } + + public void addCountry(Country country){ + if (!countryDictionary.containsKey(country.getName())){ + Connection c = null; + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db"); + //add the city + stmt = c.createStatement(); + String countryName = country.getName().replace("\"", "\"\""); + String countryDST = country.getDST().replace("\"", "\"\""); + String insertCityQuery = "INSERT INTO `" + this.name + "_Country` (`Country_Name`, `DST`) VALUES" + + " (\""+countryName+"\", \""+countryDST+"\");"; + stmt.execute(insertCityQuery); + stmt.close(); + countryDictionary.put(country.getName(), country); + countries.add(country); + } catch ( Exception e ) { + System.err.println( e.getClass().getName() + ": " + e.getMessage() ); + System.exit(0); + } + } + } + public ArrayList getAirlines() { return airlines; } diff --git a/src/main/java/seng202/group9/Core/Airline.java b/src/main/java/seng202/group9/Core/Airline.java index e423c72..54ebc8d 100644 --- a/src/main/java/seng202/group9/Core/Airline.java +++ b/src/main/java/seng202/group9/Core/Airline.java @@ -205,7 +205,7 @@ public class Airline { } /** * Function Overload of addRoutes this allow the adding of a list to route - * @param routes + * @param routesToAdd array list of routes to add routes */ public void addRoutes(ArrayList routesToAdd){ for (int i = 0; i < routesToAdd.size(); i ++){ @@ -235,19 +235,19 @@ public class Airline { * @return */ public void hasDuplicate(Airline airline) throws DataException{ - if (this.name == airline.getName()){ + if (this.name.equals(airline.getName())){ throw new DataException("This Airline Name already Exists, Please Choose Another."); } - if (this.IATA != "" && this.IATA == airline.getIATA()){ + if (!this.IATA.equals("") && this.IATA.equals(airline.getIATA())){ throw new DataException("This IATA Code already Exists, Please Choose Another."); } - if (this.ICAO != "" && this.ICAO == airline.getICAO()){ + if (!this.ICAO.equals("") && this.ICAO.equals(airline.getICAO())){ throw new DataException("This ICAO Code already Exists, Please Choose Another."); } - if (this.alias != "" && this.alias == airline.getAlias()){ + if (!this.alias.equals("") && this.alias.equals(airline.getAlias())){ throw new DataException("This Alias already Exists, Please Choose Another."); } - if (this.callSign != "" && this.callSign == airline.getCallSign()){ + if (!this.callSign.equals("") && this.callSign.equals(airline.getCallSign())){ throw new DataException("This Callsign already Exists, Please Choose Another."); } } diff --git a/src/main/java/seng202/group9/Core/Airport.java b/src/main/java/seng202/group9/Core/Airport.java index 5de5e4d..8283933 100644 --- a/src/main/java/seng202/group9/Core/Airport.java +++ b/src/main/java/seng202/group9/Core/Airport.java @@ -304,6 +304,17 @@ public class Airport { distance = 6371 * c; return distance; } + public void hasDuplicate(Airport airport) throws DataException{ + if (!airport.getName().equals("") && airport.getName().equals(this.name)){ + 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."); + } + if (!airport.getICAO().equals("") && airport.getICAO().equals(this.name)){ + throw new DataException("Airport Name already Exists, Please Choose Another."); + } + } /** * Information of the airport returned in String format. */