source: tmcsimulator/trunk/src/atmsdriver/ConsoleDriver.java @ 114

Revision 114, 12.9 KB checked in by jtorres, 9 years ago (diff)

Updating FEPSimulator for JD's linux machine. Modified ATMS/Console Driver to show red dots.

Line 
1package atmsdriver;
2
3import atmsdriver.model.Highways;
4import atmsdriver.model.Station.DIRECTION;
5import atmsdriver.model.Highway;
6import atmsdriver.model.Station;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.List;
10import java.util.Scanner;
11
12/**
13 * A console application to drive the ATMS Server.
14 *
15 * @author jdalbey, John A. Torres
16 * @version 10/11/2017
17 */
18public final class ConsoleDriver {
19    // highways model
20    private final Highways highways;
21   
22    // lists used for user input validation
23    private final List<Integer> routeNumInputList;
24    private final List<String> dotColorInputList;
25
26    /** Entry point for the application.
27     *
28     * @param args unused
29     */
30    public static void main(String[] args)
31    {
32        Highways highways = new Highways(
33        "config/vds_data/lds.txt",
34        "config/vds_data/loop.txt",
35        "config/vds_data/highwaysMeta.txt",
36        "localhost", 8080);
37        ConsoleDriver app = new ConsoleDriver(highways);
38        app.runConsole();
39    }
40    /**
41     * Constructor. Sets the highways model and generates the input validation
42     * lists, and then runs the console driver application.
43     * @param highways
44     */
45    public ConsoleDriver(Highways highways) {
46        // set highways model
47        this.highways = highways;
48       
49        // set input validation lists
50        routeNumInputList = generateRouteNumInputList();
51        dotColorInputList = new ArrayList<>(Arrays.asList("R", "Y", "G"));       
52    }
53   
54    /**
55     * Generates the route number list, used for user input validation.
56     * @return list of route numbers.
57     */
58    private ArrayList<Integer> generateRouteNumInputList()
59    {
60        ArrayList<Integer> routeNums = new ArrayList<>();
61        // add the route number for each highway to the list
62        for(Highway hwy : highways.highways)
63        {
64            routeNums.add(hwy.routeNumber);
65        }
66        return routeNums;
67    }
68
69    /**
70     * Runs the console driver application.
71     */
72    public void runConsole() {
73        Scanner sc = new Scanner(System.in);
74        // Run continuously
75        while (true) {
76            // Get necessary values for colorization of highways
77            Integer routeNumber = getRouteNumber(sc);
78            DIRECTION direction = getDirection(sc, routeNumber);
79            Double postmile = getPostmile(sc, routeNumber, direction);
80            Integer range = getRange(sc, postmile);
81            DOTCOLOR dotcolor = getDotColor(sc);
82           
83            // apply colorization to highways
84            applyColorToHighwayStretch(routeNumber, direction, postmile, range, dotcolor);
85        }
86    }
87   
88    /**
89     * Applies specified color to the specified highway stretch. Route number and
90     * direction specify the highway. Postmile and range specify the stretch of
91     * specified highway. Dot color is the color to be applied to the stretch.
92     *
93     * @param routeNumber highway route number
94     * @param direction highway direction
95     * @param postmile origin postmile value
96     * @param range range from origin postmile
97     * @param dotColor the color to be applied to specified highway stretch
98     */
99    public void applyColorToHighwayStretch(Integer routeNumber, DIRECTION direction, 
100            Double postmile, Integer range, DOTCOLOR dotColor) {
101        System.out.println("Applying " + dotColor.name() + " dots to highway " 
102                + routeNumber + " " + direction.name() + " at postmile " 
103                + postmile + " with a range of " + range + " miles...");
104       
105        // Get the highway by route number
106        Highway highway = highways.getHighwayByRouteNumber(routeNumber);
107       
108        // start value for highway section, and end value for highway section
109        // by postmile
110        Double startPost;
111        Double endPost;
112       
113        // postmiles increase from s to n and w to e
114       
115        // if the direction is south or west
116        if(direction.equals(DIRECTION.SOUTH) || direction.equals(DIRECTION.WEST))
117        {
118            // add range value to startPost to get
119            // the end postmile value of the highway section
120            startPost = postmile;
121            endPost = postmile + range;
122           
123            // iterate through the stations, if within the specified highway
124            // stretch, update the station by direction and apply dot color
125            for(Station station : highway.stations)
126            {
127                if(station.postmile > startPost && station.postmile < endPost)
128                {
129                    station.updateByDirection(direction, dotColor);
130                }
131            }
132        }
133        // if the direction is north or east
134        else
135        {
136            //subtract range value from startPost
137            // to get the end postmile value of the highway section
138            startPost = postmile;
139            endPost = postmile - range;
140           
141            // iterate through the stations, if within the specified highway
142            // section, update the station by direction and apply dot color
143            for(Station station : highway.stations)
144            {
145                if(station.postmile < startPost && station.postmile > endPost)
146                {
147                    station.updateByDirection(direction, dotColor);
148                }
149            }
150        }
151        System.out.println("");
152        highways.writeToFEP();
153    }
154   
155    /**
156     * Gets the highway route number from user and validates the input.
157     *
158     * @param sc stdIn scanner
159     * @return highway route number
160     */
161    private Integer getRouteNumber(Scanner sc) {
162        Integer routeNum = null;
163        Boolean verified = false;
164       
165        // validation loop
166        while(!verified)
167        {
168            // Prints out available route numbers to user to select from
169            System.out.print("Available route numbers: [");
170            for(Integer rtNum : routeNumInputList)
171            {
172                System.out.print(rtNum.toString() + ", ");
173            }
174            System.out.print("]");
175            System.out.println("");
176           
177            // Prompt user to input a route number
178            System.out.println("Enter a route number: ");
179            routeNum = sc.nextInt();
180            System.out.println("");
181           
182            // validate the user's input
183            if(routeNumInputList.contains(routeNum))
184            {
185                verified = true;
186            }
187            else
188            {
189                System.out.println("Invalid route number, please re-enter: ");
190            }
191        }
192       
193        return routeNum;
194    }
195   
196    /**
197     * Gets the highway direction from the user and validates the input.
198     *
199     * @param sc stdIn scanner
200     * @return highway direction
201     */
202    private DIRECTION getDirection(Scanner sc, Integer routeNum) {
203        DIRECTION direction;
204        String directionInput = null;
205        Boolean verified = false;
206       
207        // validation loop
208        while(!verified)
209        {
210            // Get available directions for route
211            ArrayList<DIRECTION> availDirs = new ArrayList<>();
212            for(Station stn : highways.getHighwayByRouteNumber(routeNum).stations)
213            {
214                if(!availDirs.contains(stn.direction))
215                {
216                    availDirs.add(stn.direction);
217                }
218            }
219           
220            // prompt user for input
221            System.out.print("Available directions for highway " + routeNum + ": [");
222            for(DIRECTION dir : availDirs)
223            {
224                System.out.print(dir.getLetter() + ", ");
225            }
226            System.out.print("]");
227            System.out.println("");
228            System.out.println("Enter a direction:");
229            directionInput = sc.next();
230            System.out.println("");
231           
232            // validate the user's input
233            if(availDirs.contains(DIRECTION.toDirection(directionInput)))
234            {
235                verified = true;
236            }
237            else
238            {
239                System.out.println("Invalid direction, please re-enter: ");
240            }
241        }
242       
243        return DIRECTION.toDirection(directionInput);
244    }
245   
246    /**
247     * Gets the starting/origin postmile value for the highway section from the
248     * user and validates the input.
249     *
250     * @param sc stdIn scanner
251     * @param routeNumber highway route number
252     * @param dir highway direction
253     * @return highway section start/origin postmile value
254     */
255    private Double getPostmile(Scanner sc, Integer routeNumber, DIRECTION dir) {
256        Double postmile = null;
257        Boolean verified = false;
258       
259        // validation loop
260        while(!verified)
261        {
262            // Get highway, and grab the floor and ceiling for postmile values
263            // from the highway stations to present to the user
264            Highway hwy = highways.getHighwayByRouteNumber(routeNumber);
265            Double floorPostmile = hwy.stations.get(0).postmile;
266            Double ceilPostmile = hwy.stations
267                    .get(hwy.stations.size() - 1).postmile;
268           
269            // present user with range of postmiles for given highway
270            System.out.println("Route " + hwy.routeNumber + " " + dir
271                    + " postmile range: [" + floorPostmile + ", " 
272                    + ceilPostmile + "]");
273           
274            // prompt user for postmile value
275            System.out.println("Enter a postmile value (Integer/Double): ");
276            postmile = sc.nextDouble();
277            System.out.println("");
278           
279            // validate user's input, ensures that the postmile is within given
280            // postmile range (floorPostmile, ceilPostmile)
281            if(postmile >= floorPostmile && postmile <= ceilPostmile)
282            {
283                verified = true;
284            }
285            else
286            {
287                System.out.println("Postmile must be within postmile range: [" + floorPostmile + ", " 
288                    + ceilPostmile + "] please re-enter: ");
289            }
290        }
291       
292        return postmile;
293    }
294   
295    /**
296     * Gets the range to extend the highway stretch from the start/origin postmile
297     * value from the user and validates the input.
298     *
299     * @param sc stdIn scanner
300     * @param postmile origin/start postmile value for highway stretch
301     * @return range value
302     */
303    private Integer getRange(Scanner sc, Double postmile) {
304        Integer range = null;
305        Boolean verified = false;
306       
307        // validation loop
308        while(!verified)
309        {
310            // prompt user for range value
311            System.out.println("Enter a range value (Integer):");
312            range = sc.nextInt();
313            System.out.println("");
314           
315            // range must be greater than or equal to 0
316            if(range >= 0)
317            {
318                verified = true;
319            }
320            else
321            {
322                System.out.println("Range must be >= 0");
323            }
324        }
325       
326        return range;
327    }
328
329    /**
330     * Gets the dot color from the user, to be applied to specified highway
331     * stretch and validates the user's input.
332     *
333     * @param sc stdIn scanner
334     * @return dot color to be applied to highway stretch
335     */
336    private DOTCOLOR getDotColor(Scanner sc) {
337        DOTCOLOR dotColor;
338        String dotColorInput = null;
339        Boolean verified = false;
340       
341        // validationloop
342        while(!verified)
343        {
344            // prompt user for color
345            System.out.println("Enter a dot color (G/Y/R):");
346            dotColorInput = sc.next();
347            System.out.println("");
348            // validate user's input
349            if(dotColorInputList.contains(dotColorInput))
350            {
351                verified = true;
352            }
353            else
354            {
355                System.out.println("Invalid dot color, please re-enter: ");
356            }
357        }
358       
359        return DOTCOLOR.toDotColor(dotColorInput);
360    }
361   
362    /**
363     * Enum for highway status dot colors.
364     *
365     * @author John A. Torres
366     * @version 10/11/2017
367     */
368    public static enum DOTCOLOR {
369
370        RED,
371        YELLOW,
372        GREEN;
373       
374        // All the first letters of the values, in order.
375        private static String allLetters = "RYG";
376       
377        /**
378         * Return the first letter of this enum.
379         *
380         * @return String first letter of this enum.
381         */
382        public String getLetter() {
383            return this.toString().substring(0, 1);
384        }
385
386        /**
387         * Returns a dot color given its first character.
388         *
389         * @param letter the first character of a dot color
390         * @return dot color corresponding to letter
391         * @pre letter must be one of allLetters
392         */
393        public static DOTCOLOR toDotColor(String letter) {
394            return values()[allLetters.indexOf(letter.charAt(0))];
395        }
396    } 
397}
Note: See TracBrowser for help on using the repository browser.