Java advanced--traffic light management system

Source: Internet
Author: User

This method simulates the situation of traffic lights in real life.

1. Build the road class first, this class can create a road in 12 directions

The code is as follows:

1  Packagecom.springtie.traffic;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 ImportJava.util.Random;6 ImportJava.util.concurrent.ExecutorService;7 Importjava.util.concurrent.Executors;8 ImportJava.util.concurrent.ScheduledExecutorService;9 ImportJava.util.concurrent.TimeUnit;Ten  One  Public classRoad { A     //Road names in 12 directions -     PrivateString Roadname; -     //collection of storage vehicles the     PrivateList<string> vehicle =NewArraylist<string>(); -  -     //Create a different path name depending on the direction -      PublicRoad (String roadname) { +          This. Roadname =Roadname; -         //Add 1000 cars to the current road +Executorservice pool =executors.newsinglethreadexecutor (); APool.execute (NewRunnable () { at              Public voidrun () { -                 Try { -Thread.Sleep ((NewRandom (). Nextint (10) + 1) * 1000); -}Catch(interruptedexception e) { - e.printstacktrace (); -                 } in                  for(intCar = 0; Car < 1000; car++) { -                     //Add the vehicle in . toVehicle.add (Roadname + "direction of vehicle" +car); +                     //System.out.println (roadname+ "direction of the vehicle" +car+ "is Coming"); -                 } the             } *         }); $         //current on the road, the light green, so that its most front of the vehicle 1 seconds after crossing the road, and then a second later came up to the front of the vehicle across the road, so circulation, know that the lights turn redPanax NotoginsengScheduledexecutorservice timer = Executors.newscheduledthreadpool (1); -Timer.scheduleatfixedrate (NewRunnable () { the              Public voidrun () { +                 if(!Vehicle.isempty ()) { A                     BooleanLampstate =lamp.valueof (roadname). islighted (); the                     if(lampstate) { +  -System.out.println (vehicle.remove (0) + "Crossing the Road"); $                     } $                 } -             } -}, 1, 1, timeunit.seconds); the     } -}

2. Create traffic lights enumeration lamp, pass the private constructor to the current lamp's corresponding light and the next light and the current state

The code is as follows:

1  Packagecom.springtie.traffic;2 3  Public enumLamp {4     //12 Semaphore enumeration Objects5S2n ("N2s", "s2w",false), s2w ("n2e", "e2w",false), e2w ("w2e", "E2s",false), E2s (6"W2n", "s2n",false), N2s (NULL,NULL,false), N2E (NULL,NULL,false), w2e (7             NULL,NULL,false), w2n (NULL,NULL,false), S2E (NULL,NULL,true), e2n (8             NULL,NULL,true), n2w (NULL,NULL,true), W2s (NULL,NULL,true);9     //Enumerate Private member variablesTen     PrivateString Oppositelamp; One     PrivateString Nextlamp; A     Private Booleanlampstate; -  -     //Private Constructors the     PrivateLamp (String Oppositelamp, String Nextlamp,Booleanlampstate) { -          This. Oppositelamp =Oppositelamp; -          This. Nextlamp =Nextlamp; -          This. lampstate =lampstate; +     } -  +     //Judging by a red or green light A      Public Booleanislighted () { at         returnlampstate; -     } -  -     //Turn the lights green and the lights turn green -      Public voidTurngreen () { -          This. lampstate =true; in         if(Oppositelamp! =NULL) { - lamp.valueof (Oppositelamp). Turngreen (); to         } +System.out.println ("Lamp:" + This. Name () + "green light, + corresponding and other four lights also for green"); -     } the  *     //Turn the lamp and the corresponding lamp red, while the next light turns green, returning to the next light $      PublicLamp turnred () {Panax Notoginseng          This. lampstate =false; -         if(Oppositelamp! =NULL) { the lamp.valueof (Oppositelamp). turnred (); +         } ALamp next =NULL; the         if(Nextlamp! =NULL) { +Next =lamp.valueof (nextlamp); -System.out.println ("green light:" + This. Name () + "switch to:" +next.name ()); $ Next.turngreen (); $         } -         returnNext; -     } the}

3. Create a light control system class Lampcontroller, using this class to control the order of lamp variables

The code is as follows:

1  Packagecom.springtie.traffic;2 3 Importjava.util.concurrent.Executors;4 ImportJava.util.concurrent.ScheduledExecutorService;5 ImportJava.util.concurrent.TimeUnit;6 7  Public classLampcontroller {8     PrivateLamp Currentlamp;9 Ten      PublicLampcontroller () { OneCurrentlamp =lamp.s2n; A         //Turn the current light to green - Currentlamp.turngreen (); -         //single Open a thread, the current light turns red while the next turn green, always repeat theScheduledexecutorservice timer = Executors.newscheduledthreadpool (1); -Timer.scheduleatfixedrate (NewRunnable () { -              Public voidrun () { -System.out.println ("When the current light turns red, return to the next light and turn it green"); +Currentlamp =currentlamp.turnred (); -             } +}, 10, 10, timeunit.seconds); A     } at}

4. Create a main class (MainClass) to test

The code is as follows:

1  Packagecom.springtie.traffic;2 3  Public classMainClass {4 5      Public Static voidMain (string[] args) {6         //represents the direction of 12 roads7string[] Directionroadname =NewString[] {"s2n", "s2w", "e2w", "E2s",8"N2s", "n2e", "w2e", "w2n", "s2e", "e2n", "n2w", "W2s" };9         //start the 12-path thread.Ten          for(inti = 0; i < directionroadname.length; i++) { One             NewRoad (Directionroadname[i]); A         } -         //Call Light control system -         NewLampcontroller (); the     } -  -}

At this point, a complete simulation of real-life traffic lights management system has been built.

Java advanced--traffic light management system

Related Article

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.