// RoadNetwork.java import java.io.File; import java.io.FileNotFoundException; import java.util.LinkedList; import java.util.Scanner; // Utility classes class Errors { static void fatal( String message ) { System.err.println( message ); System.exit( 1 ); } static void warning( String message ) { System.err.println( message ); } } // Simulation classes /** Roads are one-way streets linking intersections * @see Intersection */ class Road { private float travelTime; // measured in seconds private Intersection destination;// where road goes private Intersection source; // where road comes from // name of road is source-destination // initializer public Road( Scanner sc, LinkedList inters ) { // scan and process one road String sourceName = sc.next(); String dstName = sc.next(); // Bug: look up source and dest names! travelTime = sc.nextFloat(); // Bug: What if no next float? String skip = sc.nextLine(); // Bug: What if more junk at end of line (skip not empty) } // other methods public String toString() { return ( "Road " + source.name + " " + destination.name + " " + travelTime ); } } /** Intersections join roads * @see Road */ class Intersection { final String name; private LinkedList outgoing = new LinkedList (); private LinkedList incoming = new LinkedList (); // Bug: multiple types of intersections -- stoplight, 4-way etc // Bug: do I ever need to know about incoming roads? // initializer public Intersection( Scanner sc, LinkedList inters ) { // scan and process one intersection name = sc.next(); // Bug: look up name! String skip = sc.nextLine(); // Bug: What if more junk at end of line (skip not empty) } // other methods public String toString() { return ( "Intersection " + name ); } } /** RoadNetwork is the main class that builds the whole model * @see Road * @see Intersection */ public class RoadNetwork { // the sets of all roads and all intersections static LinkedList roads; static LinkedList inters; /** Look up s in inters, find that Intersection if it exists */ static Intersection findIntersection( String s ) { // return the intersection named s, or null if no such // Bug: Flesh this out } /** Initialize the road network by scanning its description */ static void initializeNetwork( Scanner sc ) { while (sc.hasNext()) { String command = sc.next(); if ((command == "intersection") || (command == "i")) { inters.add( new Intersection( sc, inters ) ); } else if ((command == "road") || (command == "r")) { roads.add( new Road( sc, inters ) ); } else { Errors.warning( "unknown command" ); // Bug: should we allow comments? } } } /** Print out the road network from the data structure */ static void printNetwork() { for (Intersection i:inters) { System.out.println( i.toString() ); } for (Road r:roads) { System.out.println( r.toString() ); } } /** Main program * @see initializeNetwork */ public static void main(String[] args) { try { if (args.length < 1) { Errors.fatal( "missing file name" ); } if (args.length > 1) { Errors.fatal( "too many arguments" ); } initializeNetwork( new Scanner(new File(args[0])) ); } catch (FileNotFoundException e) { Errors.fatal( "file not found: " + args[0] ); } printNetwork(); } }