import java.util.*; /** * @author Norman Casagrande */ public class GraphTest { /** * This class holds some information JUST for test purpose. * In TP4 instead of this one you will have a City (with its coordinates), * generally available in another file. */ private class WeatherInfo { public String condition; public double temperature; public double pressure; WeatherInfo(String condition, double temperature, double pressure) { this.condition = condition; this.temperature = temperature; this.pressure = pressure; } public boolean equals(Object o) { WeatherInfo param = (WeatherInfo)o; if ( param == null ) return false; else if ( this.condition == param.condition && this.temperature == param.temperature && this.pressure == param.pressure ) { return true; } else { return false; } } public String toString() { return condition + ", " + temperature + "°C, " + pressure + " mbar"; } } ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// // A reference to the graph private Graph myGraph; /** * Constructor of the testing class. It creates Graph object and * adds elements to it. */ public GraphTest() { myGraph = new MapGraph(); myGraph.addNode("New York", new WeatherInfo("Sunny", 15, 980)); myGraph.addNode("Boston", new WeatherInfo("Sunny", 10, 1023)); myGraph.addNode("Washington DC", new WeatherInfo("Cloudy", 10, 1010)); myGraph.addNode("Denver", new WeatherInfo("Rain", 12, 1100)); myGraph.addNode("Montreal", new WeatherInfo("Snow", -1, 1012)); // This should not be added thanks to Node.equals // (it already exists!) myGraph.addNode("Montreal", new WeatherInfo("Snow", -1, 1012)); myGraph.addEdge("New York", "Boston", 35); myGraph.addEdge("New York", "Washington DC", 40); myGraph.addEdge("Denver", "Montreal", 50); myGraph.addEdge("Montreal", "Boston", 30); // This should not be added thanks to Edge.equals // (it already exists!) myGraph.addEdge("Montreal", "Boston", 30); } /** * Prints out the nodes. */ public void printNodesInfo() { System.out.println("----------- Nodes Info ---------- "); List nodeList = myGraph.getNodes(); ListIterator lIt = nodeList.listIterator(); while (lIt.hasNext()) { Node tmpNode = (Node)lIt.next(); // remember: It automatically calls toString of the given object! System.out.println(tmpNode); } } /** * Prints out the edges. */ public void printEdgesInfo() { System.out.println("----------- Edges Info ---------- "); List edgeList = myGraph.getEdges(); ListIterator lIt = edgeList.listIterator(); while (lIt.hasNext()) { Edge tmpEdge = (Edge)lIt.next(); // remember: It automatically calls toString of the given object! System.out.println(tmpEdge); } } /** * Print out informations about the mathods associated to * the graph. */ public void printGeneralTests() { System.out.println("---------- General Tests --------- "); // variables used for the tests String nodeKey1 = "Denver"; String nodeKey2 = "Montreal"; List edgesList; Node tmpNode2; Edge tmpEdge; // checking sizes System.out.println( "Node count: " + myGraph.nodeCount() ); System.out.println( "Edge count: " + myGraph.edgeCount() ); ///////////////// NODE ///////////////// // checking if node exists: System.out.println("Node[" + nodeKey1 + "] exists: " + myGraph.hasNode(nodeKey1) ); // print key and object retrieved with getNode tmpNode2 = myGraph.getNode(nodeKey2); System.out.println("Node[" + tmpNode2.getKey() + "]: " + tmpNode2.getObject() ); // The edges of one node. edgesList = tmpNode2.getEdges(); System.out.println("Edges of Node[" + nodeKey2 + "]: " + edgesList); ///////////////// EDGE ///////////////// // checking if edge exists System.out.println("Edge[" + nodeKey1 + " <--> " + nodeKey2 + "] exists: " + myGraph.hasEdge(nodeKey1, nodeKey2) ); // aaannd... the other way! :) System.out.println("Edge[" + nodeKey2 + " <--> " + nodeKey1 + "] exists: " + myGraph.hasEdge(nodeKey2, nodeKey1) ); // checking the methods of Edge tmpEdge = myGraph.getEdge(nodeKey1, nodeKey2); System.out.println("Source: [" + tmpEdge.getSource().getKey() + "] - Dest: [" + tmpEdge.getDest().getKey() + "] - Cost: " + tmpEdge.getCost() ); // clearing graph System.out.println("Clearing.."); myGraph.clear(); System.out.println( "Node count: " + myGraph.nodeCount() ); System.out.println( "Edge count: " + myGraph.edgeCount() ); } /** * main method * @param args No use of the arguments */ public static void main(String[] args) { GraphTest gf = new GraphTest(); gf.printNodesInfo(); gf.printEdgesInfo(); gf.printGeneralTests(); } }