Задание 3 Жерновой Ярослав
unknown
csharp
2 years ago
5.0 kB
9
Indexable
using System; using System.Threading; public class TrafficLight { private enum LightColor { Red, Yellow, Green, FlashingYellow } private LightColor currentState; private bool hasAdditionalLights; public TrafficLight(bool hasAdditionalLights) { this.hasAdditionalLights = hasAdditionalLights; currentState = LightColor.FlashingYellow; } public void ChangeStateUp() { if (currentState == LightColor.Red) { currentState = LightColor.Yellow; } else if (currentState == LightColor.Yellow) { currentState = LightColor.Green; } else if (currentState == LightColor.Green) { currentState = LightColor.Yellow; } } public void ChangeStateDown() { if (currentState == LightColor.Yellow) { currentState = LightColor.Red; } else if (currentState == LightColor.Green) { currentState = LightColor.Yellow; } else if (currentState == LightColor.Yellow) { currentState = LightColor.Red; } } public void ToggleWorkState() { if (currentState == LightColor.FlashingYellow) { currentState = LightColor.Yellow; } else { currentState = LightColor.FlashingYellow; } } public void SetAdditionalLights(bool enable) { hasAdditionalLights = enable; } public void DisplayState() { Console.WriteLine("Текущее состояние: " + currentState.ToString()); if (hasAdditionalLights) { Console.WriteLine("Дополнительные фонари включены"); } else { Console.WriteLine("Дополнительные фонари выключены."); } } } public class PedestrianTrafficLight : TrafficLight { private bool isTimerEnabled; private int timerDuration; private int remainingTime; private Timer timer; public PedestrianTrafficLight(bool hasAdditionalLights, bool isTimerEnabled, int timerDuration) : base(hasAdditionalLights) { this.isTimerEnabled = isTimerEnabled; this.timerDuration = timerDuration; this.remainingTime = timerDuration; this.timer = null; } public void EnableTimer(int duration) { isTimerEnabled = true; timerDuration = duration; remainingTime = duration; } public void DisableTimer() { isTimerEnabled = false; remainingTime = 0; if (timer != null) { timer.Dispose(); timer = null; } } private void TimerCallback(object state) { remainingTime--; if (remainingTime < 0) { Console.WriteLine("Таймер окончен."); timer.Dispose(); timer = null; } else { Console.WriteLine("Оставшееся время: " + remainingTime + " секунд."); } } public void StartTimer() { if (isTimerEnabled && timer == null) { Console.WriteLine("Таймер идет. Осталось " + timerDuration + " секунд."); remainingTime = timerDuration; TimerCallback timerCallback = new TimerCallback(TimerCallback); timer = new Timer(timerCallback, null, 0, 1000); } else { Console.WriteLine("Таймер выключен или уже запущен."); } } } public class Program { public static void Main(string[] args) { // Создание объекта светофора для пешеходов с дополнительными огнями и таймером PedestrianTrafficLight pedestrianLight = new PedestrianTrafficLight(true, false, 30); // Изменение состояний светофора с помощью кнопок pedestrianLight.ChangeStateUp(); pedestrianLight.DisplayState(); pedestrianLight.ChangeStateDown(); pedestrianLight.DisplayState(); pedestrianLight.ToggleWorkState(); pedestrianLight.DisplayState(); pedestrianLight.ChangeStateUp(); pedestrianLight.DisplayState(); pedestrianLight.ChangeStateDown(); pedestrianLight.DisplayState(); // Включение/выключение таймера int time = 30; pedestrianLight.EnableTimer(time); pedestrianLight.StartTimer(); Thread.Sleep(time*1000); pedestrianLight.DisableTimer(); } }
Editor is loading...