Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /*
  2. John Long
  3. -Checks for a valid date
  4. */
  5. public static boolean isValidDate(String userInput)
  6. {
  7. String dateElements[];
  8. int ddInt, mmInt, yyInt;
  9. int[] daysArray = {31,28,31,30,31,30,31,31,30,31,30,31};
  10. boolean dateIsValid = true;
  11. dateElements = userInput.split("/");
  12. ddInt = Integer.parseInt(dateElements[0]);
  13. mmInt = Integer.parseInt(dateElements[1]);
  14. yyInt = Integer.parseInt(dateElements[2]);
  15. if((ddInt == 0) || (mmInt == 0) || (yyInt == 0))
  16. dateIsValid = false;
  17. else if(mmInt > 12)
  18. dateIsValid = false;
  19. else if((ddInt == 29) && (mmInt == 2) && ((((yyInt % 4 == 0) && (yyInt % 100 != 0)) || (yyInt % 400 = 0))))
  20. dateIsValid = true;
  21. else if(ddInt > daysArray[mmInt - 1])
  22. dateIsValid = false;
  23. return dateIsValid;
  24. }
  25.  
  26. /*
  27. John Long
  28. -This method searches for flights by departing airport and destination airport and prints the details of all
  29. relevant flights
  30. -It takes 2 arguments, the source and destination airports as Strings
  31. -It returns void
  32. */
  33. public static void searchFlightsSF(String source, String destination)
  34. {
  35. String aSource = "";
  36. String aDestination = "";
  37. int matches = 0;
  38.  
  39. aSource = getAirportCode(source);
  40. aDestination = getAirportCode(destination);
  41.  
  42. for(int i=0; i<flights.get(0).size(); i++)
  43. {
  44. if(aSource.equalsIgnoreCase(flights.get(1).get(i)) && aDestination.equalsIgnoreCase(flights.get(2).get(i)))
  45. {
  46. // Display flight details
  47. for(int j=0; j<8; j++)
  48. {
  49. System.out.print(flights.get(j).get(i));
  50. }
  51. System.out.println("");
  52. matches++;
  53. }
  54. }
  55. if (matches == 0)
  56. displayMessage(10);
  57.  
  58. }
  59.  
  60. /*
  61. John Long
  62. -This method searches for flights by departing airport, destination airport and departure date and prints the details of all
  63. relevant flights
  64. -It takes 3 arguments, the source and destination airports as Strings and the departure date as a String
  65. -It returns void
  66. */
  67.  
  68. public static void searchFlightsSD(String source, String destination, String date) throws ParseException
  69. {
  70. String aSource = "";
  71. String aDestination = "";
  72. int matches = 0;
  73.  
  74. SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
  75. Date date1 = sdf.parse(date);
  76.  
  77. aSource = getAirportCode(source);
  78. aDestination = getAirportCode(destination);
  79. boolean display1;
  80. boolean display2;
  81.  
  82. for(int i=0; i<flights.get(0).size(); i++)
  83. {
  84. Date date2 = sdf.parse(flights.get(6).get(i));
  85. display1 = false;
  86. display2 = false;
  87. if(aSource.equalsIgnoreCase(flights.get(1).get(i)) && aDestination.equalsIgnoreCase(flights.get(2).get(i)))
  88. {
  89. display1 = true;
  90. }
  91.  
  92. if(date1.equals(date2))
  93. display2 = true;
  94.  
  95. if(display1 == true && display2 == true)
  96. {
  97. // Display flight details
  98. for(int j=0; j<8; j++)
  99. {
  100. System.out.print(flights.get(j).get(i));
  101. }
  102. System.out.println("");
  103. matches++;
  104. }
  105. }
  106.  
  107. if(matches==0)
  108. displayMessage(10);
  109. }
  110.  
  111. /*
  112. John Long
  113. -This method gets the airport code for a particular airport name
  114. -It returns a String and takes 1 parameter, a String which is the name of the airport
  115. */
  116. public static String getAirportCode(String name)
  117. {
  118. boolean found = false;
  119. String airportCode = "";
  120. for(int i = 0; i<airports.get(0).size() && !found; i++)
  121. {
  122. if(name.equalsIgnoreCase(airports.get(0).get(i)))
  123. {
  124. airportCode = airports.get(0).get(i);
  125. found = true;
  126. }
  127. }
  128. return airportCode;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement