Created
June 24, 2014 01:14
-
-
Save jonatasemidio/2f8ed792d9bc8618387a to your computer and use it in GitHub Desktop.
Mapa Recursivo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RecursiveMap{ | |
public static TreeMap<String, String> routeCalculation (Map<String, TreeMap<String, Integer>> cityMaps, TreeMap<String, String> rota) { | |
//Map<String, ArrayList<String>> mapping = TrainsInformation.drawAllRoutes(cityMaps); | |
String originCity = rota.get("ORIGIN_CITY" ); | |
String destinationCity = rota.get("DESTINATION_CITY"); | |
String distance = rota.get("DISTANCE" ); | |
String max = rota.get("MAX" ); | |
String routeMapping = rota.get("ROUTE_MAPPING" ); | |
Integer parsedDistance = Integer.parseInt(distance); | |
Integer parsedMax = Integer.parseInt(max); | |
Integer nextDistance = 0; | |
Set<String> mapping = cityMaps.get(originCity).keySet(); | |
//System.out.println("routeMapping: "+routeMapping); | |
Iterator<String> cities = mapping.iterator(); | |
while (cities.hasNext()) { | |
String city = cities.next(); | |
System.out.println("city: "+city); | |
nextDistance = TrainsInformation.getDistance(cityMaps, originCity, city); | |
//System.out.println("nextDistance: "+nextDistance); | |
//System.out.println(nextDistance + parsedDistance); | |
if ( cities.hasNext() && ((nextDistance + parsedDistance) > parsedMax) ) { | |
System.out.println("continue ..."); | |
continue; | |
}else if ((nextDistance + parsedDistance) > parsedMax) { | |
//System.out.println("retorna ..."); | |
return rota; | |
} | |
originCity = city; | |
System.out.println("originCity: "+originCity); | |
parsedDistance += nextDistance; | |
distance = parsedDistance.toString(); | |
routeMapping += ", " + city; | |
TreeMap<String, String> currentRoute = new TreeMap<String, String>(); | |
currentRoute.put("ORIGIN_CITY" , originCity ); | |
currentRoute.put("DESTINATION_CITY" , destinationCity); | |
currentRoute.put("DISTANCE" , distance ); | |
currentRoute.put("MAX" , max ); | |
currentRoute.put("ROUTE_MAPPING" , routeMapping ); | |
System.out.println("new map : "+currentRoute); | |
return routeCalculation(cityMaps, currentRoute); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment