Dark Horse programmer traffic light Management System

Source: Internet
Author: User

 

----------- Android training, Java training, Java learning technology blog, we look forward to communicating with you! ------------

According to the needs of the traffic light Management System: design a road class to express the route. A road object represents the route, with a total of 12 routes. A new vehicle is randomly added to each route and saved in a collection. Every second, each route checks whether the lights that control the current route are green. If yes, the first car in the car's set stored in the current route is removed, indicating that the car passes through the intersection.

Public class road { Private list <string> vechicles = new arraylist <string> (); // This set contains vehicles.  Private string name = NULL;  Public Road (string name ){  This. Name = Name;      Executorservice pool = executors. newsinglethreadexecutor (); // Add a thread to randomly Add a vehicle  Pool.exe cute (New runnable (){  @ Override  Public void run (){  For (INT I = 1; I <1000; I ++ ){  Try {  Thread. Sleep (new random (). nextint (10) + 1) * 1000 );  } Catch (interruptedexception ex ){  Logger. getlogger (Road. Class. getname (). Log (level. Severe, null, ex );  }  Vechicles. Add (road. This. Name + "_" + I );  }  }      });  Scheduledexecutorservice Timer = executors. newscheduledthreadpool (1); // Add a thread to check whether the route is green every 1 second. If there is a green light, a car will drive away.  Timer. scheduleatfixedrate (  New runnable (){  @ Override  Public void run (){  If (vechicles. Size ()> 0 ){  Boolean lighted = lamp. valueof (road. This. Name). islighted ();  If (lighted = true ){  System. Out. println (vechicles. Remove (0) + "is traversing! ");  }  }  }   },  1,  1,  Timeunit. Seconds );  }Design a lamp class to represent a traffic light. Each traffic light maintains a state: Bright (green) or not bright (red ). There are 12 routes in total, and a total of 12 traffic lights will be generated. The route of the right turn is not controlled by the light, but in order to let the program adopt a unified processing method, it is assumed that there are four right turn lights, but these lights are always on, that is, it never changes. The lights of the other eight routes are paired with each other and can be grouped into four groups. One lamp is taken from each of the four groups, and the four lights are turned on in round-robin mode. The lights corresponding to the four lights change with each other, therefore, there must be a variable in the lamp class to remember the lights in the opposite direction. In the method of brightening and blackening a lamp object, the lights in the corresponding direction are also brightened and dimmed. When the light turns black, it is followed by the light of the next light, and a variable is used in the lamp class to remember its next light. No matter where the program gets the lights in a certain direction, each time it gets the same instance object, the lamp class uses enumeration. There are always only instance objects that represent the lights in 12 directions.
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 ); // 12 lamps (opposite lamps, next lamps, and status of lamps)    Private Boolean lighted;  Private string opposite;  Private string next;      Private lamp (string opposite, string next, Boolean lighted) {// you must set the lamp parameters when creating the lamp.  This. Opposite = opposite;  This. Next = next;  This. lighted = lighted;  }  Public Boolean islighted () {// lamp status  Return lighted;  }  Public void light () {// light the other party's light at the same time  This. lighted = true;  If (opposite! = NULL ){  Lamp. valueof (opposite). Light ();  }  System. Out. println (name () + "lamp is gree. There should be a total of six directions to see the car going through ");  }  Public lamp blackout () {// black the light, black the opposite Light, light the next light and return the light  This. lighted = false;  If (opposite! = NULL ){  Lamp. valueof (opposite). blackout ();  }  Lamp nextlamp = NULL;  If (next! = NULL ){  Nextlamp = lamp. valueof (next );  Nextlamp. Light ();  }  Return nextlamp;  }Design a lampcontroller class, which regularly turns the current green light into red .} Public class lampcontroller { Private lamp currentlamp; // transmits the lamp in      Public lampcontroller () {// lights up a set of lights in the constructor, creates a timer, blackens itself every 10 seconds, and lights up the next set of lights.  Currentlamp = lamp. s2n;  Currentlamp. Light ();  Scheduledexecutorservice Timer = executors. newscheduledthreadpool (1 );  Timer. scheduleatfixedrate (  New runnable (){
  @ Override  Public void run (){  Currentlamp = currentlamp. blackout ();  }      },  10,  10,  Timeunit. Seconds );      }Create a class to test the public class mainclass { Public static void main (string [] ARGs ){  String [] directions = new string [] {  "S2n", "s2w", "e2w", "e2s", "N2s", "n2e", "w2e", "w2n", "s2e", "e2n ", "n2w", "w2s" // put 12 lamps into an array  };  For (INT I = 0; I <directions. length; I ++) {// create 12 paths and place the lights in each path  New Road (directions [I]);  }      New lampcontroller (); // create a Light Controller and start working.  }} That's all I learned about the traffic lights ----------- Android training, Java training, Java learning technology blog, we look forward to communicating with you! ------------

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.