Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Daire Lavelle-16192249
- -This method takes the name of the departure airport, the name of the desination airport and the date as arguments.
- -The flights ArrayList is checked to see if the airports are referenced in it. If both are, the flights file is looped over.
- If a flight is found on the given date, from the departure airport to the destination aiport, it is added to the validFlights
- String. If an airport is not found in the airports ArrayList an error message is displayed. If no valid flights are found
- given the arguments an error message is displayed.
- -The method has no return type
- */
- public static void showFlightsForGivenDate(String departure, String destination, String date)
- {
- boolean found=false;
- int departureIndex=airports.get(0).indexOf(departure);
- int destinationIndex=airports.get(0).indexOf(destination);
- if (departureIndex==-1 || destinationIndex==-1)
- System.out.print("one or more airports were not found in the flights file");
- else
- found=true;
- if (found)
- {
- String departureCode=airports.get(1).get(departureIndex);
- String destinationCode=airports.get(1).get(destinationIndex);
- String validFlights="";
- boolean departureBool, destinationBool, dateBool;
- for (int i=0; i<flights.size(); i++)
- {
- String temp="";
- departureBool=flights.get(1).get(i).equals(departureCode);
- destinationBool=flights.get(2).get(i).equals(destinationCode);
- dateBool=flights.get(6).get(i).equals(date);
- if (departureBool && destinationBool && dateBool)
- {
- temp+="Flight "+flights.get(0).get(i)+" ";
- temp+="departs "+departure+" at "+flights.get(3).get(i)+" ";
- temp+="and arrives in "+destination+" at "+flights.get(4).get(i)+" ";
- temp+="on "+date+". The return date is "+flights.get(7).get(i)+".\n";
- }
- validFlights+=temp;
- }
- if(validFlights.equals(""))
- System.out.print("There are no flights from "+departure+" to "+destination+" on "+date);
- else
- System.out.print(validFlights);
- }
- }
- /*Daire Lavelle-16192249
- -This method takes the name and code of an airport as arguments.
- -The airports ArrayList is looped over in order to insert the airport such that the file will be in alphabetic order.
- If the airport name is the sameas one in the ArrayList a message is displayed saying that the airport is already registerd.
- It then calls the writeToAirportsFile method if an airport was added.
- -The method has no return type
- */
- public static void addAirportToArrayList(String airportName, String airportCode) throws IOException
- {
- boolean found=false;
- if (airports.get(0).size()==0)
- {
- airports.get(0).add(airportName);
- airports.get(1).add(airportCode);
- found=true;
- }
- else
- {
- boolean finished=false;
- for (int i=0; i<airports.get(0).size() && !finished; i++)
- {
- if (airports.get(0).get(i).compareToIgnoreCase(airportName)==0)
- {
- finished=true;
- System.out.println(airportName+" is already registerd.");
- }
- else if (airports.get(0).get(i).compareToIgnoreCase(airportName)>0)
- {
- airports.get(0).add(i,airportName);
- airports.get(1).add(i,airportCode);
- finished=true;
- found=true;
- }
- else if (i==airports.get(0).size()-1)
- {
- airports.get(0).add(airportName);
- airports.get(1).add(airportCode);
- finished=true;
- found=true;
- }
- }
- }
- if (found)
- writeToAirportsFile();
- }
- /*
- Daire Lavelle-16192249
- -This method takes the code of the airport the end user wishes to delete from the file as its argument
- -The findIndexOfCode method is called to check if the airport exists in the ArrayList. If the airport is found corresponding
- elements from the airports and flights Arraylists are deleted. writeToAirportsFile and writeToFlightsFile are called. If
- the airport isn't found a message is displayed.
- -The method has no return type.
- */
- public static void deleteAirportFromArrayList(String airportCode) throws IOException
- {
- airportCode=airportCode.toUpperCase();
- int airportCodeIndex = findIndexOfCode(airportCode);
- String flightsDeleted="";
- boolean flightsExist=false;
- if (airportCodeIndex>=0)
- {
- String airportName=airports.get(0).get(airportCodeIndex);
- airports.get(0).remove(airportCodeIndex);
- airports.get(1).remove(airportCodeIndex);
- writeToAirportsFile();
- for (int i=0; i<flights.get(0).size();)
- {
- if (flights.get(1).get(i).equals(airportCode) || flights.get(2).get(i).equals(airportCode))
- {
- flightsExist=true;
- flightsDeleted+="\n"+flights.get(0).get(i);
- for (int j=0; j<flights.size(); j++)
- {
- flights.get(j).remove(i);
- }
- }
- else
- i++;
- }
- System.out.println(airportName+" has been deleted from Airports.txt");
- if (flightsExist)
- System.out.println("The following flights have been deleted from Flights.txt:"+flightsDeleted);
- writeToFlightsFile();
- }
- else
- System.out.print("Airport not found");
- }
- /*
- Daire Lavelle-16192249
- -This method takes a flight code as its argument
- -It calls the findIndexOfCode method to check if the code exists in the flights ArrayList and if it does, get its index.
- If the flight exists it is deleted from flights and flights is written to Flights.txt. Otherwise a messages is displayed
- to the end user.
- -The method has no return type
- */
- public static void deleteFlightDetailsFromArrayList(String flightCode) throws IOException
- {
- int flightCodeIndex=findIndexOfFlightCode(flightCode);
- if (flightCodeIndex>=0)
- {
- for (int i=0; i<flights.size(); i++)
- flights.get(i).remove(flightCodeIndex);
- System.out.println("Flight "+flightCode+" has been deleted from Flights.txt");
- writeToFlightsFile();
- }
- else
- System.out.print("Flight not found");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement