1. participate in the analysis of the traffic light system requirements of instructor Zhang. The targets include road, lamp, and lamp controller.
Lamp (implemented by enumeration)
Package com. isoftstone. Interview. Traffic;/*** each lamp element represents a lamp in one direction, with 12 directions in total and 12 lamp elements in total. * There are lights in the following directions, each of which forms a group, and a group of lights turn green or red at the same time. Therefore, * the program code only needs to control one of the lights in each group: * s2n, n2s * s2w, n2e * e2w, w2e * e2s, w2n * s2e, n2w * e2n, w2s * The lights on the last two lines are virtual, since the south to the east and the west to the north, and their corresponding directions are not controlled by traffic lights, * We can assume that they are always green lights. * @ Author Zhang Xiaoxiang www.it315.org ** // **/Public Enum lamp {/* Each enumeration element represents a direction control lamp */s2n ("N2s", "s2w ", false), s2w ("n2e", "e2w", false), e2w ("w2e", "e2s", false), e2s ("w2n", "s2n ", false),/* the following elements indicate the lights in the opposite direction of the above elements. Their "Opposite Direction lights" and "next lights" should be ignored! */N2s (null, null, false), n2e (null, null, false), w2e (null, null, false), w2n (null, null, false ), /* right turns from the south to the east and from the west to the north are not controlled by traffic lights. Therefore, we can assume that they are always green lights */s2e (null, null, true), e2n (null, null, true), n2w (null, null, true), w2s (null, null, true); Private lamp (string opposite, string next, Boolean lighted) {This. opposite = opposite; this. next = next; this. lighted = lighted;}/* whether the current light is green */private Boolean lighted;/* The direction corresponding to the green light at the same time */private string OPP Osite;/* The next green light when the current light turns red */private string next; Public Boolean islighted () {return lighted;}/*** when a light turns green, the light in the corresponding direction must also turn green */Public void light () {This. lighted = true; If (opposite! = NULL) {lamp. valueof (opposite ). light ();} system. out. println (name () + "lamp is green, there should be a total of 6 directions to see the car pass! ");}/*** When a light turns red, the light turns red in the corresponding direction, and the lights in the next direction need to turn green * @ return next the lights to turn green */public lamp blackout () {This. lighted = false; If (opposite! = NULL) {lamp. valueof (opposite). blackout ();} lamp nextlamp = NULL; If (next! = NULL) {nextlamp = lamp. valueof (next); system. out. println ("green light from" + name () + "--------> switch to" + next); nextlamp. light () ;}return nextlamp ;}}
Road
Package COM. isoftstone. interview. traffic; import Java. util. arraylist; import Java. util. list; import Java. util. random; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. scheduledexecutorservice; import Java. util. concurrent. timeunit;/*** each road object represents a route. There are 12 routes in total, that is, a total of 12 road instance objects are generated in the system. * A new vehicle is randomly added to each route and added to a collection for saving. * Every second of each route, the system checks whether the lights on the current route are green. If yes, the first car in the set of vehicles stored in the current route is removed, indicating that the car passes through the intersection. * @ Author Zhang Xiaoxiang www.it315.org **/public class road {private list <string> vechicles = new arraylist <string> (); Private string name = NULL; public road (string name) {This. name = Name; // simulate the process of random vehicles on the road executorservice pool = executors.newsinglethreadexecutor(registry.pool.exe cute (New runnable () {public void run () {for (INT I = 1; I <1000; I ++) {try {thread. sleep (new random (). nextint (10) + 1) * 1000);} catch (interruptedexc Eption e) {e. printstacktrace ();} vechicles. add (Road. this. name + "_" + I) ;}}); // check whether the corresponding lamp is green every second. If yes, release a car scheduledexecutorservice timer = executors. newscheduledthreadpool (1); timer. scheduleatfixedrate (New runnable () {public void run () {If (vechicles. size ()> 0) {Boolean lighted = lamp. valueof (Road. this. name ). islighted (); If (lighted) {system. out. println (vechicles. remove (0) + "is traversing! ") ;}}}, 1, 1, timeunit. Seconds );}}
Lamp Controller
Package COM. isoftstone. interview. traffic; import Java. util. concurrent. executors; import Java. util. concurrent. scheduledexecutorservice; import Java. util. concurrent. timeunit; public class lampcontroller {private lamp currentlamp; Public lampcontroller () {// green the lampcontroller from south to north at the beginning; currentlamp = lamp. s2n; currentlamp. light ();/* change the current green light to a red light every 10 seconds and change the light in the next direction to green */scheduledexecutorservice timer = executors. newscheduledthreadpool (1); timer. scheduleatfixedrate (New runnable () {public void run () {system. out. println ("come on"); currentlamp = currentlamp. blackout () ;}}, 10, 10, timeunit. seconds );}}
Test class
Package COM. isoftstone. interview. traffic; public class mainclass {/*** @ Param ARGs */public static void main (string [] ARGs) {/* generate 12 directions */string [] directions = new string [] {"s2n", "s2w", "e2w", "e2s ", "N2s", "n2e", "w2e", "w2n", "s2e", "e2n", "n2w", "w2s"}; For (INT I = 0; I <directions. length; I ++) {New Road (directions [I]);}/* generate the entire traffic light system */New lampcontroller ();}}