planning #1
|
@ -7,7 +7,12 @@ public class NaviApp {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Routeplaner map = new Routeplaner();
|
Routeplaner map = new Routeplaner();
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
System.out.println(map.getNumberOfCities());
|
System.out.println(map.getNumberOfCities());
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
for (String waypoint : map.initializePlanning("Mannheim", "Wilhelmshaven"))
|
||||||
|
System.out.println(waypoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,14 @@ import java.util.TreeSet;
|
||||||
|
|
||||||
public class City {
|
public class City {
|
||||||
private String name;
|
private String name;
|
||||||
|
private int distance;
|
||||||
|
private String predecessor;
|
||||||
|
|
||||||
private TreeSet<Street> neighbors = new TreeSet<>();
|
private TreeSet<Street> neighbors = new TreeSet<>();
|
||||||
|
|
||||||
public City(String name) {
|
public City(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addConnection(City stadt2, int distance) {
|
public void addConnection(City stadt2, int distance) {
|
||||||
|
@ -23,4 +26,33 @@ public class City {
|
||||||
return neighbors;
|
return neighbors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void checkAndChangeDistance(int distance, String predecessor) {
|
||||||
|
if (distance < this.distance) {
|
||||||
|
this.distance = distance;
|
||||||
|
this.predecessor = predecessor;
|
||||||
|
System.out.println(name + " " + distance + " <- " + predecessor);
|
||||||
|
|
||||||
|
for (Street s : neighbors) {
|
||||||
|
s.setUnused();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDistance() {
|
||||||
|
return distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDistance(int distance) {
|
||||||
|
this.distance = distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPredecessor() {
|
||||||
|
return predecessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reset() {
|
||||||
|
distance = Integer.MAX_VALUE;
|
||||||
|
predecessor = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -2,6 +2,8 @@ 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.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
@ -16,6 +18,62 @@ public class Routeplaner {
|
||||||
loadData();
|
loadData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Deque<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();
|
||||||
|
|
||||||
|
Deque<String> hops = new ArrayDeque<>();
|
||||||
|
City dc = cities.get(destination);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
hops.push(dc.getName() + " " + dc.getDistance() + " km");
|
||||||
|
|
||||||
|
if (dc.getName().equals(start))
|
||||||
|
break;
|
||||||
|
|
||||||
|
dc = cities.get(dc.getPredecessor());
|
||||||
|
}
|
||||||
|
|
||||||
|
return hops;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void planRoute(String start, String destination) {
|
||||||
|
System.out.println("Prüfe " + start);
|
||||||
|
City sc = cities.get(start);
|
||||||
|
|
||||||
|
if (sc == null || cities.get(destination) == null)
|
||||||
|
throw new RuntimeException("Stadt nicht gefunden.");
|
||||||
|
|
||||||
|
for (Street s : sc.getNeighbors()) {
|
||||||
|
if (s.isUsed())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
s.setUsed();
|
||||||
|
|
||||||
|
System.out.println(s.toString());
|
||||||
|
|
||||||
|
City nc = s.getDestination();
|
||||||
|
|
||||||
|
nc.checkAndChangeDistance(sc.getDistance() + s.getDistance(), sc.getName());
|
||||||
|
|
||||||
|
if (nc.getName().equals(destination)) {
|
||||||
|
System.out.println("Route nach " + destination + " gefunden mit Entfernung: " + nc.getDistance());
|
||||||
|
} else {
|
||||||
|
System.out.println();
|
||||||
|
planRoute(nc.getName(), destination);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public int getNumberOfCities() {
|
public int getNumberOfCities() {
|
||||||
return cities.size();
|
return cities.size();
|
||||||
}
|
}
|
||||||
|
@ -85,6 +143,7 @@ public class Routeplaner {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println(start + " " + line + " " + matrix[cityLookup.get(start)][cityLookup.get(line)]);
|
||||||
this.addStreet(start, line, matrix[cityLookup.get(start)][cityLookup.get(line)]);
|
this.addStreet(start, line, matrix[cityLookup.get(start)][cityLookup.get(line)]);
|
||||||
|
|
||||||
} // while
|
} // while
|
||||||
|
|
|
@ -5,6 +5,8 @@ public class Street implements Comparable<Street> {
|
||||||
private final City destination;
|
private final City destination;
|
||||||
|
|
||||||
private final int distance;
|
private final int distance;
|
||||||
|
private boolean used;
|
||||||
|
|
||||||
|
|
||||||
public Street(City start, City destination, int distance) {
|
public Street(City start, City destination, int distance) {
|
||||||
super();
|
super();
|
||||||
|
@ -31,7 +33,7 @@ public class Street implements Comparable<Street> {
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
public City getTarget() {
|
public City getDestination() {
|
||||||
return destination;
|
return destination;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +41,18 @@ public class Street implements Comparable<Street> {
|
||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isUsed() {
|
||||||
|
return used;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsed() {
|
||||||
|
used = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUnused() {
|
||||||
|
used = false;
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return start.getName() + " -> " + destination.getName() + ": " + distance + " km";
|
return start.getName() + " -> " + destination.getName() + ": " + distance + " km";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue