java-Traffic light Management system "ten"

Source: Internet
Author: User

1, traffic light management system principle and analysis

First understand how it works, because just learn the car, probably understand how traffic lights operate, generally speaking the right turn is the default do not see the lights, you can directly turn right,

But sometimes it's different when the traffic has arrows, so we don't think about that. Then the default right turn light is always green. According to the four directions of the cardinal

Cars have their own three kinds of routes, according to the truth, the four directions have their own three direction of traffic lights. There are three (3x4) routes to consider in terms of the car, and

the corresponding traffic lights also have A A. Now we are going to discuss it in one Direction, for example, inthe direction of South, there are three kinds of cases, turning right (that is, south to east) is not necessary,

So left straight (south to north) and left (south to west), assuming that the light is green, then the corresponding direction (north to south) will also turn green. The straight-line light turns red, then the corresponding direction

(North to South) also to turn red, and the next light left (south to west) is going to turn green. The other direction is the same analysis.

2. Analysis of vehicle and lamp and lamp control

Car: The cars of the three routes each time on the route to add a car, every second check the current direction of the light is green, how to turn green the current direction of the car will be removed.

Lamp: The light in the direction of the South , in three kinds of circumstances, turn right (that is, south to east) is not necessary, So left straight (south to north) and left (south to west),

Assuming that the straight light turns green, the corresponding direction (north to south) also turns green. When the straight light turns red, the corresponding direction (north to south) also turns red, and the next light turns left (south to west) to turn green. So basically each direction of the lamp to correlate two related directions of the lights.

Road: There are a total of nine routes, each with the same direction of the car corresponding.

3, the object-oriented idea design traffic light system

(1) Lamp: It is obvious that this is a custom class, and the best way to do this is to implement the Java enumeration class.

each Lamp the element represents a light in one direction, and a total of A direction, all of which have a total A a Lamp element.

There are some directions on the lights , each of the two sets, a group of lights turn green or red at the same time, so the program code only needs to control one light in each group of lights: s2n,n2s s2w,n2e e2w,w2e

e2s,w2n s2e,n2w e2n,w2s. The lights on the last two lines above are virtual, as they are not controlled by traffic lights from south to east and from west to north, and their corresponding directions

So, it can be assumed that they are always green. So there are a total of three lamp elements, in fact, the use of 4 sets of lights that is four direction of the lamp. the way the lamp element is constructed should have been analyzed above .

the two associated directions of the lamp, there is also a value of their own lamp state ( false or true ). Lights should have a way of judging their own light state, as well as ways to turn green and turn red.

The code for the lamp is as follows:

public enum Lamp {/* Each enumeration element represents a control light of One Direction */s2n ("N2s", "s2w", false), s2w ("n2e", "e2w", false), e2w ("w2e", "E2s", false), E2s (" W2n "," s2n ", false),/* The following elements represent lights in the opposite direction of the above elements, their" opposite direction lights "and" next light "should be ignored! */n2s (Null,null,false), n2e (Null,null,false), w2e (Null,null,false), w2n (Null,null,false),/* The lights from the south to the east and the right turn from the west to the north are not controlled by traffic lights, so you can imagine that they are always green */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 = Opposite;this.next = next;this.lighted =  Lighted;} /* Whether the current light is a green */private boolean lighted;/* corresponding to the current light at the same time as green */private string opposite;/* The current light turns red when a green light */private string next; public Boolean islighted () {return lighted;}/** * When a light turns green, it will turn green */public void Light () {this.lighted = True;if (opposit E! = null) {lamp.valueof (opposite). Light ();} SYSTEM.OUT.PRINTLN (name () + "lamp is green, there should be a total of 6 directions below to see the car through!"  "); }/** * When a lamp turns red, the light in the direction will turn red, and the light in the next direction will turn green * @return The next light to turn green */public lamp BlackOut () {this.lighted = False;if (opposite! = Nu ll) {lamp.valueof (OpposiTE). BlackOut ();} Lamp nextlamp= null;if (next! = null) {Nextlamp = lamp.valueof (next); System.out.println ("green light from" + name () + "--------> Switch to" + next); Nextlamp.light ();}  return nextlamp;}}

(2) Route: A total of nine routes, that is, to produce a few instances of the object, each route on a random increase in new vehicles, and the car can be added to a collection to save.

for processing. Every second of each route checks whether the light that controls the route is green, and the first car in the collection of the course save car is removed, which means that the car has crossed the road .

Road of the class, the road has its own route name, and the car on the route name should be the same, are used in string representations, such as "s2n", the vehicle constantly randomly on the road to the process is best to use the thread to achieve,

and the process of adding a vehicle can be used for Loop to achieve. Since there is a vehicle to be added to remove the vehicle, because it is to check the current route in a second light is green, so also to use the thread to achieve,

Determine if the current route light is green, and if it is green, remove the front vehicle, the first car.

The code for the road is as follows:

public class Road {private list<string> vechicles = new arraylist<string   > ();  Private String name =null;public Road (String name) {this.name = name; The process of simulating a vehicle's constant random road executorservice pool = Executors.newsinglethreadexecutor ();p Ool.execute (new Runnable () {public void Run () {for (int i=1;i<1000;i++) {try {thread.sleep (New Random (). Nextint () + 1) * +)} catch (Interruptedexception e) {e.printstacktrace ();} Vechicles.add (Road.this.name + "_" + i);}} }); Check if the corresponding light is green every second, then release a car scheduledexecutorservice timer = executors.newscheduledthreadpool (1); Timer.scheduleatfixedrate (New Runnable () {public void run () {if (Vechicles.size () >0) {Boolean lighted = lamp.valueof ( Road.this.name). islighted (); if (lighted) {System.out.println (vechicles.remove (0) + "is traversing!");}}}},1,1, Timeunit.seconds); }}

(3) Lamp control class: Every ten seconds the current green light into a red, and let the next direction of the lights turn green. So it has to be done by thread.

The code is as follows:

public class Lampcontroller {private lamp currentlamp;public Lampcontroller () {//Just start to turn the light from south to north to green; currentlamp = lamp.s2n; Currentlamp.light ()/* * Turn the current green light to red at every 10 seconds and turn the light in the next direction to green */scheduledexecutorservice timer = Executors.newscheduledthreadpool (1); Timer.scheduleatfixedrate (new Runnable () {public  void run () { System.out.println ("Come"); Currentlamp = Currentlamp.blackout ();}},10,10,timeunit.seconds);}}

(4) Main function:

public class MainClass {public  static void Main (string[] args) {/* generates a 12-direction route */string [] directions = new string[]{"S 2N "," s2w "," e2w "," E2s "," N2s "," n2e "," w2e "," w2n "," s2e "," e2n "," n2w "," W2s "};for (int i=0;i<directions.length;i++) { New Road (Directions[i]); /* Generate entire traffic light system */new Lampcontroller ();}}

  

java-Traffic light Management system "ten"

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.