Dark Horse programmer traffic light management system learning

Source: Internet
Author: User

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

 

Object-Oriented Design Ideas

Object-Oriented Design ideas:

When there is a problem, analyze the problem: 1. What types are there? 2. What attributes and methods are there in the class. 3. The relationship between the class and the class.

 

A classic statement that summarizes the idea of object-oriented analysis:

Anyone who owns the data can access the data externally.
1. The ball moves from one end of the rope to the other end. analysis: the ball and the rope are classes. The ball has attributes: the rope, that is, the rope on which the ball moves; point. The ball has a method: move; the rope has a property: the two ends of the rope point; the rope has a method: the next position. */Class rope {private point start; private point end; rope (point start, Point End) {This. start = start; this. end = end;} public point nextpoint (point currentpoint) {If (currentpoint. equals (end) {return NULL;} If (! (Currentpoint. ispointofrope () {Throw new exception ("this point is not in a straight line");} // calculate the position of the next vertex nextpointreturn nextpoint;} class ball {private rope; private point currentpoint; ball (rope, point currentpoint) {This. rope = rope; this. currentpoint = currentpoint;} public void move () {currentpoint = rope. nextpoint (currentpoint); system. out. println ("moved... "+ currentpoint );}}

 

/* The following scenario is designed in an object-oriented way. 2. The two stones are ground into a stone knife, the stone knife is used to cut the tree into wood, and the wood is used as a chair. * // * Stonestoneknife = stonefactory. produceknife (Stone); treewood = stoneknife. Cut (tree); Chair = woodfactory. makechair (wood );*/

 

Traffic light system requirements:

The specific requirements are as follows:

  • Asynchronously and randomly generate vehicles traveling along each route.

For example:

      Vehicles from south to north-direct vehicles

      Vehicles going from West to south-turn right

      Vehicles from the east to the south-turn vehicles

      ...

  • Ignore the yellow light, only the red light and the green light are considered.
  • A left-turn vehicle control signal light should be considered, and a right-turn vehicle is not controlled by the signal light.
  • The specific traffic signal control logic is the same as that of common traffic signals in real life.

Note: vehicles in the North-South direction and vehicles in the east-west direction are allowed to interchangeably. vehicles in the same direction should be allowed to go directly and then turn left.

  • The intersection time for each vehicle to pass is 1 second (Prompt: simulation can be performed by thread sleep ).
  • You can set the random generation interval and traffic light switching interval.
  • GUI is not required. You only need to implement the system logic. You can use the LOG method to display the program running results.

 

 

Requirements for reading questions: the traffic light ignores the yellow light, only the red light and the green light are considered, so there are only two States.

Referring to the crossroads and traffic conditions in daily life, we learned that there are 12 vehicle routes in total.

 

 

 

Train of Thought Analysis: Look for classes, lights, controllers, roads (because vehicles on the road come and go, there are incoming and outgoing, so this can be used as data)

Lamp:

1. There should be 12 street lamps with a fixed number of 12 routes, which can be represented by Enumeration type for convenient operation;

2. There is a method to control the red light, to control the method of changing the green light, and to determine whether it is a green light;

3. When the red light is on, its next light should turn green;

4. Four main lines control the signal lights of the four corresponding lines;

5. There is no limit on the right turn of the vehicle, so the right turn signal should be fully bright.

 

The Code is as follows:

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 Boolean lighted; // lamp status; true: green; false: Red private string next; // The next lamp private string opposite; // The opposite light // the light Private lamp (string opposite, string next, Boolean lighted) {This. opposite = opposite; this. next = next; this. lighted = lighted;} // determines whether the light is green. Public Boolean islighted () {return lighted;} // when the light is green, the light opposite to it must also be green public void light () {This. lighted = true; If (opposite! = NULL) {lamp. valueof (opposite ). light (); // recursive;} system. out. println (name () + "lamp is green, there should be a total of 6 directions to see the car pass! ") ;}// When a light is a red light, the light opposite it should also turn red, and the next light should turn green, and return to the next light public lamp lightout () {This. lighted = false; If (opposite! = NULL) {lamp. valueof (opposite). lightout () ;}// the next lamp must be green. Lamp nextlamp = NULL; If (next! = NULL) {nextlamp = lamp. valueof (next); system. out. println ("green light from" + name () + "--------> switch to" + next); nextlamp. light () ;}return nextlamp ;}}

Controller:

1. When the controller is started, the light on a route should turn green.

2. There should be a timer in the Controller to control the light status, that is, to control the traffic light interval.

The Code is as follows:

Import Java. util. concurrent. executors; import Java. util. concurrent. scheduledexecutorservice; import Java. util. concurrent. timeunit; // Traffic Light Controller public class lampcontroller {private lamp currentlamp; Public lampcontroller () {// initialize the current light and turn it green; currentlamp = lamp. s2n; currentlamp. light (); // Timer: The current light turns red every 10 seconds and the next light turns green; scheduledexecutorservice timer = executors. newscheduledthreadpool (1); timer. scheduleatfixedrate (// new runnable () {@ overridepublic void run () {system. out. println (system. currenttimemillis (); // return the next lamp as the current lamp currentlamp = currentlamp. lightout () ;}}, 10, // the delay time for the first execution is 10, // the cycle is 10 seconds timeunit. seconds // unit of the cycle );}}

Route:

1. The direction of the route should be the same as that of the traffic signal.
2. vehicles should be randomly generated on the route, similar to the actual situation

3. There should also be a timer on the route. Every second, you can check whether the route can pass.

Note: When releasing a vehicle, you should first determine whether there is a vehicle on the road.

The Code is as follows:

Import Java. util. *; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. scheduledexecutorservice; import Java. util. concurrent. timeunit; public class road {private string name; List <string> vehicles = new arraylist <string> (); Public Road (string name) {This. name = Name; // generate a random vehicle executorservice pool = executors.newsinglethreadexecutor(registry.pool.exe cute (New runnable () {@ overridepublic void run () {for (INT I = 0; I <1000; I ++) {try {thread. sleep (new random (). nextint (10) + 1) * 1000); // random master algorithm} catch (interruptedexception e) {e. printstacktrace ();} vehicles. add (Road. this. name + "----" + I); // Add to "Collection" on a certain road}); // checks whether the light on the current line is green once every second, yes, the vehicle is allowed. // use the timer scheduledexecutorservice timer = executors. newscheduledthreadpool (1); timer. scheduleatfixedrate (New runnable () {@ ove Rridepublic void run () {If (vehicles. size ()> 0) {// a car has a Boolean lighted = lamp. valueof (Road. this. name ). islighted (); // If (lighted) {system. out. println (vehicles. remove (0) + "... is going "); // allow }}}, 1, 1, // period 1 second, view once in one second. Timeunit. Seconds );}}

Main function:

1. Generate 12 routes

2. Start the control system.

The Code is as follows:

Public class mainclass {public static void main (string [] ARGs) {string [] directions = {"s2n", "s2w", "e2w", "e2s ", "N2s", "n2e", "w2e", "w2n", "s2e", "e2n", "n2w", "w2s "}; // generate 12 lines for (INT x = 0; x <directions. length; X ++) {New Road (directions [x]) ;}// start the traffic light system new lampcontroller ();}}

Effect:

N2s lamp is green. There should be a total of six directions to see the car pass through! S2n lamp is green. There should be a total of six directions to see the car pass through! W2S----0... is goings2n ---- 0... is goingn2s ---- 0... is goingn2s ---- 1... is goings2e ---- 0... is goingw2s ---- 1... is goinge2n ---- 0... is goings2n ---- 1... is goingn2s ---- 2... is goings2e ---- 1... is goingn2w ---- 0... is goingn2w ---- 1... is going1343915995234 green light from s2n --------> switch to s2wn2e lamp is green. There should be 6 directions in total to see the car pass through! S2w lamp is green. A total of six directions can be seen below! S2W----0... is goingn2e ---- 0... is goings2w ---- 1... is goings2w ---- 2... is goingn2e ---- 1... is goings2e ---- 2... is goinge2n ---- 1... is goings2w ---- 3... is goingw2s ---- 2... is goings2w ---- 4... is goingn2e ---- 2... is goingn2w ---- 2... is goingn2e ---- 3... is going1343916005234

 

 

 

 

 

 

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

For details, see: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.