Dark Horse programmer _ traffic lights

Source: Internet
Author: User

-------------------- Android training, Java training, and hope to communicate with you! ----------------------

The traffic light management system simulates the intersection logic. Requirement 1. Route analysis. S --> NS --> ws --> E · 2. Ignore the yellow light and only consider the traffic lights. 3. The right-turn vehicle is not controlled. 4. vehicles in the North-South direction and east-west direction are allowed to turn directly in the same direction. 5. the vehicle passes through the intersection at 1 s. 6. The time interval between randomly generated vehicles and red-green control lights is customized. Object-Oriented Analysis and Design 1. Design a road class to represent a route 2. Each road object represents a route with 12 in total. 3. Add vehicles to a collection randomly on the route. 4. Continuously detect the first element in the set if the control light is green. 5. Check whether the lights on the road section are green. If the lights are red. 6. Design a lamp class to indicate the traffic lights and maintain the two states of red and green. Both methods can be red or green and return the status. 7. Four turn-on lights are designed to be evergreen. 8. The remaining eight lights are paired with each other. Each team is in the same status and polls the four lights. 9. Two variables are designed in the lamp class. One is used to remember the opposite lamp and the other is used to remember the next lamp. 10. The lamp in a certain direction is the same instance object, so it is convenient to use the lamp class to use enumeration.
11. Design a lampcontroller class to control the light changes. Programming example:

Package COM. isoftstone. interview. traffic; Public Enum lamp {s2n ("N2s", "s2w", false), s2w ("n2e", "e2w", false), e2w ("w2e ", "e2s", false), e2s ("w2n", "s2n", false), N2s (null, null, false), n2e (null, null, false ), w2e (null, null, false), w2n (null, null, false), 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 = opposit E; this. next = next; this. lighted = lighted;} private lamp () {} private Boolean lighted; private string opposite; private string next; Public Boolean islighted () {return lighted;} public void light () {This. lighted = true; If (opposite! = NULL) {lamp. valueof (opposite ). light ();} system. out. println (name () + "lamp is green, there are six directions for the car to pass");} public lamp blackout () {This. lighted = false; If (opposite! = NULL) {lamp. valueof (opposite). blackout ();} lamp nextlamp = NULL; If (next! = NULL) {nextlamp = lamp. valueof (next); nextlamp. light (); system. out. println ("green light from" + name () + "--> switch to" + next);} return nextlamp ;}} 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 () {currentlamp = lamp. s2n; currentlamp. light (); scheduledexecutorservice timer = executors. newscheduledthreadpool (1); // sets the timer. scheduleatfixedrate (New runnable () {public void run () {currentlamp = currentlamp. blackout () ;}}, 10, 10, timeunit. seconds) ;}} 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; public class road {list <string> vehicle = new arraylist <string> (); Private string name = NULL; public road (string name) {This. name = Name; executorservice pool = executors. newsinglethreadexecutor (); // Java 5 thread pool.exe cute (New runnable () {// create thread public void run () {for (INT I = 1; I <1000; I ++) {try {thread. sleep (new random (). nextint (10) + 1) * 1000);} catch (interruptedexception e) {// todo auto-generated Catch Block E. printstacktrace ();} vehicle. add (Road. this. name + "_" + I) ;}}}); scheduledexecutorservice timer = executors. newscheduledthreadpool (1); timer. scheduleatfixedrate (New runnable () {public void run () {If (vehicle. size ()> 0) {Boolean lighted = lamp. valueof (Road. this. name ). islighted (); If (lighted) {system. out. println (vehicle. remove (0) + "is traversing") ;}}}, 1, 1, timeunit. seconds) ;}} package COM. isoftstone. interview. traffic; public class mainclass {/*** @ Param ARGs */public static void main (string [] ARGs) {string [] direction = new string [] {"s2n ", "s2w", "e2w", "e2s", "N2s", "n2e", "w2e", "w2n", "s2e", "e2n", "n2w ", "w2n"}; For (INT I = 0; I <direction. length; I ++) {New Road (Direction [I]) ;}new lampcontroller ();}}

 

Iandroid training and Java training. We look forward to communicating with you! ---------------------- For details, please refer to: http://edu.csdn.net/heima

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.