Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.flight.quick.quickflight.util;
- import com.flight.quick.quickflight.dto.ShortestPath;
- import com.flight.quick.quickflight.dto.ShortestPathRequest;
- import com.flight.quick.quickflight.graph.DirectedGraph;
- import com.flight.quick.quickflight.graph.Graph;
- import com.flight.quick.quickflight.model.Airport;
- import com.flight.quick.quickflight.model.Route;
- import com.flight.quick.quickflight.service.AirportService;
- import com.flight.quick.quickflight.service.RouteService;
- import java.util.*;
- public class FlightFinderOld {
- private final AirportService airportService;
- private final RouteService routeService;
- private final String source;
- private Set<String> avoidCities = new HashSet<>();
- private Map<String, Airport> airports = new HashMap<>();
- private List<Route> routes = new ArrayList<>();
- private Graph graph = new DirectedGraph();
- private ShortestPathRequest shortestPathRequest;
- public FlightFinderOld(AirportService airportService, RouteService routeService, ShortestPathRequest shortestPathRequest, String source) {
- this.airportService = airportService;
- this.routeService = routeService;
- this.source = source;
- this.shortestPathRequest = shortestPathRequest;
- loadAirports();
- loadRoutes();
- loadAvoidedCities();
- buildGraph();
- }
- private void loadAvoidedCities() {
- for (Airport avoidedAirport : shortestPathRequest.getAvoid()) {
- avoidCities.add(avoidedAirport.getIata());
- }
- }
- private void loadAirports() {
- ArrayList<Airport> airportList = airportService.getAllAirports();
- for(Airport airport: airportList) {
- airports.put(airport.getIata(), airport);
- }
- }
- private void loadRoutes() {
- ArrayList<Route> routesList = routeService.getAllRoutesBySource(source);
- for(Route route: routesList) {
- routes.add(route);
- }
- }
- private void buildGraph() {
- for (Route route: routes) {
- Airport origin = airports.get(route.getFromAirport());
- Airport destination = airports.get(route.getToAirport());
- Double airDistance = AirportAirDistance.calculateAirDistance(origin, destination);
- graph.addEdge(origin, destination, airDistance);
- }
- }
- /**
- * Find shortest path between cities given in program argument.
- *
- * Also avoid cities that are passed through program arguments and
- * visit city passed through program arguments
- */
- public ShortestPath findShortestPath() {
- Airport origin = airports.get(shortestPathRequest.getStart().getIata());
- Airport destination = airports.get(shortestPathRequest.getEnd().getIata());
- Airport via = null;
- if (shortestPathRequest.getVia() != null) {
- via = airports.get(shortestPathRequest.getVia().getIata());
- }
- if(via == null) {
- return findPathWithoutVia(origin, destination);
- } else {
- return findPathWithVia(origin, destination, via);
- }
- }
- private ShortestPath findPathWithVia(Airport origin, Airport destination, Airport via) {
- List<Airport> pathToVia = findPathFromOriginToVia(origin, via);
- List<Airport> pathToDestination = findPathFromViaToDestination(destination, via);
- if (pathToVia.size() == 0 || pathToDestination.size() == 0) {
- return new ShortestPath();
- } else {
- ArrayList<Airport> path = new ArrayList<>();
- path.addAll(pathToVia);
- path.addAll(pathToDestination);
- Double total = calculateDistance(path);
- return new ShortestPath(path, total);
- }
- }
- private Double calculateDistance(ArrayList<Airport> path) {
- Double totalDistance = 0.00;
- for (int i = 0; i < path.size() - 1; i++) {
- totalDistance += graph.getWeight(path.get(i), path.get(i + 1));
- }
- return totalDistance;
- }
- private List<Airport> findPathFromViaToDestination(Airport destination, Airport via) {
- List<Airport> pathToDestination = graph.findShortestPath(via, destination, avoidCities);
- Collections.reverse(pathToDestination);
- return pathToDestination;
- }
- private List<Airport> findPathFromOriginToVia(Airport origin, Airport via) {
- List<Airport> pathToVia = graph.findShortestPath(origin, via, avoidCities);
- Collections.reverse(pathToVia);
- return pathToVia;
- }
- private ShortestPath findPathWithoutVia(Airport origin, Airport destination) {
- ArrayList<Airport> path = graph.findShortestPath(origin, destination, avoidCities);
- if (path.size() == 0) {
- return new ShortestPath();
- } else {
- Collections.reverse(path);
- Double totalDistance = calculateDistance(path);
- return new ShortestPath(path, totalDistance);
- }
- }
- private Double printPath(List<Airport> path, Double totalDistance) {
- for (int i = 0; i < path.size() - 1; i++) {
- 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()));
- totalDistance += graph.getWeight(path.get(i), path.get(i + 1));
- }
- return totalDistance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement