Traffic light Management system-experience 2-project source code and comments. doc

Source: Internet
Author: User
1. Lamp class Preparation Package com.isoftstone.interview;
 /** * 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 of the following direction of the lamp, each two formed a group, a group of lights at the same time green or red, so, * program code to control each group of lights 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. * * Public enum Lamp {*/* Each enumeration element represents a directional control light/s2n ("N2s", "s2w", false), s2w ("e2n", "e2w", false), e2w ("w2e", "E2s", false), E2s ("w2n", "s2n", false),/* The following element represents a light in the opposite direction to the element above, 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),/* The light from south to east and from west to North is not controlled by traffic lights; Because these four directions are right-handed, * in China's real life vehicles on the right line, so the right-hand side of the vehicle does not need to control the traffic lights can walk vehicles * will not be with other routes of the vehicle conflict; So, you can imagine that they always green/s2e (
	
	Null,null,true), n2w (Null,null,true), W2s (Null,null,true), e2n (null,null,true);
	/* The current light is lit, that is, whether the green light/private Boolean lighted;
	/* With the current light is the corresponding direction of green * * Private String opposite;
	
	/* The current light is turned red now a green lamp/private String next; Construction method with above three parameters private Lamp (string opposite, string Next,boolean lighted) {thIs.opposite = opposite;
		This.next = Next;
	this.lighted = lighted;
	/** * To determine whether the Green method * @return * * public boolean islighted () {return lighted;
		/** * A light turns green, it corresponds to the direction of the lamp will also turn green/public void light () {this.lighted = true; If opposite is null, the change method is dead loop, which is always on the green light if (opposite!= null) {/*valueof () method is a method for enumerating the classes; The string name is passed in to return the corresponding enumeration object; * Some people may say why not The opposite variable is defined directly as an enumeration lamp type; * Here I consider: if the definition of lamp-type, then the above s2n these property variables * inside inconvenient to pass the value, changed to write now, will be opposite defined as string, * directly to these
		The property variable is passed into a string, which facilitates a lot of * * lamp.valueof (opposite). Light (); SYSTEM.OUT.PRINTLN (name () + "lamp is green" there should be a total of 6 directions below to see the car go through.
	");
		/** * A lamp to turn red, the corresponding direction of the lamp will also be red, and the next direction of the light to turn green * @return next to turn green lights/public Lamp blackout () {this.lighted = false;
		if (opposite!= null) {lamp.valueof (opposite). Blackout ();
		Lamp nextlamp = null;
			if (next!= null) {Nextlamp = lamp.valueof (next);
			The name () method is also a method that enumerates the classes, returning a System.out.println of its own property name ("green light from" + name () + "--------> Switch to" + next); Nextlamp. Light ();
	return nextlamp; } 2.

Road class of writing 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;
 /** * Each Road object represents a route with a total of 12 routes, that is, a total of 12 road instance objects are generated in the system.
 * 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's collection of the first car removed, that means the car through the intersection. * * public class Road {//vehicles represents the car two, is a car two sets, including big trucks, small driving and so on all the cars list<string> vehicles = new arraylist<string>
	();
	
	The name member variable represents the direction String name = NULL;
		
		Public Road (String name) {this.name = name; The process of simulating a vehicle's random road/*executors is the executor of the new Jdk5 feature, so to remember: our tools such as utils are all end with s, * How to use how to spell the class to remember, executors similar, modified class has a large number of static methods , the change executor can new multiple thread pools, * Of course, can also be new to the following separate thread, whether it is a thread pool or threads return a executorservice type of value/executorservice executor = executors.new
		Singlethreadexecutor (); Runnable is an interface that cannot be directly new but can be made into an inner class new out of its implementationClass Executor.execute (new Runnable () {@Override public void run () {//Generate 999 vehicles for (int i=1;i<1000;i++)
					{try {//Break 1-10-second random time Thread.Sleep (new Random (). Nextint (10) + 1) * 1000);
					catch (Interruptedexception e) {e.printstacktrace (); }/* Internal class Access external member variable can speak the change member variable of the outer class The final finally, you can also use the external class name in the inner class. This. The method of external class variables achieves the purpose * * VEHICLES.ADD (Road.this.name + "_
				"+ i);
		
		}
			}
		}); Every second to check whether the corresponding light is green, is to release a car//timer (serial explosive) of the preparation, the application is quite extensive, will; New is scheduled (dispatch) dispatch pool Scheduledexecutorservice timer =
		Executors.newscheduledthreadpool (1); Timer.scheduleatfixedrate (New Runnable () {@Override public void run () {//first check if there are any vehicles on the road (Vehicles.size ()
					>0) {Boolean lighted = lamp.valueof (Road.this.name). islighted ();
					if (lighted) {//traversing: Passing through SYSTEM.OUT.PRINTLN (vehicles.remove (0) + "is traversing!"); }}, 1,//How much time do you want to do this 1,//How much time to do it again timeunit.seconds);//show the quantitative unit of the above value is 1 seconds or 1 minutes or1 hours; } 3.

Lampcontroller Class of writing package com.isoftstone.interview;
Import java.util.concurrent.Executors;

Import Java.util.concurrent.TimeUnit;
	
	/** * Vehicle controller, the main operating object * * Public class Lampcontroller {private Lamp currentlamp;
		Public Lampcontroller () {//Just start letting the light from south to north turn green;
		Currentlamp = lamp.s2n;
		
		Currentlamp.light (); 
			* * Every 10 seconds the current green light into a red light, and let the next direction of the light Green/executors.newscheduledthreadpool (1). Scheduleatfixedrate (New Runnable () {@Override public void Run () {SYSTEM.OUT.PRINTLN The vehicle is capable you come.
				");
			Currentlamp = Currentlamp.blackout ();
		
	}, ten, timeunit.seconds); } 4.

MainClass Class of writing package com.isoftstone.interview;  public class MainClass {/** * @param args/public static void main (string[] args) {/* Generate 12-direction routes/* string[]
		
		Directions = {"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 the entire traffic light system/New Lampcontroller ();
}
}

 

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.