// RoadNetwork.java /** Road network simulator (or it will become one once it's done) * @author Douglas W. Jones * @version Feb. 15, 2021 */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.Collection; /** Centralized error reporting tools * error reports are output to System.err (standard error, aka stderr) */ class Error { /** Fatal error * @param msg -- the warning message * this never returns, the application is terminated abnormally */ public static void fatal( String msg ) { System.err.println( "Error: " + msg ); System.exit( 1 ); // report failure to the OS (or to the shell) } /** Non-fatal warning * @param msg -- the warning message */ public static void warn( String msg ) { System.err.println( "Warning: " + msg ); } } class Road { Intersection source; // road from here Intersection destination; // road to there double travelTime; // time in seconds public Road( Scanner in ) { String srcname; // name of source intersection String dstname; // name of destination intersection float time; // travel time between intersections if (in.hasNext()) { srcname = in.next(); } else { Error.warn( "road with no source" ); srcname = "???"; } if (in.hasNext()) { dstname = in.next(); } else { Error.warn( "road " + srcname + " with no destination" ); dstname = "???"; } if (in.hasNextFloat()) { time = in.nextFloat(); } else { Error.warn( "road " + srcname + " " + dstname + " with no travel time" ); time = 99999.9F; } // BUG: What about time units? if (in.hasNext( ";" )) { in.next( ";" ); } else { Error.warn( "road " + srcname + " " + dstname + " " + time + " with no semicolon" ); } if (time <= 0.0F) { Error.warn( "road " + srcname + " " + dstname + " " + time + " with negative travel time" ); time = 99999.9F; } // look up the intersection called srcname to set source // look up the intersection called dstname to set destination // set travelTime to time } // BUG: need to add behavior for roads } class Intersection { // BUG: is collection the right class? Should I pick a subclass? Collection incoming; // the roads leading here Collection outgoing; // the roads that come from here public Intersection( Scanner in ) { // BUG: need detail! } // BUG: need to add behavior for roads } public class RoadNetwork { /** build a road network model by scanning an input file * @param in -- the scanner used to read the file * Input file contains keywords road and intersection, * details beyond that are handled by the appropriate classes */ private static void buildModel( Scanner in ) { while (in.hasNext()) { String keyword = in.next(); if ("intersection".equals( keyword )) { new Intersection( in ); } else if ("road".equals( keyword )) { new Road( in ); } else { Error.warn( "not a keyword in the input: " + keyword ); } } } /** main method mostly concerned with command line arguments * @param args -- the command line arguments * Want just one argument, the file name for model description */ public static void main( String[] args ) { // arg[0] should be the name of a file describing a road network if (args.length < 1) { Error.fatal( "missing command line argument, a file name" ); } if (args.length > 1) { Error.warn( "extra command line argument: " + args[1] ); } try { buildModel( new Scanner( new File( args[0] ) ) ); // BUG: simulate the network that was built } catch ( FileNotFoundException e ) { Error.fatal( "could not open file: " + args[0] ); } } }