tag: Java OS art for div management
The traffic light management system logic of the intersection is simulated. The specific requirements are as follows: a vehicle traveling along each route is randomly generated asynchronously. For example, a vehicle from the South to the North turns right. A vehicle from the east to the south-turn a vehicle... Ignore the yellow light, only the red light and the green light are considered. A left-turn vehicle control signal light should be considered, and a right-turn vehicle is not controlled by the signal light. The specific traffic signal control logic is the same as that of common traffic signals in real life. Note: vehicles in the North-South direction and vehicles in the east-west direction are allowed to interchangeably. vehicles in the same direction should be allowed to go directly and then turn left. The intersection time for each vehicle to pass is 1 second (Prompt: simulation can be performed by thread sleep ). You can set the random generation interval and traffic light switching interval. GUI is not required. You only need to implement the system logic. You can use the LOG method to display the program running results. Multiple vehicles will appear on each route. New vehicles should be randomly added on the route, and a car should be reduced every second during the light-green period. Design a road class to represent a route. Each road object represents a route. There are 12 routes in total, that is, a total of 12 road instance objects will be generated in the system. A new vehicle is randomly added to each route and saved in a collection. Every second, each route checks whether the lights that control the current route are green. If yes, the first car in the car's set stored in the current route is removed, indicating that the car passes through the intersection. Every second of each route checks whether the light that controls the current route is green. When a light changes from green to red, the light in the next direction should be 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 must have a method of changing from Bright to black, and can return their own bright black state. There are 12 routes in total, so a total of 12 traffic lights are generated in the system. The route of the right turn is not controlled by the light, but in order to let the program adopt a unified processing method, it is assumed that there are four right turn lights, but these lights are always on, that is, it never changes. In addition to the lights of the other eight routes in the right turn direction, they are paired and can be classified into four groups. Therefore, during programming, as long as one lamp is taken from each of the four groups, the four lights are turned on in round-robin mode, and the lights corresponding to the four lights change with each other, therefore, there must be a variable in the lamp class to remember the lights in the opposite direction. In the method of brightening and blackening a lamp object, the lights in the corresponding direction are also brightened and dimmed. When the light turns black, it is followed by the light of the next light, and a variable is used in the lamp class to remember its next light. No matter where the program gets the lights in a certain direction, each time it gets the same instance object, it is very convenient for the lamp class to use enumeration, there are always only instance objects that represent the lights in 12 directions. Design a lampcontroller class, which regularly turns the current green light red. Each road object has a name member variable to represent the direction, and a vehicles member variable to represent the vehicle set in the direction. In the road object constructor, enable a thread to add a vehicle to the vehicles set at every random time (represented by a string in the form of "route name _ id ). Enable a timer in the construction method of the road object, and check whether the light in the direction is green every second. If yes, print the vehicle set and remove the first car from the set. There are 12 lights in the lamp writing system. In other parts of the program, the corresponding lamp instance object can be obtained based on the lamp name. Based on these factors, it is easier to define the lamp class in the enumeration form in Java 5. The bright black state in each lamp object is represented by the lighted variable. The lamp objects in the s2n, s2w, e2w, and e2n directions are selected for round-robin and brightening, the lamp object also has an oppositelampname variable to indicate the lights in the opposite direction. Then, a nextlampname variable is used to indicate the next bright light after the light turns on. The three variables are assigned values in the form of constructor. Because enumeration elements must be referenced after definition, they cannot be referenced by each other in constructor, the lights in the opposite direction and the next direction are represented in strings. The following methods are added to brighten and blacklist a lamp: Light and blackout. For lamp objects in four directions: s2n, s2w, e2w, and e2n, inside the two methods, the light in the opposite direction should be turned on and black, and the blackout method should also turn on the next light. In addition to lamp objects in the s2n, s2w, e2w, and e2n directions, the nextlampname and oppositelampname attributes of lamp objects in other directions are set to null, the nextlampname and oppositelampname attributes of lamp objects in the s2n, s2w, e2w, and e2n directions must be set to null to prevent light and blackout from entering an endless loop. The lampcontroller class can only have one set of traffic light control systems in the entire system. Therefore, it is best to design the lampcontroller class as a singleton. Set the first green light in the lampcontroller constructor. In the start method of the lampcontroller object, the current light turns green, and then a timer is started. The current light turns red every 10 seconds and the next light turns green. Compile the mainclass class and use the for loop to create Objects representing 12 routes. Then obtain the lampcontroller object and call its start method.