Traffic light management system to achieve traffic

Source: Internet
Author: User


Traffic light Management system

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 and the traffic light Exchange time interval custom, you can set

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

Video 1-6 for analysis

Video 7-11 for code design

Analysis of roads: ways to increase vehicles and reduce vehicles, to do threads using JDK1.5 technologies: Thread Pools

The use of timer, the use of generics in the set;

Analysis of the lamp: using the enumeration technique

Lamp controller: Change of control lamp

Here are a few key points to note:

1, attention to object-oriented design

A two stone knives, stone knives cut trees, trees made of chairs ...

(Knife knifefactory (Stone first,stone second))

2, Lamp enumeration class design------Incoming oppsite, next, lighted design

3, light and extinguished the design

Key code implementation:

The design of the road:

Package Com.isoftstone.interview;

Import java.util.ArrayList;

Import java.util.List;

Import Java.util.Random;

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

Import Java.util.concurrent.ScheduledExecutorService;

Import Java.util.concurrent.TimeUnit;

Publicclass Road {

List<string>vehicles=new arraylist<string> ();

Private stringname=null;

Public Road (String name) {

This.name=name;

Executorservice pool= executors.newsinglethreadexecutor ();

Pool.execute (New Runnable () {

Publicvoid Run () {

for (int i=1;i<1000;i++) {

try {

Thread.Sleep ((New Random (). Nextint (10) +1) *1000);

catch (Interruptedexception e) {

E.printstacktrace ();

}

Vehicles.add (road.this.name+ "_" +i);

}

}

});

Scheduledexecutorservice Timer=executors.newscheduledthreadpool (1);

Timer.scheduleatfixedrate (

New Runnable () {

Publicvoid Run () {

if (Vehicles.size () >0) {

Boolean lighted=lamp.valueof (Road.this.name). islighted ();

if (lighted) {

System.out.println (vehicles.remove (0) + "is travelling!");

}

}

}

},

1,

1,

Timeunit.seconds);

}

}

The design of the lamp:

Package Com.isoftstone.interview;

Publicenum 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 Stringoppsite;

Private Stringnext;

privatebooleanlighted;

Private Lamp (String oppsite,string Next,boolean lighted) {

This.oppsite=oppsite;

This.next=next;

this.lighted=lighted;

}

Publicboolean islighted () {

returnlighted;

}

Publicvoid Light () {

This.lighted=true;

if (oppsite!=null) {

Lamp.valueof (oppsite). Lighted=true;

}

System.out.println (THIS.name () + "lamp is green, there will be 6 directions to see the car");

}

Publicvoid Blackout () {

This.lighted=false;

if (oppsite!=null) {

Lamp.valueof (oppsite). Lighted=false;

}

Lamp Nextlamp=null;

if (next!=null) {

Nextlamp=lamp.valueof (next);

System.out.println ("green light from" +name () + "--> switch to" +next);

Nextlamp.light ();

}

}

}

Design of lamp Controller:

Package Com.isoftstone.interview;

Import java.util.concurrent.Executors;

Import Java.util.concurrent.ScheduledExecutorService;

Import Java.util.concurrent.TimeUnit;

Publicclass Lampcontroller {

Public Lampcurrentlamp;

Public Lampcontroller () {

currentlamp=lamp.s2n;

Currentlamp.light ();

Scheduledexecutorservice Pool=executors.newscheduledthreadpool (1);

Pool.scheduleatfixedrate (

New Runnable () {

Publicvoid Run () {

Currentlamp.blackout ();

}

},

10,

10,

Timeunit.seconds);

}

}

The design of the main class:

Package Com.isoftstone.interview;

Publicclass MainClass {

Publicstaticvoid Main (string[] args) {

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]);

}

Newlampcontroller ();

}

}


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.