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

Revision 104, 12.9 KB checked in by jdalbey, 9 years ago (diff)

atmsdriver/ConsoleDriver.java Added a main method.

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    private 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    }
153   
154    /**
155     * Gets the highway route number from user and validates the input.
156     *
157     * @param sc stdIn scanner
158     * @return highway route number
159     */
160    private Integer getRouteNumber(Scanner sc) {
161        Integer routeNum = null;
162        Boolean verified = false;
163       
164        // validation loop
165        while(!verified)
166        {
167            // Prints out available route numbers to user to select from
168            System.out.print("Available route numbers: [");
169            for(Integer rtNum : routeNumInputList)
170            {
171                System.out.print(rtNum.toString() + ", ");
172            }
173            System.out.print("]");
174            System.out.println("");
175           
176            // Prompt user to input a route number
177            System.out.println("Enter a route number: ");
178            routeNum = sc.nextInt();
179            System.out.println("");
180           
181            // validate the user's input
182            if(routeNumInputList.contains(routeNum))
183            {
184                verified = true;
185            }
186            else
187            {
188                System.out.println("Invalid route number, please re-enter: ");
189            }
190        }
191       
192        return routeNum;
193    }
194   
195    /**
196     * Gets the highway direction from the user and validates the input.
197     *
198     * @param sc stdIn scanner
199     * @return highway direction
200     */
201    private DIRECTION getDirection(Scanner sc, Integer routeNum) {
202        DIRECTION direction;
203        String directionInput = null;
204        Boolean verified = false;
205       
206        // validation loop
207        while(!verified)
208        {
209            // Get available directions for route
210            ArrayList<DIRECTION> availDirs = new ArrayList<>();
211            for(Station stn : highways.getHighwayByRouteNumber(routeNum).stations)
212            {
213                if(!availDirs.contains(stn.direction))
214                {
215                    availDirs.add(stn.direction);
216                }
217            }
218           
219            // prompt user for input
220            System.out.print("Available directions for highway " + routeNum + ": [");
221            for(DIRECTION dir : availDirs)
222            {
223                System.out.print(dir.getLetter() + ", ");
224            }
225            System.out.print("]");
226            System.out.println("");
227            System.out.println("Enter a direction:");
228            directionInput = sc.next();
229            System.out.println("");
230           
231            // validate the user's input
232            if(availDirs.contains(DIRECTION.toDirection(directionInput)))
233            {
234                verified = true;
235            }
236            else
237            {
238                System.out.println("Invalid direction, please re-enter: ");
239            }
240        }
241       
242        return DIRECTION.toDirection(directionInput);
243    }
244   
245    /**
246     * Gets the starting/origin postmile value for the highway section from the
247     * user and validates the input.
248     *
249     * @param sc stdIn scanner
250     * @param routeNumber highway route number
251     * @param dir highway direction
252     * @return highway section start/origin postmile value
253     */
254    private Double getPostmile(Scanner sc, Integer routeNumber, DIRECTION dir) {
255        Double postmile = null;
256        Boolean verified = false;
257       
258        // validation loop
259        while(!verified)
260        {
261            // Get highway, and grab the floor and ceiling for postmile values
262            // from the highway stations to present to the user
263            Highway hwy = highways.getHighwayByRouteNumber(routeNumber);
264            Double floorPostmile = hwy.stations.get(0).postmile;
265            Double ceilPostmile = hwy.stations
266                    .get(hwy.stations.size() - 1).postmile;
267           
268            // present user with range of postmiles for given highway
269            System.out.println("Route " + hwy.routeNumber + " " + dir
270                    + " postmile range: [" + floorPostmile + ", " 
271                    + ceilPostmile + "]");
272           
273            // prompt user for postmile value
274            System.out.println("Enter a postmile value (Integer/Double): ");
275            postmile = sc.nextDouble();
276            System.out.println("");
277           
278            // validate user's input, ensures that the postmile is within given
279            // postmile range (floorPostmile, ceilPostmile)
280            if(postmile >= floorPostmile && postmile <= ceilPostmile)
281            {
282                verified = true;
283            }
284            else
285            {
286                System.out.println("Postmile must be within postmile range: [" + floorPostmile + ", " 
287                    + ceilPostmile + "] please re-enter: ");
288            }
289        }
290       
291        return postmile;
292    }
293   
294    /**
295     * Gets the range to extend the highway stretch from the start/origin postmile
296     * value from the user and validates the input.
297     *
298     * @param sc stdIn scanner
299     * @param postmile origin/start postmile value for highway stretch
300     * @return range value
301     */
302    private Integer getRange(Scanner sc, Double postmile) {
303        Integer range = null;
304        Boolean verified = false;
305       
306        // validation loop
307        while(!verified)
308        {
309            // prompt user for range value
310            System.out.println("Enter a range value (Integer):");
311            range = sc.nextInt();
312            System.out.println("");
313           
314            // range must be greater than or equal to 0
315            if(range >= 0)
316            {
317                verified = true;
318            }
319            else
320            {
321                System.out.println("Range must be >= 0");
322            }
323        }
324       
325        return range;
326    }
327
328    /**
329     * Gets the dot color from the user, to be applied to specified highway
330     * stretch and validates the user's input.
331     *
332     * @param sc stdIn scanner
333     * @return dot color to be applied to highway stretch
334     */
335    private DOTCOLOR getDotColor(Scanner sc) {
336        DOTCOLOR dotColor;
337        String dotColorInput = null;
338        Boolean verified = false;
339       
340        // validationloop
341        while(!verified)
342        {
343            // prompt user for color
344            System.out.println("Enter a dot color (G/Y/R):");
345            dotColorInput = sc.next();
346            System.out.println("");
347            // validate user's input
348            if(dotColorInputList.contains(dotColorInput))
349            {
350                verified = true;
351            }
352            else
353            {
354                System.out.println("Invalid dot color, please re-enter: ");
355            }
356        }
357       
358        return DOTCOLOR.toDotColor(dotColorInput);
359    }
360   
361    /**
362     * Enum for highway status dot colors.
363     *
364     * @author John A. Torres
365     * @version 10/11/2017
366     */
367    public static enum DOTCOLOR {
368
369        RED,
370        YELLOW,
371        GREEN;
372       
373        // All the first letters of the values, in order.
374        private static String allLetters = "RYG";
375       
376        /**
377         * Return the first letter of this enum.
378         *
379         * @return String first letter of this enum.
380         */
381        public String getLetter() {
382            return this.toString().substring(0, 1);
383        }
384
385        /**
386         * Returns a dot color given its first character.
387         *
388         * @param letter the first character of a dot color
389         * @return dot color corresponding to letter
390         * @pre letter must be one of allLetters
391         */
392        public static DOTCOLOR toDotColor(String letter) {
393            return values()[allLetters.indexOf(letter.charAt(0))];
394        }
395    } 
396}
Note: See TracBrowser for help on using the repository browser.