Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2 .Wyszukanie połączenia (może być z przesiadkami) w określonym dniu\
- static List<Flight> searchFlightAtDay(String townFrom, String townTo, LocalDate date) {
- if (townFrom == null || townTo == null || date == null) {
- throw new IllegalArgumentException("Niepoporawne miasto lub data");
- }
- List<Flight> flightWithChange = new ArrayList<>();
- for (int i = 0; i < flights.size(); i++) {
- if (flights.get(i).getFrom().getTown().equalsIgnoreCase(townFrom)
- && flights.get(i).getDeparture().toLocalDate().equals(date)) {
- for (int j = 0; j < flights.size(); j++) {
- if (flights.get(j).getFrom().getTown().equals(flights.get(i).getDestination().getTown())
- && flights.get(j).getDeparture().toLocalDate().equals(date)) {
- if (flights.get(i).getArrival().isBefore(flights.get(j).getDeparture())) {
- flightWithChange.add(flights.get(i));
- flightWithChange.add(flights.get(j));
- }
- }
- }
- }
- }
- return flightWithChange;
- }
- private static List<Flight> searchDirectConnectionAtDay(String townFrom, String townTo, LocalDate date) {
- if (townFrom == null || townTo == null || date == null) {
- throw new IllegalArgumentException("Niepoporawne miasto lub data");
- }
- return flights
- .stream()
- .filter(f -> f.getFrom().getTown().equalsIgnoreCase(townFrom) &&
- f.getDestination().getTown().equalsIgnoreCase(townTo) &&
- f.getDeparture().toLocalDate().equals(date))
- .collect(Collectors.toList());
- }
- public static Flight[] fromListToArray(List<Flight> flightList) {
- Flight[] array = new Flight[flightList.size()];
- for (int i = 0; i < array.length; i++) {
- array[i] = flightList.get(i);
- }
- return array;
- }
- public static Map<String, Flight[]> mapFlight(String townFrom, String townTo, LocalDate date) {
- HashMap<String, Flight[]> flightMap = new HashMap<>();
- List<Flight> directFlights = searchDirectConnectionAtDay(townFrom,townTo,date);
- List<Flight> flightWithChange = searchFlightAtDay(townFrom,townTo,date);
- flightMap.put("DIRECT", fromListToArray(directFlights));
- flightMap.put("WITH CHANGE", fromListToArray(flightWithChange));
- return flightMap;
- }
- public static Map<String, String> formatMapFlight(String townFrom, String townTo, LocalDate date) {
- HashMap<String, String> flightMap = new HashMap<>();
- List<Flight> directFlights = searchDirectConnectionAtDay(townFrom,townTo,date);
- List<Flight> flightWithChange = searchFlightAtDay(townFrom,townTo,date);
- flightMap.put("DIRECT", Arrays.toString(fromListToArray(directFlights)));
- flightMap.put("WITH CHANGE", Arrays.toString(fromListToArray(flightWithChange)));
- return flightMap;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement