Erste Iteration für Routenplanung eingefügt.
parent
4109e6253e
commit
ff6d3cc6ae
|
@ -8,6 +8,8 @@ public class NaviApp {
|
|||
Routeplaner map = new Routeplaner();
|
||||
|
||||
System.out.println(map.getNumberOfCities());
|
||||
|
||||
map.planRoute("Mannheim", "Trier");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,11 +4,14 @@ import java.util.TreeSet;
|
|||
|
||||
public class City {
|
||||
private String name;
|
||||
private int distance;
|
||||
private String predecessor;
|
||||
|
||||
private TreeSet<Street> neighbors = new TreeSet<>();
|
||||
|
||||
public City(String name) {
|
||||
this.name = name;
|
||||
reset();
|
||||
}
|
||||
|
||||
public void addConnection(City stadt2, int distance) {
|
||||
|
@ -23,4 +26,20 @@ public class City {
|
|||
return neighbors;
|
||||
}
|
||||
|
||||
public void checkAndChangeDistance(int distance, String predecessor) {
|
||||
if (distance < this.distance) {
|
||||
this.distance = distance;
|
||||
this.predecessor = predecessor;
|
||||
}
|
||||
}
|
||||
|
||||
public int getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
private void reset() {
|
||||
distance = Integer.MAX_VALUE;
|
||||
predecessor = null;
|
||||
}
|
||||
|
||||
}
|
|
@ -18,11 +18,26 @@ public class Routeplaner {
|
|||
|
||||
public String[] planRoute(String start, String destination) {
|
||||
|
||||
City sc = cities.get(start);
|
||||
|
||||
if (sc == null || cities.get(destination) == null)
|
||||
throw new RuntimeException("Stadt nicht gefunden.");
|
||||
|
||||
for (Street s : sc.getNeighbors()) {
|
||||
System.out.println(s.toString());
|
||||
|
||||
City nc = s.getDestination();
|
||||
nc.checkAndChangeDistance(s.getDistance(), sc.getName());
|
||||
|
||||
if (nc.getName().equals(destination)) {
|
||||
System.out.println("Route nach " + destination + " gefunden mit Entfernung: " + nc.getDistance());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getNumberOfCities() {
|
||||
return cities.size();
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public class Street implements Comparable<Street> {
|
|||
return start;
|
||||
}
|
||||
|
||||
public City getTarget() {
|
||||
public City getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue