Changeset 180 in tmcsimulator for trunk/src/atmsdriver/ConsoleTrafficDriver.java
- Timestamp:
- 10/27/2017 01:38:13 PM (9 years ago)
- File:
-
- 1 moved
-
trunk/src/atmsdriver/ConsoleTrafficDriver.java (moved) (moved from trunk/src/atmsdriver/ConsoleDriver.java) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/atmsdriver/ConsoleTrafficDriver.java
r176 r180 5 5 import atmsdriver.model.Highway; 6 6 import atmsdriver.model.Station; 7 import atmsdriver.model.LoopDetector.DOTCOLOR; 7 8 import java.io.FileInputStream; 8 9 import java.util.ArrayList; … … 21 22 * @version 10/11/2017 22 23 */ 23 public final class Console Driver {24 public final class ConsoleTrafficDriver { 24 25 // highways model 25 26 private final Highways highways; … … 27 28 // lists used for user input validation 28 29 private final List<Integer> routeNumInputList; 29 private final List<String> dotColorInputList; 30 /** 31 * Properties for the ConsoleDriver 30 /** 31 * Properties for the ConsoleTrafficDriver 32 32 */ 33 33 private static Properties ConsoleDriverProperties; … … 57 57 58 58 // Construct the console driver using the highways model 59 Console Driver driver = new ConsoleDriver(highways);59 ConsoleTrafficDriver driver = new ConsoleTrafficDriver(highways); 60 60 driver.runConsole(); 61 61 } else { … … 92 92 * @param highways 93 93 */ 94 public Console Driver(Highways highways) {94 public ConsoleTrafficDriver(Highways highways) { 95 95 // set highways model 96 96 this.highways = highways; … … 98 98 // set input validation lists 99 99 routeNumInputList = generateRouteNumInputList(); 100 dotColorInputList = new ArrayList<>(Arrays.asList("R", "Y", "G"));101 100 } 102 101 … … 121 120 public void runConsole() { 122 121 Scanner sc = new Scanner(System.in); 122 char choice = 'A'; 123 123 // Run continuously 124 while (true) { 124 while (choice != 'Q') 125 { 125 126 // Get necessary values for colorization of highways 126 127 Integer routeNumber = getRouteNumber(sc); … … 129 130 Double range = getRange(sc, postmile); 130 131 DOTCOLOR dotcolor = getDotColor(sc); 131 132 132 133 // apply colorization to highways 133 applyColorToHighwayStretch(routeNumber, direction, postmile, range, dotcolor); 134 } 135 } 136 137 /** 138 * Applies specified color to the specified highway stretch. Route number and 139 * direction specify the highway. Postmile and range specify the stretch of 140 * specified highway. Dot color is the color to be applied to the stretch. 141 * 142 * @param routeNumber highway route number 143 * @param direction highway direction 144 * @param postmile origin postmile value 145 * @param range range from origin postmile 146 * @param dotColor the color to be applied to specified highway stretch 147 */ 148 public void applyColorToHighwayStretch(Integer routeNumber, DIRECTION direction, 149 Double postmile, Double range, DOTCOLOR dotColor) { 150 System.out.println("Applying " + dotColor.name() + " dots to highway " 151 + routeNumber + " " + direction.name() + " at postmile " 152 + postmile + " with a range of " + range + " miles..."); 153 154 // Get the highway by route number 155 Highway highway = highways.getHighwayByRouteNumber(routeNumber); 156 157 // start value for highway section, and end value for highway section 158 // by postmile 159 Double startPost; 160 Double endPost; 161 162 // postmiles increase from s to n and w to e 163 164 // if the direction is south or west 165 if(direction.equals(DIRECTION.SOUTH) || direction.equals(DIRECTION.WEST)) 166 { 167 // add range value to startPost to get 168 // the end postmile value of the highway section 169 startPost = postmile; 170 endPost = postmile + range; 171 172 // iterate through the stations, if within the specified highway 173 // stretch, update the station by direction and apply dot color 174 for(Station station : highway.stations) 175 { 176 if(station.postmile >= startPost && station.postmile <= endPost) 177 { 178 station.updateByDirection(direction, dotColor); 134 highways.applyColorToHighwayStretch(routeNumber, direction, postmile, range, dotcolor); 135 136 System.out.println("Add another entry or Send now? (A/S)"); 137 choice = sc.next().toUpperCase().trim().charAt(0); 138 System.out.println(""); 139 if (choice == 'S') 140 { 141 // Send highway model to FEP for transmit to ATMS 142 try { 143 highways.writeToFEP(); 144 } catch (SimulationException ex) { 145 System.out.println("Skipping writeToFEP..."); 179 146 } 180 } 181 } 182 // if the direction is north or east 183 else 184 { 185 //subtract range value from startPost 186 // to get the end postmile value of the highway section 187 startPost = postmile; 188 endPost = postmile - range; 189 190 // iterate through the stations, if within the specified highway 191 // section, update the station by direction and apply dot color 192 for(Station station : highway.stations) 193 { 194 if(station.postmile <= startPost && station.postmile >= endPost) 195 { 196 station.updateByDirection(direction, dotColor); 197 } 198 } 199 } 200 System.out.println(""); 201 // Omit this since it's now running as a task in ATMSBatchDriver 202 // try { 203 // highways.writeToFEP(); 204 // } catch (SimulationException ex) { 205 // System.out.println("Skipping writeToFEP..."); 206 // } 207 } 147 System.out.println("Add another entry or Quit? (A/Q)"); 148 choice = sc.next().toUpperCase().trim().charAt(0); 149 } 150 } 151 } 152 208 153 209 154 /** … … 398 343 // prompt user for color 399 344 System.out.println("Enter a dot color (G/Y/R):"); 400 dotColorInput = sc.next() ;345 dotColorInput = sc.next().toUpperCase(); 401 346 System.out.println(""); 402 347 // validate user's input 403 if( dotColorInputList.contains(dotColorInput))348 if(DOTCOLOR.allLetters.contains(dotColorInput)) 404 349 { 405 350 verified = true; … … 414 359 } 415 360 416 /**417 * Enum for highway status dot colors. Each color has associated volume418 * and occupancy constants.419 *420 * @author John A. Torres, jdalbey421 * @version 10/11/2017422 */423 public static enum DOTCOLOR {424 425 RED(1, 0.06f),426 YELLOW(3,0.059f), // speed = 26427 GREEN(0,0);428 429 // All the first letters of the values, in order.430 private static String allLetters = "RYG";431 432 private int vol; /* volume */433 private float occ; /* occupancy */434 435 private DOTCOLOR(int v, float o)436 {437 vol = v;438 occ = o;439 }440 /**441 * Return the first letter of this enum.442 *443 * @return String first letter of this enum.444 */445 public String getLetter() {446 return this.toString().substring(0, 1);447 }448 449 public int volume()450 {451 return vol;452 }453 public float occupancy()454 {455 return occ;456 }457 /**458 * Returns a dot color given its first character.459 *460 * @param letter the first character of a dot color461 * @return dot color corresponding to letter462 * @pre letter must be one of allLetters463 */464 public static DOTCOLOR toDotColor(String letter) {465 return values()[allLetters.indexOf(letter.charAt(0))];466 }467 }468 361 }
Note: See TracChangeset
for help on using the changeset viewer.
