Parsers, Core Files and Dataset Controllers

This commit is contained in:
YaFedImYaEatIm
2016-09-09 10:05:02 +12:00
parent f0e1691f69
commit bbb9a250ba
22 changed files with 83161 additions and 74 deletions
@@ -0,0 +1,127 @@
package seng202.group9.Controller;
import seng202.group9.Core.Airline;
import java.io.*;
import java.util.ArrayList;
public class AirlineParser extends Parser {
String filePath = "";
ArrayList<Airline> parsedAirline;
public AirlineParser(String filePath){
this.filePath = filePath;
parsedAirline = new ArrayList<Airline>();
}
public String parse() throws DataException{
int successful = 0;
int error = 0;
int duplicate = 0;
File file = new File(filePath);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
//read file here
//id, name, alias, city, iata, icao, callsign, country, active
//sample line: 2,"135 Airways",\N,"","GNL","GENERAL","United States","N"
String airlName = "";
String airlAlias = "";
String airlIATA = "";
String airlICAO = "";
String airlCallsign = "";
String airlCountry = "";
String airlActive = "";
String parts[] = {"", "", "", "", "", "", "", ""};
int part = 0;
boolean inQuotes = false;
for (int i = 0; i < line.length(); i ++){
if (line.charAt(i) == ','){
if (inQuotes == true){
parts[part] += line.charAt(i);
}else{
part++;
}
}else if (line.charAt(i) == '\"'){
inQuotes = !inQuotes;
}else {
parts[part] += line.charAt(i);
}
}
//check length
if (parts.length != 8){
System.out.println(parts[1] + " does not have 8 entries.");
error++;
continue;
}
//types do not need to be checked as they are all strings
boolean errorBreak = false;
//cehck sizes of [][] eg {3,0} will check if length == 3 or == 0. if -1 the size is ignored
int partSizes[][] = {{-1}, {-1}, {-1}, {2,0}, {3,0}, {-1}, {-1}, {1}};
for (int i = 1; i < partSizes.length; i ++){
boolean passable = false;
for (int j = 0; j < partSizes[i].length; j++){
if (partSizes[i][j] != -1) {
if (parts[i].equals("\\N")){
parts[i] = "";
}
if (parts[i].length() == partSizes[i][j] || parts[i].equals("-") ||
parts[i].equals("N/A")) {
passable = true;
}
}else{
passable = true;
}
}
if (passable == false){
System.out.println(parts[1] + " has Length: " + parts[i].length() + ", Value: " + parts[i] + " @ Index: " + i);
error++;
errorBreak = true;
break;
}
}
if (errorBreak == true){
continue;
}
//passing is done now add stuff to array
//id, name, alias, iata, icao, callsign, country, active
airlName = parts[1];
airlAlias = parts[2];
airlIATA = parts[3];
airlICAO = parts[4];
airlCallsign = parts[5];
airlCountry = parts[6];
airlActive = parts[7];
parsedAirline.add(new Airline(airlName, airlAlias, airlIATA, airlICAO, airlCallsign, airlCountry, airlActive));
successful++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new DataException("File: " +this.filePath+" is Missing.");
} catch (IOException e) {
e.printStackTrace();
throw new DataException(this.filePath + " is Corrupted.");
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
throw new DataException(this.filePath + " is unable to initialise reader.");
}
}
return String.format("Entries Successfully Entered: %1$d.\n" +
"Entries With Errors: %2$d", successful, error);
}
public ArrayList<Airline> getResult(){
return parsedAirline;
}
}
@@ -0,0 +1,184 @@
package seng202.group9.Controller;
import seng202.group9.Core.Airport;
import seng202.group9.Core.City;
import seng202.group9.Core.Country;
import java.io.*;
import java.util.ArrayList;
public class AirportParser extends Parser {
String filePath = "";
ArrayList<Airport> parsedAirports;
ArrayList<City> parsedCities;
ArrayList<Country> parsedCountries;
public AirportParser(String filePath){
this.filePath = filePath;
parsedAirports = new ArrayList<Airport>();
parsedCities = new ArrayList<City>();
parsedCountries = new ArrayList<Country>();
}
public String parse() throws DataException{
int successful = 0;
int error = 0;
int duplicate = 0;
File file = new File(filePath);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
//read file here
//id, name, city, country, iata/ffa, icao, lat, long, altitude, Timezone, DST, TimeOlson
//sample line: 1,"Goroka","Goroka","Papua New Guinea","GKA","AYGA",-6.081689,145.391881,5282,10,"U","Pacific/Port_Moresby"
String airpName = "";
String airpCity = "";
String airpCountry = "";
String airpIATA_FFA = "";
String airpICAO = "";
double airpLat = 0.0;
double airpLong = 0.0;
double airpAltitude = 0.0;
double airpTimezone = 0;
String airpDST = "U";
String airpOlson = "";
//line = line.replaceAll("\"", "");
String parts[] = {"", "", "", "", "", "", "", "", "", "", "", ""};
int part = 0;
boolean inQuotes = false;
for (int i = 0; i < line.length(); i ++){
if (line.charAt(i) == ','){
if (inQuotes == true){
parts[part] += line.charAt(i);
}else{
part++;
}
}else if (line.charAt(i) == '\"'){
inQuotes = !inQuotes;
}else {
parts[part] += line.charAt(i);
}
}
//System.out.println(parts[0] + " | " + parts[1] + " | " + parts[2] + " | " + parts[3] + " | " + parts[4] + " | "
// + parts[5] + " | " + parts[6] + " | " + parts[7] + " | " + parts[8] + " | " + parts[9] + " | " + parts[10] + " | " + parts[11]);
//check length
if (parts.length != 12){
System.out.println(parts[1] + " does not have 12 entries.");
error++;
continue;
}
//check types I = integer, S = String D = Double
char partTypes[] = {'I','S', 'S','S', 'S', 'S', 'D', 'D', 'D', 'D', 'S', 'S'};
//ignore the first value as their id is not useful to us.
boolean errorBreak = false;
for (int i = 1; i < partTypes.length; i ++){
if (partTypes[i] == 'I'){
try {
if (parts[i].equals("\\N")){
parts[i] = "0";
}
Integer.parseInt(parts[i]);
}catch (NumberFormatException e){
System.out.println(parts[1] + " has Value: " + parts[i] + " And is not a Integer Formattable Value.");
error++;
errorBreak = true;
break;
}
}else if (partTypes[i] == 'D'){
try{
if (parts[i].equals("\\N")){
parts[i] = "0";
}
Double.parseDouble(parts[i]);
}catch (NumberFormatException e){
System.out.println(parts[1] + " has Value: " + parts[i] + " And is not a Double Formattable Value.");
error++;
errorBreak = true;
break;
}
}
}
if (errorBreak == true){
continue;
}
//cehck sizes of [][] eg {3,0} will check if length == 3 or == 0. if -1 the size is ignored
int partSizes[][] = {{-1}, {-1}, {-1}, {-1}, {3,0}, {4,0}, {-1}, {-1}, {-1}, {-1}, {1,0}, {-1}};
for (int i = 1; i < partSizes.length; i ++){
boolean passable = false;
for (int j = 0; j < partSizes[i].length; j++){
if (partSizes[i][j] != -1) {
if (parts[i].equals("\\N")){
parts[i] = "";
}
if (parts[i].length() == partSizes[i][j]) {
passable = true;
}
}else{
passable = true;
}
}
if (passable == false){
System.out.println(parts[1] + " has Length: " + parts[i].length() + ", Value: " + parts[i] + " @ Index: " + i);
error++;
errorBreak = true;
break;
}
}
if (errorBreak == true){
continue;
}
//passing is done now add stuff to array
airpName = parts[1];
airpCity = parts[2];
airpCountry = parts[3];
airpIATA_FFA = parts[4];
airpICAO = parts[5];
airpLat = Double.parseDouble(parts[6]);
airpLong = Double.parseDouble(parts[7]);
airpAltitude = Double.parseDouble(parts[8]);
airpTimezone = Double.parseDouble(parts[9]);
airpDST = parts[10];
airpOlson = parts[11];
parsedAirports.add(new Airport(airpName, airpCity, airpCountry, airpIATA_FFA, airpICAO, airpLat, airpLong, airpAltitude));
parsedCities.add(new City(airpCity, airpCountry, airpTimezone, airpOlson));
parsedCountries.add(new Country(airpDST, airpCountry));
successful++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new DataException("File: " +this.filePath+" is Missing.");
} catch (IOException e) {
e.printStackTrace();
throw new DataException(this.filePath + " is Corrupted.");
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
throw new DataException(this.filePath + " is unable to initialise reader.");
}
}
return String.format("Entries Successfully Entered: %1$d.\n" +
"Entries With Errors: %2$d", successful, error);
}
public ArrayList<Airport> getResult(){
return parsedAirports;
}
public ArrayList<City> getCityResult(){
return parsedCities;
}
public ArrayList<Country> getCountryResult(){
return parsedCountries;
}
}
@@ -14,17 +14,15 @@ import seng202.group9.GUI.MainMenuBar;
*/
public class App extends Application
{
ArrayList<DatasetController> Datasets = new ArrayList<DatasetController>();
ArrayList<Dataset> Datasets = new ArrayList<Dataset>();
public static void main( String[] args )
{
launch(args);
}
/**
* Starts the application
* @param primaryStage main "stage" of the program
* @see The last sessions menu or the getting started page.
*/
@Override
public void start(Stage primaryStage) {
@@ -37,5 +35,33 @@ public class App extends Application
primaryStage.setScene(scene);
primaryStage.show();
//testing out dataset
Dataset dataset1 = null;
try {
dataset1 = new Dataset("test's", Dataset.getExisting);
}catch (DataException e){
e.printStackTrace();
}
//testing out airport parser
AirportParser airportParser = new AirportParser("res/Samples/Airports.txt");
try {
System.out.println(airportParser.parse());
} catch (DataException e) {
e.printStackTrace();
}
//testing out airline parser
AirlineParser airlineParser = new AirlineParser("res/Samples/Airlines.txt");
try {
System.out.println(airlineParser.parse());
} catch (DataException e) {
e.printStackTrace();
}
//testing out route parser
RouteParser routeParser = new RouteParser("res/Samples/Routes.txt");
try {
System.out.println(routeParser.parse());
} catch (DataException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,321 @@
package seng202.group9.Controller;
import seng202.group9.Core.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class Dataset {
String name;
static boolean getExisting = true;
static boolean createNew = false;
ArrayList<Airline> airlines;
ArrayList<Airport> airports;
ArrayList<Route> routes;
ArrayList<FlightPath> flightPaths;
ArrayList<Country> countries;
ArrayList<City> cities;
/**
*
* @param name Name of the database
* @param action either Dataset.getExisting or Dataset.createNew
* @throws DataException Throws an exception if there is some error ie databases with the same name
*/
public Dataset(String name, boolean action) throws DataException {
this.name = name;
this.airlines = new ArrayList<Airline>();
this.airports = new ArrayList<Airport>();
this.flightPaths = new ArrayList<FlightPath>();
this.routes = new ArrayList<Route>();
this.cities = new ArrayList<City>();
this.countries = new ArrayList<Country>();
if (action == getExisting){
updateDataset();
//after this make connections. ie filling in the country.cities airports.routes etc
}else if (action == createNew){
createTables();
}
}
/**
* Updates Dataset Arrays from Database.
* @throws DataException
*/
public void updateDataset() throws DataException{
Connection c = null;
Statement stmt = null;
int numOfDuplicateNames = 0;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db");
stmt = c.createStatement();
String queryName = this.name.replace("'", "''").replace("\"", "\"\"");//this allows quotes in datasets
ResultSet rs = stmt.executeQuery( "SELECT COUNT(*) FROM `Datasets` WHERE `Dataset_Name` = '"+queryName+"';" );
while ( rs.next() ) {
numOfDuplicateNames = rs.getInt("COUNT(*)");
}
if (numOfDuplicateNames == 0){
throw new DataException("There is no Dataset under this Name.");
}
rs.close();
stmt.close();
//if no problem update data;
/*//////////////////
//get all airlines//
//////////////////*/
stmt = c.createStatement();
String queryAirlines = "SELECT * FROM `"+this.name+"_Airline`";
rs = stmt.executeQuery(queryAirlines);
while ( rs.next() ){
//Airline(int ID, String name, String alias, String IATA, String ICAO, String callSign, String country, String active)
int airID = rs.getInt("Airline_ID");
String airName = rs.getString("Name");
String airIATA = rs.getString("IATA");
String airICAO = rs.getString("ICAO");
String airAlias = rs.getString("Alias");
String airCallsign = rs.getString("CallSign");
String airCountry = rs.getString("Country");
String airActive = rs.getString("Active");
airlines.add(new Airline(airID, airName, airIATA, airICAO, airAlias, airCallsign, airCountry, airActive));
}
rs.close();
stmt.close();
/*//////////////////
//get all Airports//
//////////////////*/
stmt = c.createStatement();
String queryAirport = "SELECT * FROM `"+this.name+"_Airport`";
rs = stmt.executeQuery(queryAirport);
while ( rs.next() ){
//Airport(int ID, String name, String city, String country, String IATA_FFA, String ICAO, double latitude, double longitude, double altitude)
int airpID = rs.getInt("Airport_ID");
String airpName = rs.getString("Name");
String airpCity = rs.getString("City");
String airpCountry = rs.getString("Country");
String airpIATA_FFA = rs.getString("IATA/FFA");
String airpICAO = rs.getString("ICAO");
double airpLatitude = rs.getDouble("Latitude");
double airpLongitude = rs.getDouble("Longitude");
double airpAltitude = rs.getDouble("Altitude");
airports.add(new Airport(airpID, airpName, airpCity, airpCountry, airpIATA_FFA, airpICAO, airpLatitude, airpLongitude, airpAltitude));
}
rs.close();
stmt.close();
/*////////////////
//get all cities//
////////////////*/
stmt = c.createStatement();
String queryCities = "SELECT * FROM `"+this.name+"_City`";
rs = stmt.executeQuery(queryCities);
while ( rs.next() ){
//City(String name, String timezone, String timeOlson)
String cityName = rs.getString("City_Name");
String cityCountry = rs.getString("Country_Name");
double cityTz = rs.getDouble("Timezone");
String cityTimeOlson = rs.getString("Olson_Timezone");
cities.add(new City(cityName, cityCountry, cityTz, cityTimeOlson));
}
rs.close();
stmt.close();
/*///////////////////
//get all Countries//
///////////////////*/
stmt = c.createStatement();
String queryCountry = "SELECT * FROM `"+this.name+"_Country`";
rs = stmt.executeQuery(queryCountry);
while ( rs.next() ){
//Country(String DST, String name)
String countName = rs.getString("Country_Name");
String countDST = rs.getString("DST");
countries.add(new Country(countDST, countName));
}
rs.close();
stmt.close();
/*/////////////////////
//get all Flight Path//
/////////////////////*/
stmt = c.createStatement();
String queryFlightPath = "SELECT * FROM `"+this.name+"_Flight_Path`";
rs = stmt.executeQuery(queryFlightPath);
while ( rs.next() ){
//FlightPath(int ID, String departureAirport, String arrivalAirport)
int flightpID = rs.getInt("Path_ID");
String flightpDepart = rs.getString("Source_Airport");
String flightpArrive = rs.getString("Destination_Airport");
flightPaths.add(new FlightPath(flightpID, flightpDepart, flightpArrive));
}
rs.close();
stmt.close();
/*///////////////////////
//get all flight points//
///////////////////////*/
for (int i = 0; i < flightPaths.size(); i++){
stmt = c.createStatement();
String queryFlightPoints = "SELECT * FROM `" + this.name + "_Flight_Points` WHERE `Index_ID` = "+flightPaths.get(i).getID() + " ORDER BY `Order` ASC";
rs = stmt.executeQuery(queryFlightPoints);
while (rs.next()) {
//FlightPoint(String name, int ID, int indexID, String type, String via,
//int heading, double altitude, double legDistance, double totalDistance,
//double latitude, double longitude)
String flightPtName = rs.getString("Name");
int flightPtID = rs.getInt("Point_ID");
int flightPtInd = rs.getInt("Index_ID");
String flightPtType = rs.getString("Type");
String flightPtVia = rs.getString("Via");
int flightPtheading = rs.getInt("Heading");
double flightPtAltitude = rs.getDouble("Altitude");
double flightPtLegDistance = rs.getDouble("Leg_Dist");
double flightPtTotDist = rs.getDouble("Tot_Dist");
double flightPtLatitude = rs.getDouble("Latitude");
double flightPtLongitude = rs.getDouble("Longitude");
flightPaths.get(i).addFlightPoint(new FlightPoint(flightPtName, flightPtID, flightPtInd
, flightPtType, flightPtVia, flightPtheading, flightPtAltitude, flightPtLegDistance, flightPtTotDist,
flightPtLatitude, flightPtLongitude));
}
rs.close();
stmt.close();
}
/*////////////////
//Get all Routes//
////////////////*/
stmt = c.createStatement();
String queryRoute = "SELECT * FROM `"+this.name+"_Routes`";
rs = stmt.executeQuery(queryRoute);
while ( rs.next() ){
//Route(int ID, String airline, String departureAirport, String arrivalAirport,
//String codeShare, int stops, String equipment)
int routeID = rs.getInt("Route_ID");
String routeAirline = rs.getString("Airline");
String routeDestAirport = rs.getString("Source_Airport");
String routeArrvAirport = rs.getString("Destination_Airport");
String routeCodeShare = rs.getString("Codeshare");
int routeStops = rs.getInt("Stops");
String routeEquip = rs.getString("Equipment");
routes.add(new Route(routeID, routeAirline, routeDestAirport, routeArrvAirport, routeCodeShare, routeStops, routeEquip));
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
public void createTables() throws DataException{
Connection c = null;
Statement stmt = null;
int numOfDuplicateNames = 0;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:res/userdb.db");
stmt = c.createStatement();
String queryName = this.name.replace("'", "''").replace("\"", "\"");//this allows quotes in datasets
ResultSet rs = stmt.executeQuery( "SELECT COUNT(*) FROM `Datasets` WHERE `Dataset_Name` = '"+queryName+"';" );
while ( rs.next() ) {
numOfDuplicateNames = rs.getInt("COUNT(*)");
}
if (numOfDuplicateNames > 0){
throw new DataException("There is already a Dataset with this Name.");
}
stmt.close();
rs.close();
//if no problem create tables;
//create airline table;
stmt = c.createStatement();
String createAirlineQuery = "CREATE TABLE `"+this.name+"_Airline` " +
"(`Airline_ID` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " +
"`Name` TEXT, " +
"`Alias` TEXT, " +
"`IATA` VARCHAR(2), " +
"`ICAO` VARCHAR(3), " +
"`Callsign` TEXT, " +
"`Country` TEXT, " +
"`Active` VARCHAR(1));";
stmt.execute(createAirlineQuery);
stmt.close();
//create airport table;
stmt = c.createStatement();
String createAirportQuery = "CREATE TABLE `"+this.name+"_Airport` " +
"(`Airport_ID` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " +
"`Name` TEXT, " +
"`City` TEXT, " +
"`Country` TEXT, " +
"`IATA/FFA` VARCHAR(3), " +
"`ICAO` VARCHAR(4), " +
"`Latitude` REAL, " +
"`Longitude` REAL, " +
"`Altitude` REAL);";
stmt.execute(createAirportQuery);
stmt.close();
//create City table;
stmt = c.createStatement();
String createCityTable = "CREATE TABLE `"+this.name+"_City` " +
"(`City_Name` TEXT UNIQUE, " +
"`Country_Name` TEXT, " +
"`Timezone` REAL, " +
"`Olson_Timezone` TEXT)";
stmt.execute(createCityTable);
stmt.close();
//create Country Table
stmt = c.createStatement();
String createCountryTable = "CREATE TABLE `"+this.name+"_Country` " +
"(`Country_Name` TEXT UNIQUE, " +
"`DST` VARCHAR(1))";
stmt.execute(createCountryTable);
stmt.close();
//create flightpath table
stmt = c.createStatement();
String createFlightPathTable = "CREATE TABLE `"+this.name+"_Flight_Path` " +
"(`Path_ID` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " +
"`Source_Airport` TEXT, " +
"`Destination_Airport` TEXT)";
stmt.execute(createFlightPathTable);
//create flight point table
stmt = c.createStatement();
String createFlightPointTable = "CREATE TABLE `"+this.name+"_Flight_Points` " +
"(`Index_ID` INTEGER ," +
"`Name` TEXT, " +
"`Point_ID` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " +
"`Type` TEXT, " +
"`Via` TEXT, " +
"`Heading` TEXT, " +
"`Altitude` INTEGER, " +
"`Tot_Dist` INTEGER, " +
"`Longitude` REAL, " +
"`Latitude` REAL, " +
"`Leg_Dist` INTEGER, " +
"`Order` INTEGER)";
stmt.execute(createFlightPointTable);
stmt.close();
//create routes table
stmt = c.createStatement();
String createRoutesTable = "CREATE TABLE `"+this.name+"_Routes` " +
"(`Route_ID` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " +
"`Airline` TEXT, " +
"`Source_Airport` TEXT, " +
"`Destination_Airport` TEXT, " +
"`Codeshare` VARCHAR(1), " +
"`Stops` INTEGER, " +
"`Equipment` TEXT)";
stmt.execute(createRoutesTable);
stmt.close();
//insert dataset into table
stmt = c.createStatement();
String insertDataset = "INSERT INTO `Datasets` (`Dataset_Name`) VALUES ('"+queryName+"');";
stmt.execute(insertDataset);
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
}
@@ -1,5 +0,0 @@
package seng202.group9.Controller;
public class DatasetController {
}
@@ -0,0 +1,158 @@
package seng202.group9.Controller;
import seng202.group9.Core.FlightPoint;
import java.io.*;
import java.util.ArrayList;
public class FlightPathParser extends Parser {
String filePath = "";
ArrayList<FlightPoint> parsedPoints;
public FlightPathParser(String filePath){
this.filePath = filePath;
parsedPoints = new ArrayList<FlightPoint>();
}
public String parse() throws DataException{
int successful = 0;
int error = 0;
int duplicate = 0;
File file = new File(filePath);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
//read file here
//(String type, String via, double altitude, double latitude, double longitude
//APT,NZCH,0,-43.48664019,172.53368221
String flightPtType = "";
String flightPtVia = "";
double flightPtAltitude = 0.0;
double flightPtLat = 0.0;
double flightPtLong = 0.0;
//line = line.replaceAll("\"", "");
String parts[] = {"", "", "", "", ""};
int part = 0;
boolean inQuotes = false;
for (int i = 0; i < line.length(); i ++){
if (line.charAt(i) == ','){
if (inQuotes == true){
parts[part] += line.charAt(i);
}else{
part++;
}
}else if (line.charAt(i) == '\"'){
inQuotes = !inQuotes;
}else {
parts[part] += line.charAt(i);
}
}
//System.out.println(parts[0] + " | " + parts[1] + " | " + parts[2] + " | " + parts[3] + " | " + parts[4] + " | "
// + parts[5] + " | " + parts[6] + " | " + parts[7] + " | " + parts[8] + " | " + parts[9] + " | " + parts[10] + " | " + parts[11]);
//check length this may need to be optional depending on size as the lecturers have given us size 5 while the websites give a lot more
if (parts.length != 5){
System.out.println(parts[1] + " does not have 5 entries.");
error++;
continue;
}
//check types I = integer, S = String D = Double P = Pass
char partTypes[] = {'S', 'S', 'D', 'D', 'D'};
boolean errorBreak = false;
for (int i = 0; i < partTypes.length; i ++){
if(partTypes[i] == 'P'){
//if P it is a pass
} else if (partTypes[i] == 'I'){
try {
if (parts[i].equals("\\N")){
parts[i] = "0";
}
Integer.parseInt(parts[i]);
}catch (NumberFormatException e){
System.out.println(parts[1] + " has Value: " + parts[i] + " And is not a Integer Formattable Value.");
error++;
errorBreak = true;
break;
}
}else if (partTypes[i] == 'D'){
try{
if (parts[i].equals("\\N")){
parts[i] = "0";
}
Double.parseDouble(parts[i]);
}catch (NumberFormatException e){
System.out.println(parts[1] + " has Value: " + parts[i] + " And is not a Double Formattable Value.");
error++;
errorBreak = true;
break;
}
}
}
if (errorBreak == true){
continue;
}
//cehck sizes of [][] eg {3,0} will check if length == 3 or == 0. if -1 the size is ignored
int partSizes[][] = {{3}, {-1}, {-1}, {-1}, {-1}};
for (int i = 0; i < partSizes.length; i ++){
boolean passable = false;
for (int j = 0; j < partSizes[i].length; j++){
if (partSizes[i][j] != -1) {
if (parts[i].equals("\\N")){
parts[i] = "";
}
if (parts[i].length() == partSizes[i][j]) {
passable = true;
}
}else{
passable = true;
}
}
if (passable == false){
System.out.println(parts[1] + " has Length: " + parts[i].length() + ", Value: " + parts[i] + " @ Index: " + i);
error++;
errorBreak = true;
break;
}
}
if (errorBreak == true){
continue;
}
//passing is done now add stuff to array
//(String type, String via, double altitude, double latitude, double longitude
flightPtType = parts[0];
flightPtVia = parts[1];
flightPtAltitude = Double.parseDouble(parts[2]);
flightPtLat = Double.parseDouble(parts[3]);
flightPtLong = Double.parseDouble(parts[4]);
parsedPoints.add(new FlightPoint(flightPtType, flightPtVia, flightPtAltitude, flightPtLat, flightPtLong));
successful++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new DataException("File: " +this.filePath+" is Missing.");
} catch (IOException e) {
e.printStackTrace();
throw new DataException(this.filePath + " is Corrupted.");
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
throw new DataException(this.filePath + " is unable to initialise reader.");
}
}
return String.format("Entries Successfully Entered: %1$d.\n" +
"Entries With Errors: %2$d", successful, error);
}
public ArrayList<FlightPoint> getResult(){
return parsedPoints;
}
}
@@ -0,0 +1,8 @@
package seng202.group9.Controller;
import java.util.ArrayList;
public abstract class Parser {
abstract String parse() throws DataException;
}
@@ -0,0 +1,160 @@
package seng202.group9.Controller;
import seng202.group9.Core.Route;
import java.io.*;
import java.util.ArrayList;
public class RouteParser extends Parser {
String filePath = "";
ArrayList<Route> parsedRoutes;
public RouteParser(String filePath){
this.filePath = filePath;
parsedRoutes = new ArrayList<Route>();
}
public String parse() throws DataException{
int successful = 0;
int error = 0;
int duplicate = 0;
File file = new File(filePath);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
//read file here
//String airline, airline id, String departureAirport, airport id, String arrivalAirport, arrival airport id, String codeShare, int stops, String equipment
//sample line: 2B,410,AER,2965,KZN,2990,,0,CR2
String routeAirName = "";
String routeDepart = "";
String routeArrive = "";
String routeCodeshare = "";
int routeStops = 0;
String routeEquip = "";
//line = line.replaceAll("\"", "");
String parts[] = {"", "", "", "", "", "", "", "", ""};
int part = 0;
boolean inQuotes = false;
for (int i = 0; i < line.length(); i ++){
if (line.charAt(i) == ','){
if (inQuotes == true){
parts[part] += line.charAt(i);
}else{
part++;
}
}else if (line.charAt(i) == '\"'){
inQuotes = !inQuotes;
}else {
parts[part] += line.charAt(i);
}
}
//System.out.println(parts[0] + " | " + parts[1] + " | " + parts[2] + " | " + parts[3] + " | " + parts[4] + " | "
// + parts[5] + " | " + parts[6] + " | " + parts[7] + " | " + parts[8] + " | " + parts[9] + " | " + parts[10] + " | " + parts[11]);
//check length
if (parts.length != 9){
System.out.println(parts[1] + " does not have 9 entries.");
error++;
continue;
}
//check types I = integer, S = String D = Double P = Pass
char partTypes[] = {'S', 'P', 'S','P', 'S', 'P', 'S', 'I', 'S'};
boolean errorBreak = false;
for (int i = 0; i < partTypes.length; i ++){
if(partTypes[i] == 'P'){
//if P it is a pass
} else if (partTypes[i] == 'I'){
try {
if (parts[i].equals("\\N")){
parts[i] = "0";
}
Integer.parseInt(parts[i]);
}catch (NumberFormatException e){
System.out.println(parts[1] + " has Value: " + parts[i] + " And is not a Integer Formattable Value.");
error++;
errorBreak = true;
break;
}
}else if (partTypes[i] == 'D'){
try{
if (parts[i].equals("\\N")){
parts[i] = "0";
}
Double.parseDouble(parts[i]);
}catch (NumberFormatException e){
System.out.println(parts[1] + " has Value: " + parts[i] + " And is not a Double Formattable Value.");
error++;
errorBreak = true;
break;
}
}
}
if (errorBreak == true){
continue;
}
//cehck sizes of [][] eg {3,0} will check if length == 3 or == 0. if -1 the size is ignored
int partSizes[][] = {{2,3}, {-1}, {3,4}, {-1}, {3,4}, {-1}, {1,0}, {-1}, {-1}};
for (int i = 0; i < partSizes.length; i ++){
boolean passable = false;
for (int j = 0; j < partSizes[i].length; j++){
if (partSizes[i][j] != -1) {
if (parts[i].equals("\\N")){
parts[i] = "";
}
if (parts[i].length() == partSizes[i][j]) {
passable = true;
}
}else{
passable = true;
}
}
if (passable == false){
System.out.println(parts[1] + " has Length: " + parts[i].length() + ", Value: " + parts[i] + " @ Index: " + i);
error++;
errorBreak = true;
break;
}
}
if (errorBreak == true){
continue;
}
//passing is done now add stuff to array
//String airline, airline id, String departureAirport, airport id, String arrivalAirport, arrival airport id, String codeShare, int stops, String equipment
routeAirName = parts[0];
routeDepart = parts[1];
routeArrive = parts[2];
routeCodeshare = parts[3];
routeStops = Integer.parseInt(parts[4]);
routeEquip = parts[5];
parsedRoutes.add(new Route(routeAirName, routeDepart, routeArrive, routeCodeshare, routeStops, routeEquip));
successful++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new DataException("File: " +this.filePath+" is Missing.");
} catch (IOException e) {
e.printStackTrace();
throw new DataException(this.filePath + " is Corrupted.");
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
throw new DataException(this.filePath + " is unable to initialise reader.");
}
}
return String.format("Entries Successfully Entered: %1$d.\n" +
"Entries With Errors: %2$d", successful, error);
}
public ArrayList<Route> getResult(){
return parsedRoutes;
}
}