Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1.  
  2.     /*Daire Lavelle-16192249
  3.     -This method takes the name of the departure airport, the name of the desination airport and the date as arguments.
  4.    
  5.     -The flights ArrayList is checked to see if the airports are referenced in it. If both are, the flights file is looped over.
  6.     If a flight is found on the given date, from the departure airport to the destination aiport, it is added to the validFlights
  7.     String. If an airport is not found in the airports ArrayList an error message is displayed. If no valid flights are found
  8.     given the arguments an error message is displayed.
  9.    
  10.     -The method has no return type
  11.     */
  12.     public static void showFlightsForGivenDate(String departure, String destination, String date)
  13.     {
  14.         boolean found=false;
  15.         int departureIndex=airports.get(0).indexOf(departure);
  16.         int destinationIndex=airports.get(0).indexOf(destination);
  17.         if (departureIndex==-1 || destinationIndex==-1)
  18.             System.out.print("one or more airports were not found in the flights file");
  19.         else
  20.             found=true;
  21.        
  22.         if (found)
  23.         {
  24.             String departureCode=airports.get(1).get(departureIndex);
  25.             String destinationCode=airports.get(1).get(destinationIndex);
  26.             String validFlights="";
  27.             boolean departureBool, destinationBool, dateBool;
  28.             for (int i=0; i<flights.size(); i++)
  29.             {
  30.                 String temp="";
  31.                 departureBool=flights.get(1).get(i).equals(departureCode);
  32.                 destinationBool=flights.get(2).get(i).equals(destinationCode);
  33.                 dateBool=flights.get(6).get(i).equals(date);
  34.                 if (departureBool && destinationBool && dateBool)
  35.                 {
  36.                     temp+="Flight "+flights.get(0).get(i)+" ";
  37.                     temp+="departs "+departure+" at "+flights.get(3).get(i)+" ";
  38.                     temp+="and arrives in "+destination+" at "+flights.get(4).get(i)+" ";
  39.                     temp+="on "+date+". The return date is "+flights.get(7).get(i)+".\n";
  40.                 }
  41.                 validFlights+=temp;
  42.             }
  43.             if(validFlights.equals(""))
  44.                 System.out.print("There are no flights from "+departure+" to "+destination+" on "+date);
  45.             else
  46.                 System.out.print(validFlights);
  47.         }
  48.     }
  49.    
  50.     /*Daire Lavelle-16192249
  51.     -This method takes the name and code of an airport as arguments.
  52.    
  53.     -The airports ArrayList is looped over in order to insert the airport such that the file will be in alphabetic order.
  54.     If the airport name is the sameas one in the ArrayList a message is displayed saying that the airport is already registerd.
  55.     It then calls the writeToAirportsFile method if an airport was added.
  56.    
  57.     -The method has no return type
  58.     */
  59.     public static void addAirportToArrayList(String airportName, String airportCode)    throws IOException
  60.     {
  61.         boolean found=false;
  62.         if (airports.get(0).size()==0)
  63.         {
  64.             airports.get(0).add(airportName);
  65.             airports.get(1).add(airportCode);
  66.             found=true;
  67.         }
  68.         else
  69.         {
  70.             boolean finished=false;
  71.             for (int i=0; i<airports.get(0).size() && !finished; i++)
  72.             {
  73.                 if (airports.get(0).get(i).compareToIgnoreCase(airportName)==0)
  74.                 {
  75.                     finished=true;
  76.                     System.out.println(airportName+" is already registerd.");
  77.                 }
  78.                 else if (airports.get(0).get(i).compareToIgnoreCase(airportName)>0)
  79.                 {
  80.                     airports.get(0).add(i,airportName);
  81.                     airports.get(1).add(i,airportCode);
  82.                     finished=true;
  83.                     found=true;
  84.                 }
  85.                 else if (i==airports.get(0).size()-1)
  86.                 {
  87.                     airports.get(0).add(airportName);
  88.                     airports.get(1).add(airportCode);
  89.                     finished=true;
  90.                     found=true;
  91.                 }
  92.             }
  93.         }
  94.         if (found)
  95.             writeToAirportsFile();
  96.  
  97.     }
  98.    
  99.    
  100.     /*
  101.     Daire Lavelle-16192249
  102.     -This method takes the code of the airport the end user wishes to delete from the file as its argument
  103.    
  104.     -The findIndexOfCode method is called to check if the airport exists in the ArrayList. If the airport is found corresponding
  105.     elements from the airports and flights Arraylists are deleted. writeToAirportsFile and writeToFlightsFile are called. If
  106.     the airport isn't found a message is displayed.
  107.    
  108.     -The method has no return type.
  109.     */
  110.     public static void deleteAirportFromArrayList(String airportCode)   throws IOException
  111.     {
  112.         airportCode=airportCode.toUpperCase();
  113.         int airportCodeIndex = findIndexOfCode(airportCode);
  114.         String flightsDeleted="";
  115.         boolean flightsExist=false;
  116.         if (airportCodeIndex>=0)
  117.         {
  118.             String airportName=airports.get(0).get(airportCodeIndex);
  119.             airports.get(0).remove(airportCodeIndex);
  120.             airports.get(1).remove(airportCodeIndex);
  121.             writeToAirportsFile();
  122.            
  123.             for (int i=0; i<flights.get(0).size();)
  124.             {
  125.                 if (flights.get(1).get(i).equals(airportCode) || flights.get(2).get(i).equals(airportCode))
  126.                 {
  127.                     flightsExist=true;
  128.                     flightsDeleted+="\n"+flights.get(0).get(i);
  129.                     for (int j=0; j<flights.size(); j++)
  130.                     {
  131.                         flights.get(j).remove(i);
  132.                     }
  133.                 }
  134.                 else
  135.                     i++;
  136.             }
  137.             System.out.println(airportName+" has been deleted from Airports.txt");
  138.             if (flightsExist)
  139.                 System.out.println("The following flights have been deleted from Flights.txt:"+flightsDeleted);
  140.             writeToFlightsFile();
  141.         }
  142.         else
  143.             System.out.print("Airport not found");
  144.     }
  145.    
  146.     /*
  147.     Daire Lavelle-16192249
  148.     -This method takes a flight code as its argument
  149.     -It calls the findIndexOfCode method to check if the code exists in the flights ArrayList and if it does, get its index.
  150.     If the flight exists it is deleted from flights and flights is written to Flights.txt. Otherwise a messages is displayed
  151.     to the end user.
  152.     -The method has no return type
  153.     */
  154.     public static void deleteFlightDetailsFromArrayList(String flightCode) throws IOException
  155.     {
  156.         int flightCodeIndex=findIndexOfFlightCode(flightCode);
  157.         if (flightCodeIndex>=0)
  158.         {
  159.             for (int i=0; i<flights.size(); i++)
  160.                 flights.get(i).remove(flightCodeIndex);
  161.             System.out.println("Flight "+flightCode+" has been deleted from Flights.txt");
  162.             writeToFlightsFile();
  163.         }
  164.         else
  165.             System.out.print("Flight not found");
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement