Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. // 2 .Wyszukanie połączenia (może być z przesiadkami) w określonym dniu\
  2. static List<Flight> searchFlightAtDay(String townFrom, String townTo, LocalDate date) {
  3. if (townFrom == null || townTo == null || date == null) {
  4. throw new IllegalArgumentException("Niepoporawne miasto lub data");
  5. }
  6. List<Flight> flightWithChange = new ArrayList<>();
  7. for (int i = 0; i < flights.size(); i++) {
  8. if (flights.get(i).getFrom().getTown().equalsIgnoreCase(townFrom)
  9. && flights.get(i).getDeparture().toLocalDate().equals(date)) {
  10. for (int j = 0; j < flights.size(); j++) {
  11. if (flights.get(j).getFrom().getTown().equals(flights.get(i).getDestination().getTown())
  12. && flights.get(j).getDeparture().toLocalDate().equals(date)) {
  13. if (flights.get(i).getArrival().isBefore(flights.get(j).getDeparture())) {
  14. flightWithChange.add(flights.get(i));
  15. flightWithChange.add(flights.get(j));
  16. }
  17. }
  18. }
  19. }
  20. }
  21.  
  22. return flightWithChange;
  23. }
  24.  
  25. private static List<Flight> searchDirectConnectionAtDay(String townFrom, String townTo, LocalDate date) {
  26. if (townFrom == null || townTo == null || date == null) {
  27. throw new IllegalArgumentException("Niepoporawne miasto lub data");
  28. }
  29. return flights
  30. .stream()
  31. .filter(f -> f.getFrom().getTown().equalsIgnoreCase(townFrom) &&
  32. f.getDestination().getTown().equalsIgnoreCase(townTo) &&
  33. f.getDeparture().toLocalDate().equals(date))
  34. .collect(Collectors.toList());
  35. }
  36.  
  37. public static Flight[] fromListToArray(List<Flight> flightList) {
  38. Flight[] array = new Flight[flightList.size()];
  39. for (int i = 0; i < array.length; i++) {
  40. array[i] = flightList.get(i);
  41. }
  42. return array;
  43. }
  44.  
  45. public static Map<String, Flight[]> mapFlight(String townFrom, String townTo, LocalDate date) {
  46. HashMap<String, Flight[]> flightMap = new HashMap<>();
  47. List<Flight> directFlights = searchDirectConnectionAtDay(townFrom,townTo,date);
  48. List<Flight> flightWithChange = searchFlightAtDay(townFrom,townTo,date);
  49. flightMap.put("DIRECT", fromListToArray(directFlights));
  50. flightMap.put("WITH CHANGE", fromListToArray(flightWithChange));
  51. return flightMap;
  52. }
  53.  
  54. public static Map<String, String> formatMapFlight(String townFrom, String townTo, LocalDate date) {
  55. HashMap<String, String> flightMap = new HashMap<>();
  56. List<Flight> directFlights = searchDirectConnectionAtDay(townFrom,townTo,date);
  57. List<Flight> flightWithChange = searchFlightAtDay(townFrom,townTo,date);
  58. flightMap.put("DIRECT", Arrays.toString(fromListToArray(directFlights)));
  59. flightMap.put("WITH CHANGE", Arrays.toString(fromListToArray(flightWithChange)));
  60. return flightMap;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement