package ch.zhaw.ads; import java.util.*; public class RouteServer implements CommandExecutor { /** build the graph given a text file with the topology */ public Graph createGraph(String topo) throws Exception { Graph graph = new AdjListGraph<>(DijkstraNode.class, Edge.class); String[] lines = topo.split("\n"); for(String line : lines){ String[] strings = line.split(" "); try { graph.addEdge(strings[0], strings[1], Double.parseDouble(strings[2])); graph.addEdge(strings[1], strings[0], Double.parseDouble(strings[2])); } catch (Throwable e) { e.printStackTrace(); } } return graph; // TODO implement } /** apply the dijkstra algorithm */ public void dijkstraRoute(Graph graph, String from, String to) { // TODO implement } /** find the route in the graph after applied dijkstra the route should be returned with the start town first */ public List getRoute(Graph graph, String to) { List route = new LinkedList<>(); DijkstraNode town = graph.findNode(to); do { route.add(0, town); town = town.getPrev(); } while (town != null); return route; } public String execute(String topo) throws Exception { Graph graph = createGraph(topo); dijkstraRoute(graph, "Winterthur", "Lugano"); List route = getRoute(graph, "Lugano"); // generate result string StringBuilder builder = new StringBuilder(); for (DijkstraNode rt : route) builder.append(rt).append("\n"); return builder.toString(); } public static void main(String[] args)throws Exception { String swiss = "Winterthur Zürich 25\n" + "Zürich Bern 126\n" + "Zürich Genf 277\n" + "Zürich Luzern 54\n" + "Zürich Chur 121\n" + "Zürich Berikon 16\n" + "Bern Genf 155\n" + "Genf Lugano 363\n" + "Lugano Luzern 206\n" + "Lugano Chur 152\n" + "Chur Luzern 146\n" + "Luzern Bern 97\n" + "Bern Berikon 102\n" + "Luzern Berikon 41\n"; RouteServer server = new RouteServer(); System.out.println(server.execute(swiss)); } }