Dark Horse programmer java-Traffic Light Management system "ten"

Source: Internet
Author: User

-- Java training, Android training, iOS training,. NET training , look forward to communicating with you! --

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 element represents a light in a direction, with a total of 12 All directions, all in all 12 Span style= "font-family: ' The song Body '; > 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, you can pretend they're always green. So there are a total of Span style= "font-family: ' Times New Roman ';" >lamp element, actually using 4 lamp The constructor of the element should have the above analysis

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 directional control light */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 disregarded! */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 lamp is green */private boolean lighted;/* and the current lamp is green at the same time the corresponding direction */private string opposite;/* Current light turns red nowadays a green lamp */private string next;public boolean islighted () {return lighted;}  /** *  When a light turns green, the light that corresponds to the direction will also turn green  */public void light () {this.lighted = true;if (Opposite != null) {lamp.valueof (opposite). Light ();} SYSTEM.OUT.PRINTLN (name ()  + " lamp is green, there should be a total of 6 directions to see the car through!" ;  }  /** *  when a lamp turns red, the light in the direction should 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 != null) { 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;  //simulates a vehicle's ongoing random journey 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)  * 1000);}  catch  (interruptedexception e)  {e.printstacktrace ();} Vechicles.add (road.this.name +  "_"  + i);}} &NBSP);  //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 On"); Currentlamp = Currentlamp.blackout ();}},10,10,timeunit.seconds);}}

(4) start the program :

public class MainClass {public static void main (string[] args) {/* generates a 12-direction route */string [] directions = new string[]{"S2n" , "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 ();}}

Summary:

Difficulties:

1. The business logic of the transport system is not easy to understand.

2. There are a lot of traffic situations to be handled, and the enumeration classes need to be used to refine each case

3. How to think about object-oriented thinking: How to design classes, and the properties and behaviors of classes. What is the relationship and interaction between classes and classes.


Dark Horse programmer 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.