Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.78 KB | None | 0 0
  1. import java.text.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Project3
  6. {
  7. /*Declare ArrayLists globally so they can be accessed in all methods*/
  8.  
  9. public static ArrayList<String> airportDetails = new ArrayList<String>();
  10. public static ArrayList<String> flightDetails = new ArrayList<String>();
  11.  
  12. /*Main method validates userInput. When calling the program the user must enter command line arguments. Depending on what they would like the program to do they enter a code e.g AA stands for Add Airport. The input is validated in main depending on which code they enter(as each code has different requirements). If it is valid it passes the userInput to the appropriate methods*/
  13.  
  14. public static void main(String [] args) throws IOException, ParseException
  15. {
  16. String pattern = "AA|EA|DA|EF|DF|SF|SD";
  17. String pattern1 = "[A-Za-z]{3}";
  18. String pattern2 = "[A-Za-z]{1,3}+[0-9]{1,4}";
  19. String pattern3 = "[A-Za-z]+";
  20. boolean validInput = true;
  21. filesToArrayLists();
  22. if(args.length < 2 || args.length > 5)
  23. displayMessage(0);
  24. else
  25. {
  26. args[0] = args[0].toUpperCase();
  27. if(!(args[0].matches(pattern)))
  28. displayMessage(1);
  29. else
  30. {
  31. switch(args[0])
  32. {
  33. case "AA":
  34. if(args.length != 3)
  35. displayMessage(0);
  36. else
  37. {
  38. if(args[2].length() != 3 || (!args[2].matches(pattern1)))
  39. {
  40. displayMessage(2);
  41. validInput = false;
  42. }
  43. if(validInput)
  44. {
  45. addAirportDetails(args[1].toUpperCase(),args[2].toUpperCase());
  46. }
  47. }
  48. break;
  49. case "EA":
  50. if(args.length != 3)
  51. displayMessage(0);
  52. else
  53. {
  54. if(args[1].length() != 3 || (!args[1].matches(pattern1)))
  55. {
  56. displayMessage(2);
  57. validInput = false;
  58. }
  59. if(validInput)
  60. {
  61. editAirportDetails(args[1].toUpperCase(),args[2].toUpperCase());
  62. }
  63. }
  64. break;
  65. case "DA":
  66. if(args.length != 2)
  67. displayMessage(0);
  68. else
  69. {
  70. if(args[1].length() != 3 || (!args[1].matches(pattern1)))
  71. {
  72. displayMessage(2);
  73. validInput = false;
  74. }
  75. if(validInput)
  76. deleteAirportDetails(args[1].toUpperCase());
  77. }
  78. break;
  79. case "EF":
  80. if(args.length != 5)
  81. displayMessage(0);
  82. else
  83. {
  84. if(args[3].length() < 8 || args[3].length() > 10 || args[4].length() < 8 || args[4].length() > 10)
  85. {
  86. displayMessage(6);
  87. validInput = false;
  88. }
  89. else if((!args[1].matches(pattern2)))
  90. {
  91. displayMessage(7);
  92. validInput = false;
  93. }
  94. else if(args[2].length() != 7)
  95. {
  96. displayMessage(8);
  97. validInput = false;
  98. }
  99. else
  100. {
  101.  
  102. boolean validDays = isValidDays(args[2]);
  103. boolean validOrder = validateDate(args[3],args[4]);
  104. boolean validDates1 = isValidDate(args[3]);
  105. boolean validDates2 = isValidDate(args[4]);
  106. if(!validOrder || !validDates1 || !validDates2)
  107. {
  108. displayMessage(10);
  109. validInput = false;
  110. }
  111. if(!validDays)
  112. {
  113. displayMessage(11);
  114. validInput = false;
  115. }
  116. if(validInput)
  117. editFlightDetails(args[1].toUpperCase(),args[2].toUpperCase(),args[3],args[4]);
  118.  
  119. }
  120. }
  121. break;
  122. case "DF":
  123. if(args.length != 2)
  124. displayMessage(0);
  125. else
  126. {
  127. if(!args[1].matches(pattern2))
  128. {
  129. displayMessage(7);
  130. validInput = false;
  131. }
  132. if(validInput)
  133. {
  134. deleteFlightDetails(args[1].toUpperCase());
  135. }
  136. }
  137. break;
  138. case "SF":
  139. if(args.length != 3)
  140. displayMessage(0);
  141. else
  142. {
  143. if(!args[1].matches(pattern3) || !args[2].matches(pattern3))
  144. {
  145. displayMessage(14);
  146. validInput = false;
  147. }
  148. if(validInput)
  149. showFlightDetails(args[1],args[2]);
  150. }
  151. break;
  152. case "SD":
  153. if(args.length != 4)
  154. displayMessage(0);
  155. else
  156. {
  157. if(!args[1].matches(pattern3) || !args[2].matches(pattern3))
  158. {
  159. displayMessage(14);
  160. validInput = false;
  161. }
  162. boolean validDate = isValidDate(args[3]);
  163. if(!validDate)
  164. {
  165. displayMessage(10);
  166. validInput = false;
  167. }
  168. if(validInput)
  169. showFlightDetailsOnDay(args[1],args[2],args[3]);
  170. }
  171. break;
  172.  
  173. }
  174. }
  175. }
  176. }
  177.  
  178. /*filesToArrayLists reads in both Airports.txt and Flights.txt and converts them to the two global arrayLists*/
  179.  
  180. public static void filesToArrayLists() throws IOException
  181. {
  182. Scanner in;
  183. String lineFromFile;
  184. File aFile = new File("Airports.txt");
  185. if(aFile.exists())
  186. {
  187. airportDetails = new ArrayList<String>();
  188. in = new Scanner(aFile);
  189. while(in.hasNext())
  190. {
  191. lineFromFile = in.nextLine();
  192. airportDetails.add(lineFromFile);
  193. }
  194. in.close();
  195. }
  196. aFile = new File("Flights.txt");
  197. if(aFile.exists())
  198. {
  199. flightDetails = new ArrayList<String>();
  200. in = new Scanner(aFile);
  201. while(in.hasNext())
  202. {
  203. lineFromFile = in.nextLine();
  204. flightDetails.add(lineFromFile);
  205. }
  206. in.close();
  207. }
  208. }
  209.  
  210. /* displayMessage prints out any errors or statements found while running the code, be it the userInput is not valid for what they wish to do or a file doesn't exist etc.*/
  211.  
  212. public static void displayMessage(int msgNum) //throws IOException
  213. {
  214. String msg = "";
  215. switch(msgNum)
  216. {
  217. case 0: msg = "Invalid number of command-line arguments.";
  218. break;
  219. case 1: msg = "Invalid first command-line argument.";
  220. break;
  221. case 2: msg = "Airport code must be three alphabetic characters in length.";
  222. break;
  223. case 3: msg = "File named Airports.txt does not exist.";
  224. break;
  225. case 4: msg = "Airport already exists.";
  226. break;
  227. case 5: msg = "Airport details added";
  228. break;
  229. case 6: msg = "Dates must be in the form dd/mm/yyyy";
  230. break;
  231. case 7: msg = "Format of airport code is incorrect";
  232. break;
  233. case 8: msg = "Days of the week must have 7 inputs(either letters or -)";
  234. break;
  235. case 9: msg = "Flight does not exist";
  236. break;
  237. case 10: msg = "Invalid date(s) entered";
  238. break;
  239. case 11: msg = "Format of travel days is incorrect";
  240. break;
  241. case 12: msg = "Flight details edited.";
  242. break;
  243. case 13: msg = "Flight details deleted.";
  244. break;
  245. case 14: msg = "Airport names must only contain alphabetic characters";
  246. break;
  247. case 15: msg = "Airport does not exist in Airports.txt";
  248. break;
  249. case 16: msg = "Airport records deleted";
  250. break;
  251. case 17: msg = "One or more of the airports entered does not exist in Airports.txt";
  252. break;
  253. case 18: msg = "All flights requested have been displayed";
  254. break;
  255. }
  256. System.out.println(msg);
  257. displayInstructions();
  258. }
  259.  
  260. /* displayInstructions is called from displayMessage and when the user gets their message be it an error or a statement they will then be presented with a list of instructions on how to go about adding/deleting from the files etc.*/
  261.  
  262. public static void displayInstructions()
  263. {
  264. String instructions = "Add airport:\tjava FlightManager AA Lisbon LIS";
  265. instructions += "Edit airport name:\tjava FlightManager EA BHD Belfast";
  266. instructions += "Delete airport:\tjava FlightManager DA SNN";
  267. instructions += "Edit flight details:\tjava FlightManager EF EI320 -TWTF-- 1/5/2017 31/10/2017";
  268. instructions += "Delete flight details:\tjava FlightManager DF EI320";
  269. instructions += "Display details of flight from one airport to another:\tjava FlightManager SF Dublin Shannon";
  270. instructions += "Display details of flight between airports on certain date:\tjava FlightManager SD Dublin Shannon 1/5/2017";
  271. System.out.println(instructions);
  272. }
  273.  
  274. /*validateDate tests to see if the start date entered actually occurs before the end date entered. If this is true it returns true to the main method*/
  275.  
  276. public static boolean validateDate(String date1, String date2)
  277. {
  278. boolean validInput = false;
  279. String d1[] = date1.split("/");
  280. String d2[] = date2.split("/");
  281. if(Integer.parseInt(d1[2]) < Integer.parseInt(d2[2]))
  282. validInput = true;
  283. else if(Integer.parseInt(d1[2]) == Integer.parseInt(d2[2]))
  284. {
  285. if(Integer.parseInt(d1[1]) < Integer.parseInt(d2[1]))
  286. validInput = true;
  287. else if(Integer.parseInt(d1[1]) == Integer.parseInt(d2[1]))
  288. {
  289. if(Integer.parseInt(d1[0]) < Integer.parseInt(d2[0]))
  290. validInput = true;
  291. }
  292. }
  293. return validInput;
  294. }
  295.  
  296. /* isValidDate tests if a particular date is valid depending on the year, month and day entered by the user. if the date is found to be valid the method will return true, otherwise it will return false*/
  297.  
  298. public static boolean isValidDate(String userInput)
  299. {
  300. String dateElements[];
  301. int ddInt, mmInt, yyInt;
  302. int[] daysArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  303. boolean dateIsValid = true;
  304. dateElements = userInput.split("/");
  305. ddInt = Integer.parseInt(dateElements[0]);
  306. mmInt = Integer.parseInt(dateElements[1]);
  307. yyInt = Integer.parseInt(dateElements[2]);
  308. if ((ddInt == 0) || (mmInt == 0) || (yyInt == 0))
  309. dateIsValid = false;
  310. else if (mmInt > 12)
  311. dateIsValid = false;
  312. else if ((ddInt == 29) && (mmInt == 2) && ((((yyInt % 4 == 0) && (yyInt % 100 != 0)) || (yyInt % 400 == 0))))
  313. dateIsValid = true;
  314. else if (ddInt > daysArray[mmInt - 1])
  315. dateIsValid = false;
  316. return dateIsValid;
  317. }
  318.  
  319. /* isValidDays tests to see if the days entered by the user for editing flight details is in the correct format(i.e either M or - for the first, T or - for the second etc). if the format is correct the method will return true, otherwise it will return false*/
  320.  
  321. public static boolean isValidDays(String days)
  322. {
  323. String[] daysPattern = {"M","T","W","T","F","S","S"};
  324. boolean validInput = true;
  325. String aCharacter;
  326. days = days.toUpperCase();
  327. int count = 0;
  328. for(int i = 0 ; i < 7 && validInput ; i++)
  329. {
  330. aCharacter = days.substring(i,i+1);
  331. if(!aCharacter.equals(daysPattern[i]) && !aCharacter.equals("-"))
  332. validInput = false;
  333. count++;
  334. }
  335. System.out.println(count);
  336. return validInput;
  337. }
  338.  
  339. /* addAirportDetails tests to see if the userInput already exists in airportDetails. If it exists it sends a message to the user, otherwise it adds it to the arrayList airportDetails and subsequently updates the file Airports.txt*/
  340.  
  341. public static void addAirportDetails(String airportName, String airportCode) throws IOException
  342. {
  343. boolean found = false;
  344. String aLine;
  345. String[] elements = new String[8];
  346. File aFile = new File("Airports.txt");
  347. PrintWriter output = new PrintWriter(aFile);
  348.  
  349. for(int i = 0; i < airportDetails.size() && !found; i++)
  350. {
  351. elements = airportDetails.get(i).split(",");
  352. if(elements[0].equals(airportName))
  353. found = true;
  354. }
  355. if(!found)
  356. {
  357. airportDetails.add(airportName+","+airportCode);
  358. Collections.sort(airportDetails);
  359. for(int i = 0; i < airportDetails.size(); i++)
  360. output.println(airportDetails.get(i));
  361. output.close();
  362. }
  363. else
  364. displayMessage(4);
  365. }
  366.  
  367. /* editAirportDetails tests to see if an airport code exists in Airports.txt, if it does it will update the name of the airport in Airports.txt with the name entered by the user*/
  368.  
  369. public static void editAirportDetails(String aCode, String aName) throws IOException
  370. {
  371. boolean found = false;
  372. int count = 0;
  373. String aLine;
  374. String[] elements = new String[2];
  375. File aFile = new File("Airports.txt");
  376. PrintWriter output = new PrintWriter(aFile);
  377. for(int i = 0; i < airportDetails.size() && !found; i++)
  378. {
  379. elements = airportDetails.get(i).split(",");
  380. if(elements[1].equals(aCode))
  381. found = true;
  382. count++;
  383. }
  384. if(found)
  385. {
  386. airportDetails.set(count-1, aName+","+aCode);
  387. Collections.sort(airportDetails);
  388. for(int i = 0; i < airportDetails.size(); i++)
  389. output.println(airportDetails.get(i));
  390. output.close();
  391. }
  392. else
  393. displayMessage(15);
  394. }
  395.  
  396. /* deleteAirportDetails tests to see if an airport code exists in Airports.txt, if it does it will delete the airport from the file and then go on to delete any flights in Flights.txt that are associated with that airport*/
  397.  
  398. public static void deleteAirportDetails(String aCode) throws IOException
  399. {
  400. boolean found = false;
  401. int count = 0;
  402. String aLine;
  403. String[] elements = new String[2];
  404. File aFile = new File("Airports.txt");
  405. PrintWriter output = new PrintWriter(aFile);
  406. for(int i = 0; i < airportDetails.size() && !found; i++)
  407. {
  408. elements = airportDetails.get(i).split(",");
  409. if(elements[1].equals(aCode))
  410. found = true;
  411. count++;
  412. }
  413. if(found)
  414. {
  415. airportDetails.remove(count-1);
  416. Collections.sort(airportDetails);
  417. for(int i = 0; i < airportDetails.size(); i++)
  418. output.println(airportDetails.get(i));
  419. }
  420. else
  421. displayMessage(15);
  422. output.close();
  423. String[] elements2 = new String[8];
  424. File bFile = new File("Flights.txt");
  425. PrintWriter output2 = new PrintWriter(bFile);
  426. for(int i = 0; i < flightDetails.size(); i++)
  427. {
  428. found = false;
  429. elements = flightDetails.get(i).split(",");
  430. if(elements[1].equals(aCode) || elements[2].equals(aCode))
  431. found = true;
  432. if(found)
  433. flightDetails.remove(i);
  434. }
  435. for(int i = 0; i < flightDetails.size(); i++)
  436. output2.println(flightDetails.get(i));
  437. output2.close();
  438. if(found)
  439. displayMessage(16);
  440. }
  441.  
  442. /* editFlightDetails searches the flightDetails arrayList for the flight code entered by the user, if the flight code exists it will update the information for that flight with the userInput in the arrayList and subsequently update Flights.txt*/
  443.  
  444. public static void editFlightDetails(String flightCode, String days, String startDate, String endDate) throws IOException
  445. {
  446. boolean found = false;
  447. int count = 0;
  448. String aLine;
  449. String[] elements = new String[8];
  450. File aFile = new File("Flights.txt");
  451. PrintWriter output = new PrintWriter(aFile);
  452. for(int i = 0; i < flightDetails.size() && !found; i++)
  453. {
  454. elements = flightDetails.get(i).split(",");
  455. if(elements[0].equals(flightCode))
  456. found = true;
  457. count++;
  458. }
  459. if(found)
  460. {
  461. flightDetails.set(count-1,flightCode+","+elements[1]+","+elements[2]+","+elements[3]+","+elements[4]+","+days+","+startDate+","+endDate);
  462. Collections.sort(flightDetails);
  463. for(int i = 0; i < flightDetails.size(); i++)
  464. output.println(flightDetails.get(i));
  465. output.close();
  466. displayMessage(12);
  467. }
  468. else
  469. displayMessage(9);
  470. }
  471.  
  472. /* deleteFlightDetails searches the flightDetails ArrayList for the flight code entered by the user, if it exists the method will remove the entry from the arrayList and subsequently update Flights.txt*/
  473.  
  474. public static void deleteFlightDetails(String flightCode) throws IOException
  475. {
  476. boolean found = false;
  477. int count = 0;
  478. String aLine;
  479. String[] elements = new String[8];
  480. File aFile = new File("Flights.txt");
  481. PrintWriter output = new PrintWriter(aFile);
  482. for(int i = 0; i < flightDetails.size() && !found; i++)
  483. {
  484. elements = flightDetails.get(i).split(",");
  485. if(elements[0].equals(flightCode))
  486. found = true;
  487. count++;
  488. }
  489. if(found)
  490. {
  491. flightDetails.remove(count-1);
  492. Collections.sort(flightDetails);
  493. for(int i = 0; i < flightDetails.size(); i++)
  494. output.println(flightDetails.get(i));
  495. output.close();
  496. displayMessage(13);
  497. }
  498. else
  499. displayMessage(9);
  500. }
  501.  
  502. /* showFlightDetails will display all the flights that go from the departure airport entered to the arrival airport entered by the user*/
  503.  
  504. public static void showFlightDetails(String depAirport, String arrAirport) throws IOException
  505. {
  506. boolean depFound = false, arrFound = false;
  507. boolean code1Found = false, code2Found = false;
  508. int depCount = 0, arrCount = 0;
  509. String aLine;
  510. String depCode = "", arrCode = "";
  511. String[] tempAirports = new String[2];
  512. String[] tempFlights = new String[8];
  513. for(int i = 0; i < airportDetails.size() && !depFound ; i++)
  514. {
  515. tempAirports = airportDetails.get(i).split(",");
  516. if(tempAirports[0].equalsIgnoreCase(depAirport))
  517. {
  518. depFound = true;
  519. depCode = tempAirports[1];
  520. }
  521. }
  522. for(int i = 0; i < airportDetails.size() && !arrFound && depFound ; i++)
  523. {
  524. tempAirports = airportDetails.get(i).split(",");
  525. if(tempAirports[0].equalsIgnoreCase(arrAirport))
  526. {
  527. arrFound = true;
  528. arrCode = tempAirports[1];
  529. }
  530. }
  531. if( !depFound || !arrFound)
  532. displayMessage(17);
  533. else
  534. {
  535. System.out.println("FlightCode\tDeparture Time\tArrival Time\tDays\t\tStart Date\t\tEnd Date");
  536. for(int i = 0; i < flightDetails.size() ; i++)
  537. {
  538. code1Found = false;
  539. code2Found = false;
  540. tempFlights = flightDetails.get(i).split(",");
  541. if(tempFlights[1].equals(depCode))
  542. code1Found = true;
  543. if(tempFlights[2].equals(arrCode))
  544. code2Found = true;
  545. if(code1Found && code2Found)
  546. {
  547. System.out.print(tempFlights[0]);
  548. for(int j = 3; j < tempFlights.length; j++)
  549. System.out.print("\t\t"+tempFlights[j]);
  550. System.out.println();
  551. }
  552. }
  553. displayMessage(18);
  554. }
  555. }
  556.  
  557. /* showFlightDetails will display all the flights that go from the departure airport entered to the arrival airport entered by the user on a specific date also entered by the user. To do this it checks to see if the date entered is withtin the period of flying(found in Flights.txt) and also if that date is on one of the days specified in Flights.txt*/
  558.  
  559. public static void showFlightDetailsOnDay(String depAirport, String arrAirport, String date) throws ParseException
  560. {
  561. boolean depFound = false, arrFound = false;
  562. boolean code1Found = false, code2Found = false;
  563. boolean dateAfter = false, dateBefore = false;
  564. boolean dayOfTravel = false;
  565. int depCount = 0, arrCount = 0;
  566. String aLine;
  567. String depCode = "", arrCode = "";
  568. String[] tempAirports = new String[2];
  569. String[] tempFlights = new String[8];
  570. String[] days = new String[7];
  571.  
  572. for(int i = 0; i < airportDetails.size() && !depFound ; i++)
  573. {
  574. tempAirports = airportDetails.get(i).split(",");
  575. if(tempAirports[0].equalsIgnoreCase(depAirport))
  576. {
  577. depFound = true;
  578. depCode = tempAirports[1];
  579. }
  580. }
  581. for(int i = 0; i < airportDetails.size() && !arrFound && depFound ; i++)
  582. {
  583. tempAirports = airportDetails.get(i).split(",");
  584. if(tempAirports[0].equalsIgnoreCase(arrAirport))
  585. {
  586. arrFound = true;
  587. arrCode = tempAirports[1];
  588. }
  589. }
  590. if( !depFound || !arrFound)
  591. displayMessage(17);
  592. else
  593. {
  594. System.out.println("FlightCode\tDeparture Time\tArrival Time\tDays\t\tStart Date\t\tEnd Date");
  595. for(int i = 0; i < flightDetails.size() ; i++)
  596. {
  597. code1Found = false;
  598. code2Found = false;
  599. tempFlights = flightDetails.get(i).split(",");
  600. if(tempFlights[1].equals(depCode))
  601. code1Found = true;
  602. if(tempFlights[2].equals(arrCode))
  603. code2Found = true;
  604. dateAfter = validateDate(tempFlights[6], date);
  605. dateBefore = validateDate(date, tempFlights[7]);
  606. if(dateBefore && dateAfter)
  607. {
  608. GregorianCalendar aCalendar = new GregorianCalendar();
  609. DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
  610. Date newdate = df.parse(date);
  611. aCalendar.setTime(newdate);
  612. int day = aCalendar.get(Calendar.DAY_OF_WEEK);
  613. day = day - 2;
  614. if(day == -1)
  615. day = 6;
  616. for(int j = 0 ; i < 7 ; i++)
  617. {
  618. days[j] = flightDetails.get(5).substring(j,j+1);
  619. }
  620. if(!days[day].equals("-"))
  621. dayOfTravel = true;
  622. }
  623. if(code1Found && code2Found && dayOfTravel)
  624. {
  625. //System.out.println("FlightCode\tDeparture Time\tArrival Time\tDays Of The Week\tStart Date\tEnd Date");
  626. System.out.print(tempFlights[0]);
  627. for(int j = 3; j < tempFlights.length; j++)
  628. System.out.print("\t\t"+tempFlights[j]);
  629. System.out.println();
  630. }
  631.  
  632. }
  633. displayMessage(18);
  634. }
  635. }
  636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement