Zad 3 Доработал

mail@pastecode.io avatar
unknown
csharp
7 months ago
6.1 kB
1
Indexable
Never
using System;
using System.Threading;

public class TrafficLight
{
    protected enum LightColor
    {
        Red,
        Yellow,
        Green,
        FlashingYellow
    }

    protected LightColor currentState;
    protected 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;
    private bool isAuto;
    private int Times;

    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)
    {
        if (currentState == LightColor.Red | currentState == LightColor.Green)
    {
        isTimerEnabled = true;
        timerDuration = duration;
        remainingTime = duration;
    }
    else{
        Console.WriteLine("Включен желтый сигнал светофора. Невозмлжно включить Таймер");
    }
    }

    public void DisableTimer()
    {
        isTimerEnabled = false;
        remainingTime = 0;
        if (timer != null)
        {
            timer.Dispose();
            timer = null;
        }
         if (currentState == LightColor.Green)
        {
            currentState = LightColor.Red;
        }
        else if (currentState == LightColor.Red)
        {
            currentState = LightColor.Green;
        } 
    }

    private void TimerCallback(object state)
    {
        remainingTime--;

        if (remainingTime < 0)
        {
            Console.WriteLine("Таймер окончен.");
            timer.Dispose();
            timer = null;
            DisableTimer();
        }
        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 void Auto(int time)
    {
    Times = time/5;
    if(isAuto)
        {
        isAuto = false;
        DisableTimer();
        Console.WriteLine("выключение автоматического режима");
        }
    
    
    else
    {
        isAuto = true;
        while (Times!=0)
        {
        int timee = 3;
        EnableTimer(timee);
        StartTimer();
        Thread.Sleep(timee*1000);
        DisplayState();
        Thread.Sleep(2000);
        Times=Times-1;
        }
    }

    }
}

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();
        
        pedestrianLight.ChangeStateUp();
        pedestrianLight.DisplayState();

        // Включение/выключение таймера
        
        pedestrianLight.Auto(50);
        
        pedestrianLight.Auto(0);
    }
}
Leave a Comment