solved s07 ex03

This commit is contained in:
navid.sassan 2020-11-08 20:09:30 +01:00
parent e8700f39ef
commit cc51b4d33b
3 changed files with 79 additions and 1 deletions

View File

@ -51,4 +51,4 @@ public class AdjListGraph<N extends Node,E extends Edge>
public Iterable<N> getNodes() { public Iterable<N> getNodes() {
return nodes; return nodes;
} }
} }

View File

@ -0,0 +1,40 @@
package ch.zhaw.ads;
import java.util.Scanner;
public class RouteServer implements CommandExecutor {
@Override
public String execute(String command) {
Graph<DijkstraNode, Edge> graph = this.readInput(command);
StringBuffer result = new StringBuffer(100);
for (Node node : graph.getNodes()) {
result.append(node.getName() + "\n");
}
result.append("\"\n");
return result.toString();
}
private Graph<DijkstraNode, Edge> readInput(String input) {
int dist;
String line, src, dest;
String[] values;
Scanner scanner = new Scanner(input);
Graph<DijkstraNode, Edge> graph = new AdjListGraph<DijkstraNode, Edge>(DijkstraNode.class, Edge.class);
while (scanner.hasNextLine()) {
line = scanner.nextLine();
try {
values = line.split(" ");
graph.addEdge(values[0], values[1], Integer.valueOf(values[2]));
graph.addEdge(values[1], values[0], Integer.valueOf(values[2]));
} catch (Throwable e) {
System.err.println("Error.");
continue;
}
}
scanner.close();
return graph;
}
}

View File

@ -0,0 +1,38 @@
package ch.zhaw.ads;
import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;
import java.util.*;
import java.io.*;
public class RouteServerTest {
RouteServer routeServer;
String input;
@Before
public void setUp() throws Exception {
routeServer = new RouteServer();
input = openFile("./build/resources/main/Swiss.txt");
}
@Test
public void test1() {
System.out.println(routeServer.execute(input));
}
// helper functions
private String openFile(String name) throws Exception {
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(name), "ISO-8859-1"));
StringBuffer b = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
b.append(line);
b.append('\n');
}
return b.toString();
}
}