Nächste Schritt für Routenplanung
parent
ff6d3cc6ae
commit
dbeb4418f3
|
@ -9,7 +9,7 @@ public class NaviApp {
|
||||||
|
|
||||||
System.out.println(map.getNumberOfCities());
|
System.out.println(map.getNumberOfCities());
|
||||||
|
|
||||||
map.planRoute("Mannheim", "Trier");
|
map.initializePlanning("Mannheim", "München");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ public class City {
|
||||||
private String name;
|
private String name;
|
||||||
private int distance;
|
private int distance;
|
||||||
private String predecessor;
|
private String predecessor;
|
||||||
|
private boolean visited;
|
||||||
|
|
||||||
private TreeSet<Street> neighbors = new TreeSet<>();
|
private TreeSet<Street> neighbors = new TreeSet<>();
|
||||||
|
|
||||||
|
@ -37,9 +38,26 @@ public class City {
|
||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDistance(int distance) {
|
||||||
|
this.distance = distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVisited() {
|
||||||
|
return visited;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVisited() {
|
||||||
|
visited = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPredecessor() {
|
||||||
|
return predecessor;
|
||||||
|
}
|
||||||
|
|
||||||
private void reset() {
|
private void reset() {
|
||||||
distance = Integer.MAX_VALUE;
|
distance = Integer.MAX_VALUE;
|
||||||
predecessor = null;
|
predecessor = null;
|
||||||
|
visited = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ package de.th_mannheim.informatik.routenplaner.domain;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
|
@ -16,26 +17,62 @@ public class Routeplaner {
|
||||||
loadData();
|
loadData();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] planRoute(String start, String destination) {
|
public LinkedList<String> initializePlanning(String start, String destination) {
|
||||||
|
cities.get(start).setDistance(0);
|
||||||
|
|
||||||
|
planRoute(start, destination);
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("Route:");
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
LinkedList<String> hops = new LinkedList<>();
|
||||||
|
City dc = cities.get(destination);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
hops.add(dc.getName() + " " + dc.getDistance() + " km");
|
||||||
|
System.out.println("Stadt: " + dc.getName() + " " + dc.getDistance() + " km");
|
||||||
|
|
||||||
|
if (dc.getName().equals(start))
|
||||||
|
break;
|
||||||
|
|
||||||
|
dc = cities.get(dc.getPredecessor());
|
||||||
|
|
||||||
|
System.out.println("\tVorher: " + dc.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
return hops;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void planRoute(String start, String destination) {
|
||||||
|
System.out.println("Prüfe " + start);
|
||||||
City sc = cities.get(start);
|
City sc = cities.get(start);
|
||||||
|
sc.setVisited();
|
||||||
|
|
||||||
if (sc == null || cities.get(destination) == null)
|
if (sc == null || cities.get(destination) == null)
|
||||||
throw new RuntimeException("Stadt nicht gefunden.");
|
throw new RuntimeException("Stadt nicht gefunden.");
|
||||||
|
|
||||||
for (Street s : sc.getNeighbors()) {
|
for (Street s : sc.getNeighbors()) {
|
||||||
System.out.println(s.toString());
|
System.out.println(s.toString());
|
||||||
|
|
||||||
City nc = s.getDestination();
|
City nc = s.getDestination();
|
||||||
nc.checkAndChangeDistance(s.getDistance(), sc.getName());
|
|
||||||
|
if (nc.isVisited()) {
|
||||||
|
System.out.println("schon besucht");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
nc.checkAndChangeDistance(sc.getDistance() + s.getDistance(), sc.getName());
|
||||||
|
|
||||||
if (nc.getName().equals(destination)) {
|
if (nc.getName().equals(destination)) {
|
||||||
System.out.println("Route nach " + destination + " gefunden mit Entfernung: " + nc.getDistance());
|
System.out.println("Route nach " + destination + " gefunden mit Entfernung: " + nc.getDistance());
|
||||||
|
} else {
|
||||||
|
planRoute(nc.getName(), destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNumberOfCities() {
|
public int getNumberOfCities() {
|
||||||
|
|
Loading…
Reference in New Issue