/* Road Network Simulator * Author: Douglas Jones * Status: Awful * Version: 9/8/2020 */ import java.util.LinkedList; import java.util.Iterator; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /** * Roads connect intersections * @see Intersection */ class Road { // instance variables float travelTime; Intersection destination; Intersection source; // the collection of all instances private static final LinkedList allRoads = new LinkedList (); /** The only constructor * @param sc Scanner from which description comes * Input format scanned from sc: source-name destination-name travel-time *
where source-name is the name of the source intersection and *
destination-name is the name of the destination intersection and *
travel-time is a floating point time in seconds */ public Road( Scanner sc ) { // keyword Road was already scanned String src = sc.next(); // where does it come from String dst = sc.next(); // where does it go travelTime = sc.nextFloat(); // BUG: destination = findintersection( dst ) // BUG: source = findintersection( dst ) allRoads.add( this ); // this is the only place items are added! } /** Primarily for debugging * @return textual name and travel time of the road */ public String toString() { return source.name + " " + destination.name + " " + travelTime; } /** Allow outsiders to iterate over all roads * @return textual name and travel time of the road */ public static Iterator iterator() { return allRoads.iterator(); } } /** * Intersections are connected by roads * @see Road */ class Intersection { // instance variables String name; final LinkedList outgoing = new LinkedList (); final LinkedList incoming = new LinkedList (); // the collection of all instances private static final LinkedList allIntersections = new LinkedList (); /** The only constructor for Intersection * @arg sc the scanner from which the Intersection description is scanned * Input format scanned from sc: name *
where name is a string */ public Intersection( Scanner sc ) { name = sc.next(); // pick off name of intersection; // BUG: Intersection type? Other attributes? Handle this later. // BUG: Must Detect duplicate definitions of intersections? allIntersections.add( this ); } /** Primarily for debugging * @return textual name and travel time of the road */ public String toString() { return name; // BUG: Other attributes? } /** Allow outsiders to iterate over all roads * @return textual name and travel time of the road */ public static Iterator iterator() { return allIntersections.iterator(); } /** Allow finding intersections by name * @return textual name and travel time of the road */ public static Intersection lookup( String n ) { for (Intersection i: allIntersections) { if (i.equals( n )) return i; } return null; } } /** * Main class builds model and will someday simulate it * @see Road * @see Intersection */ public class RoadNetwork { private static void readNetwork( Scanner sc ) { while (sc.hasNext()) { // until the input file is finished String command = sc.next(); if ("intersection".equals( command )) { new Intersection( sc ); } else if ("road".equals( command )) { new Road( sc ); } else { // Bug: Complain about unknown command } } } private static void writeNetwork() { // BUG some kind of loop printing intersections // BUG some kind of loop printing roads } public static void main( String[] args ) { if (args.length < 1) { // BUG complain about missing args } else try { readNetwork( new Scanner( new File( args[0] ) ) ); writeNetwork(); } catch ( FileNotFoundException e) { // BUG complain about file not found } } }