Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  1. package com.flight.quick.quickflight.util;
  2.  
  3. import com.flight.quick.quickflight.dto.ShortestPath;
  4. import com.flight.quick.quickflight.dto.ShortestPathRequest;
  5. import com.flight.quick.quickflight.graph.DirectedGraph;
  6. import com.flight.quick.quickflight.graph.Graph;
  7. import com.flight.quick.quickflight.model.Airport;
  8. import com.flight.quick.quickflight.model.Route;
  9. import com.flight.quick.quickflight.service.AirportService;
  10. import com.flight.quick.quickflight.service.RouteService;
  11.  
  12. import java.util.*;
  13.  
  14. public class FlightFinderOld {
  15.  
  16.     private final AirportService airportService;
  17.  
  18.     private final RouteService routeService;
  19.  
  20.     private final String source;
  21.  
  22.     private Set<String> avoidCities = new HashSet<>();
  23.  
  24.     private Map<String, Airport> airports = new HashMap<>();
  25.  
  26.     private List<Route> routes = new ArrayList<>();
  27.  
  28.     private Graph graph = new DirectedGraph();
  29.  
  30.     private ShortestPathRequest shortestPathRequest;
  31.  
  32.     public FlightFinderOld(AirportService airportService, RouteService routeService, ShortestPathRequest shortestPathRequest, String source) {
  33.         this.airportService = airportService;
  34.         this.routeService = routeService;
  35.         this.source = source;
  36.         this.shortestPathRequest = shortestPathRequest;
  37.  
  38.         loadAirports();
  39.         loadRoutes();
  40.         loadAvoidedCities();
  41.  
  42.         buildGraph();
  43.     }
  44.  
  45.     private void loadAvoidedCities() {
  46.         for (Airport avoidedAirport : shortestPathRequest.getAvoid()) {
  47.             avoidCities.add(avoidedAirport.getIata());
  48.         }
  49.     }
  50.  
  51.     private void loadAirports() {
  52.         ArrayList<Airport> airportList = airportService.getAllAirports();
  53.  
  54.         for(Airport airport: airportList) {
  55.             airports.put(airport.getIata(), airport);
  56.         }
  57.     }
  58.  
  59.     private void loadRoutes() {
  60.         ArrayList<Route> routesList = routeService.getAllRoutesBySource(source);
  61.  
  62.         for(Route route: routesList) {
  63.             routes.add(route);
  64.         }
  65.     }
  66.  
  67.     private void buildGraph() {
  68.         for (Route route: routes) {
  69.             Airport origin = airports.get(route.getFromAirport());
  70.             Airport destination = airports.get(route.getToAirport());
  71.  
  72.             Double airDistance = AirportAirDistance.calculateAirDistance(origin, destination);
  73.  
  74.             graph.addEdge(origin, destination, airDistance);
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Find shortest path between cities given in program argument.
  80.      *
  81.      * Also avoid cities that are passed through program arguments and
  82.      * visit city passed through program arguments
  83.      */
  84.     public ShortestPath findShortestPath() {
  85.         Airport origin = airports.get(shortestPathRequest.getStart().getIata());
  86.         Airport destination = airports.get(shortestPathRequest.getEnd().getIata());
  87.         Airport via = null;
  88.  
  89.         if (shortestPathRequest.getVia() != null) {
  90.             via = airports.get(shortestPathRequest.getVia().getIata());
  91.         }
  92.  
  93.         if(via == null) {
  94.             return findPathWithoutVia(origin, destination);
  95.         } else {
  96.             return findPathWithVia(origin, destination, via);
  97.         }
  98.     }
  99.  
  100.     private ShortestPath findPathWithVia(Airport origin, Airport destination, Airport via) {
  101.         List<Airport> pathToVia = findPathFromOriginToVia(origin, via);
  102.  
  103.         List<Airport> pathToDestination = findPathFromViaToDestination(destination, via);
  104.  
  105.         if (pathToVia.size() == 0 || pathToDestination.size() == 0) {
  106.             return new ShortestPath();
  107.         } else {
  108.  
  109.             ArrayList<Airport> path = new ArrayList<>();
  110.             path.addAll(pathToVia);
  111.             path.addAll(pathToDestination);
  112.  
  113.             Double total = calculateDistance(path);
  114.  
  115.             return new ShortestPath(path, total);
  116.         }
  117.     }
  118.  
  119.     private Double calculateDistance(ArrayList<Airport> path) {
  120.         Double totalDistance = 0.00;
  121.  
  122.         for (int i = 0; i < path.size() - 1; i++) {
  123.             totalDistance += graph.getWeight(path.get(i), path.get(i + 1));
  124.         }
  125.  
  126.         return totalDistance;
  127.     }
  128.  
  129.     private List<Airport> findPathFromViaToDestination(Airport destination, Airport via) {
  130.         List<Airport> pathToDestination = graph.findShortestPath(via, destination, avoidCities);
  131.  
  132.         Collections.reverse(pathToDestination);
  133.         return pathToDestination;
  134.     }
  135.  
  136.     private List<Airport> findPathFromOriginToVia(Airport origin, Airport via) {
  137.         List<Airport> pathToVia = graph.findShortestPath(origin, via, avoidCities);
  138.  
  139.         Collections.reverse(pathToVia);
  140.         return pathToVia;
  141.     }
  142.  
  143.     private ShortestPath findPathWithoutVia(Airport origin, Airport destination) {
  144.         ArrayList<Airport> path = graph.findShortestPath(origin, destination, avoidCities);
  145.  
  146.         if (path.size() == 0) {
  147.             return new ShortestPath();
  148.         } else {
  149.             Collections.reverse(path);
  150.  
  151.             Double totalDistance = calculateDistance(path);
  152.  
  153.             return new ShortestPath(path, totalDistance);
  154.         }
  155.     }
  156.  
  157.     private Double printPath(List<Airport> path, Double totalDistance) {
  158.         for (int i = 0; i < path.size() - 1; i++) {
  159.             System.out.println(String.format("%s %s %d", path.get(i).getCity(), path.get(i + 1).getCity(), graph.getWeight(path.get(i), path.get(i + 1)).longValue()));
  160.             totalDistance += graph.getWeight(path.get(i), path.get(i + 1));
  161.         }
  162.         return totalDistance;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement