Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.4 kB
8
Indexable
using System;
using System.Collections.Generic;
using System.Threading;

public class TaxiFleetManagementApp
{
    private static readonly int NumTaxis = 5;
    private static readonly Random RandomGenerator = new Random();

    private static readonly Mutex ConsoleMutex = new Mutex();
    private static readonly SemaphoreSlim TaxiAvailabilitySemaphore = new SemaphoreSlim(NumTaxis);

    private static readonly List<Taxi> Taxis = new List<Taxi>();
    private static int selectedTaxiId = -1;

    private static bool showInfo = false;
    private static bool showIdInfo = false;

    public static void Main()
    {
        for (int i = 1; i <= NumTaxis; i++)
        {
            Taxis.Add(new Taxi(i));
        }

        for (int i = 0; i < NumTaxis; i++)
        {
            ThreadPool.QueueUserWorkItem(Taxis[i].SimulateTaxi);
        }

        MainMenu();
    }


    private static void MainMenu()
    {
        while (true)
        {
            showIdInfo = false;
            showInfo = false;
            Console.WriteLine("========= Taxi Fleet Management =========");
            Console.WriteLine("1. Podgląd live floty");
            Console.WriteLine("2. Przebyte kursy");
            Console.WriteLine("3. Wybierz pojazd, który chcesz śledzić");
            Console.WriteLine("0. Wyjście");
            Console.WriteLine("========================================");
            Console.Write("Wybierz opcję: ");

            string input = Console.ReadLine();

            if (input == "1")
            {
                selectedTaxiId = -1;
                Console.Clear();
                Console.WriteLine("========================================");
                Console.WriteLine("Podgląd live floty");
                Console.WriteLine("========================================");
                showInfo = true;

                foreach (var taxi in Taxis)
                {
                    ConsoleMutex.WaitOne();
                    
                    ConsoleMutex.ReleaseMutex();
                }
                Console.WriteLine();
                Console.WriteLine("Naciśnij dowolny klawisz, aby powrócić do menu głównego.");
                Console.ReadKey();
                Console.Clear();

                // Uruchomienie symulacji taksówek
                for (int i = 0; i < NumTaxis; i++)
                {
                    ThreadPool.QueueUserWorkItem(Taxis[i].SimulateTaxi);
                }
            }
            else if (input == "2")
            {
                showIdInfo = false;
                showInfo = false;
                StatystykiPrzebytychKursow();
            }
            else if (input == "3")
            {
                showInfo = false;
                WybierzTaxi();
            }
            else if (input == "0")
            {
                break;
            }
            else
            {
                Console.WriteLine("Niepoprawny wybór. Spróbuj ponownie.");
            }
        }
    }

    private static void StatystykiPrzebytychKursow()
    {
        Console.Clear();
        Console.WriteLine("========================================");
        Console.WriteLine("Statystyki przebytych kursów");
        Console.WriteLine("========================================");

        foreach (var taxi in Taxis)
        {
            ConsoleMutex.WaitOne();
            Console.WriteLine($"Taxi {taxi.Id} - Przebyte kursy: {taxi.PrzebyteKursy}, Zarobione pieniądze: {taxi.ZarobionePieniadze:C}");
            ConsoleMutex.ReleaseMutex();
        }
        Console.WriteLine();
        Console.WriteLine("Naciśnij dowolny klawisz, aby powrócić do menu głównego.");
        Console.ReadKey();
        Console.Clear();
    }

    private static void WybierzTaxi()
    {
        Console.Clear();
        Console.WriteLine("========================================");
        Console.WriteLine("Wybierz pojazd, który chcesz śledzić");
        Console.WriteLine("========================================");

        ConsoleMutex.WaitOne();
        foreach (var taxi in Taxis)
        {
            Console.WriteLine($"{taxi.Id}. Taxi {taxi.Id}");
        }
        ConsoleMutex.ReleaseMutex();

        Console.Write("Wybierz ID taksówki: ");
        string input = Console.ReadLine();

        if (int.TryParse(input, out int selectedId) && selectedId > 0 && selectedId <= NumTaxis)
        {
            selectedTaxiId = selectedId;
            Console.Clear();
            Console.WriteLine("========================================");
            Console.WriteLine($"Śledzenie taksówki {selectedTaxiId}");
            Console.WriteLine("========================================");
            Console.WriteLine();
            Console.WriteLine("Naciśnij dowolny klawisz, aby powrócić do menu głównego.");
            showIdInfo = true;
            Console.ReadKey();
            Console.Clear();
        }
        else
        {
            Console.WriteLine("Niepoprawny wybór. Spróbuj ponownie.");
        }
    }

    public class Taxi
    {
        public int Id { get; }
        public bool IsAvailable { get; private set; }
        public int PrzebyteKursy { get; private set; }
        public decimal ZarobionePieniadze { get; private set; }
        public string Status => IsAvailable ? "searching for a passenger..." : "on a ride...";

        public Taxi(int id)
        {
            Id = id;
            IsAvailable = true;
            PrzebyteKursy = 0;
            ZarobionePieniadze = 0;
        }

        public void SimulateTaxi(object state)
        {
            while (true)
            {
                ConsoleMutex.WaitOne();
                if (showInfo)
                    Console.WriteLine($"Taxi {Id} is {Status}");
                else if (selectedTaxiId == Id && showIdInfo)
                    Console.WriteLine($"Taxi {Id} is {Status}");
                ConsoleMutex.ReleaseMutex();

                Thread.Sleep(RandomGenerator.Next(2000, 5000));
                TaxiAvailabilitySemaphore.Wait();

                ConsoleMutex.WaitOne();
                if (showInfo)
                    Console.WriteLine($"Taxi {Id} has picked up a passenger!");
                else if (selectedTaxiId == Id && showIdInfo)
                    Console.WriteLine($"Taxi {Id} has picked up a passenger!");
                ConsoleMutex.ReleaseMutex();

                // Simulate ride
                Thread.Sleep(RandomGenerator.Next(2000, 5000));

                ConsoleMutex.WaitOne();
                if (showInfo)
                    Console.WriteLine($"Taxi {Id} has completed the ride!");
                else if (selectedTaxiId == Id && showIdInfo)
                    Console.WriteLine($"Taxi {Id} has completed the ride!");
                ConsoleMutex.ReleaseMutex();

                Thread.Sleep(RandomGenerator.Next(500, 1000));

                TaxiAvailabilitySemaphore.Release();

                // Update stats
                IsAvailable = true;
                PrzebyteKursy++;
                decimal zarobionePieniadze = RandomGenerator.Next(10, 50);
                ZarobionePieniadze += zarobionePieniadze;

            }
        }
    }
}