Traffic light Management system-experience 1-Project requirements analysis and drawing analysis

Source: Internet
Author: User
Tags time interval

1. Traffic light Management System project requirements

Simulation to achieve the intersection of traffic lights management system logic, the specific requirements are as follows:

Ø randomly generated vehicles that follow each route.

For example:

Vehicles coming from the south to the north----straight vehicles.

Vehicles from the west to the south----turn right

Vehicles from east to south----left-turn vehicles

。。。

The signal lights ignore the yellow light, only consider red light and green light.

Ø left-turn vehicle control signal should be considered, right turn vehicle is not controlled by signal light.

Ø the control logic of specific signal lights is the same as that of ordinary traffic lights in real life, regardless of the control logic under special circumstances.

Note: North-south to vehicles and things to the vehicle alternately release, the same direction waiting for the vehicle should first release straight vehicles and then release left-turn vehicles.

Ø each car through the intersection time of 1 seconds (hint: can be simulated through the way of thread sleep).

Ø randomly generated vehicle time interval as well as the traffic light exchange time interval from the set, you can setup.

Ø does not require the implementation of the GUI, only consider the system logic implementation, can display the results of the program log.

2. Don't dream, be sure to paint

Drawing is very helpful in understanding and analyzing problems, do you have a better way to do it than paint?

There are a total of 12 routes, in order to unify the programming model, you can assume that each route has a traffic light to control it, the right turn of the 4 line of control lights can be assumed to be called the Evergreen State, in addition, the other 8 lines are 22 pairs, can be grouped into 4 groups, so, The program only needs to consider the switching order of the control lights in the 4 routes marked with the number in the figure, and the control lights of the 4 routes in the opposite direction follow these 4 routes without extra consideration.

detailed analysis and code writing are as follows:

/**

* Each lamp element represents a light in one direction, with a total of 12 directions, all a total of 12 lamp elements.

* There are some lights in the direction, each two form a group, a group of lights to turn green or red, so,

* Program code to control only one light in each group can be:

* S2N,N2S

* S2W,N2E

* E2W,W2E

* E2S,W2N

* s2e,n2w

* E2n,w2s

* The lights on the last two lines are virtual, because they are not controlled by the traffic lights from the south to the east and from the west, so they can be assumed to be always green.

*/

/* Each enumeration element represents a control light in one direction.

S2n ("N2s", "s2w",false), s2w ("e2n", "e2w",false), e2w ("w2e", "E2s",false), E2s ("w2n", "s2n") ",false),

/* The following elements represent the lights in the opposite direction of the above elements, and their "opposite direction light" and "next light" should be ignored. */

N2S (null,null,false), n2e (null,null,false), W2E (null ,null,false), w2n (null,null,false),

* * from south to east and from west to north, such as the right corner of the lamp is not controlled by the traffic lights, because these four directions are the right hand direction,

* In China's real life, the vehicle on the right, so the right-hand side of the vehicle does not need to control the traffic lights can walk vehicles

* Will not conflict with other vehicles on the road; So, you can assume they always have green light.

S2E (null,null,true), n2w (null,null,true), W2s (null ,null,true), e2n (null,null,true);

3. Object-oriented analysis and design

there will be multiple vehicles on each route, a random addition of new cars on the route, and a reduction in the number of vehicles per second during light green.

Ø design a road class to represent the route, each road object represents a route, a total of 12 routes, that is, the system will produce a total of 12 road instance objects.

Ø a random addition of new vehicles on each route, added to a collection to save.

Ø every second will check the control of the route of the light is green, is the route to save the vehicle in the collection of the first car removed, that means the car through the intersection.

L every second will check the control of the route of the light is green, a lamp from green to red, should be the next direction of the lamp green.

Ø design a lamp class to represent a traffic light, each traffic light maintains a state: bright (green) or not bright (red), each traffic light has to lighten and darken the method, and can return to their own bright black state.

Ø a total of 12 routes, so the system will produce a total of 12 traffic lights. The right corner of the route was not controlled by the lamp, but in order to allow the program to adopt a unified approach, it is assumed that there are four lights on the right turn, but these lights for the illuminated steady state, that is, never black (red).

Ø In addition to the right turning direction of the other 8 lines of light, they are 22 pairs, can be grouped into 4 groups, so, in the programming process, as long as from each of the 4 groups out of a light, the 4 lights turn to turn the light, and the 4 light direction corresponding to the lamp will change with it, So there is a variable in the lamp class to remember the lamp in the opposite direction, and in the light and darken method of a Lamp object, the light in the corresponding direction is also lightened and darkened. Each lamp turns black, it is accompanied by the next lamp light, lamp class also use a variable to remember their next lamp.

Ø no matter where you go to get a light in a certain direction, each time you get the same instance object, the lamp class instead of an enumeration is obviously very handy, and always has only an instance object that represents a 12-direction lamp.

Ø Design a Lampcontroller class, which is timed to turn the current green light red.

Object-oriented explanation and tips:

We tentatively envisage what objects: traffic lights, control systems of traffic lights, automobiles, routes. The car went through the intersection when he saw the light on its own line green. No, we also need to see whether there is a car in front of it, to see if there is a car, which object should be asked. The way to the road, the road is stored in a collection of vehicles, it is obvious that the road should be to increase the number of vehicles and reduce vehicles. Look at the topic, we do not have to reflect the process of vehicle movement, but to capture the vehicle through the intersection process, that is, to capture the road to reduce the process of a car, so the car does not need to be designed as a separate object, with a string representation can be.

Object-oriented design grasps an important experience: who owns the data, and who provides the means to manipulate the data. A few typical cases can be firmly mastered : people on the blackboard to draw a circle, the train driver emergency brake, you shut the door and so on.

4. Introduction to code implementation (main class analysis)

preparation of road class

Each road object has a name member variable to represent the direction, and a vehicles member variable to represent the vehicle collection in the direction.

Start a thread in the road object's construction method to add a car to the vehicles collection at random intervals (represented by a string in the form of a "route name _id").

Starts a timer in the road object's construction method, checks every second to see if the light in that direction is green, prints the collection of vehicles, and removes the first car in the collection.

L Lamp-type Preparation

The system has 12 directions of lights, in the other parts of the program can be based on the name of the lamp to obtain the corresponding lamp instance object, synthesizing these factors, the lamp class with the enumeration form in Java5 is simpler.

The bright black state in each lamp object is represented by a lighted variable, and the lamp objects in the four directions of s2n, s2w, e2w and e2n are then polled to brighten, and the lamp object must have a Oppositelampname variable to represent the lights in the opposite direction, Then use a nextlampname variable to indicate the next bright light after the light is lightened. These three variables are assigned in the form of a construction method, because the enumeration elements must be referenced after the definition, so no more references to each other in the method can be constructed, so the lights in the opposite direction and the next direction are represented as strings.

Increase the way to brighten and darken lamp: light and blackout, for lamp objects in the four directions of s2n, s2w, e2w, and e2n, the interior of the two methods will brighten and darken the lights in the opposite direction, and the Blackout method will also make the next light brighter.

In addition to the lamp objects in the four directions of s2n, s2w, e2w, e2n, the Nextlampname and Oppositelampname properties of the lamp objects in the other direction are set to NULL, and s2n, s2w, e2w, E2n the Nextlampname and Oppositelampname properties of the lamp objects in these four directions must be set to NULL to prevent light and blackout from entering the dead loop.

Preparation of Lampcontroller class

The entire system can only have a set of traffic light control system, so the Lampcontroller class is best designed as a single case (that is, a private variable an external construction method).

The first green lamp is set in the Lampcontroller construction method.

The Lampcontroller object's Start method turns the current light green, then starts a timer, turns the current light red and turns the next light green every 10 seconds.

Preparation of MainClass class

Creates an object representing 12 routes with the For loop.

The Lampcontroller object is then obtained and the Start method is invoked.

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.