Advertisement
Vlad_Savitskiy

Trains

Jun 26th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CSLight
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             while (true)
  14.             {
  15.                 Console.Clear();
  16.                 Console.WriteLine("Сконфигурируйте новый поезд!");
  17.                 ShowActualInfo(null);
  18.  
  19.                 Train train = CreateTrain();
  20.                 train.ShowTrain();
  21.  
  22.                 Ticket ticket = CreateTicket(train);
  23.                 ShowActualInfo(ticket);
  24.             }
  25.         }
  26.  
  27.         private static Ticket CreateTicket(Train train)
  28.         {
  29.             while (true)
  30.             {
  31.                 Console.Write("\nВыберите вагон: ");
  32.                 string userInput = Console.ReadLine();
  33.  
  34.                 if (int.TryParse(userInput, out int wagonNumber))
  35.                 {
  36.                     if (wagonNumber > train.WagonsCount || wagonNumber < 1)
  37.                         Print("Такого вагона в поезде нет, попробуйте ещё раз.", ConsoleColor.Red);
  38.                     else
  39.                     {
  40.                         Console.Clear();
  41.                         while (true)
  42.                         {
  43.                             Wagon wagon = train.GetWagon(wagonNumber - 1);
  44.                             wagon.ShowWagon();
  45.  
  46.                             Console.Write("\nВыберите свободное место: ");
  47.                             userInput = Console.ReadLine();
  48.  
  49.                             if (int.TryParse(userInput, out int seatNumber))
  50.                             {
  51.                                 Console.Clear();
  52.                                 if (seatNumber > wagon.SeatsCount || seatNumber < 0)
  53.                                     Print("Такого места в вагоне нет, попробуйте ещё раз.", ConsoleColor.Red);
  54.                                 else
  55.                                 {
  56.                                     Seat seat = wagon.GetSeat(seatNumber - 1);
  57.  
  58.                                     if (seat.IsFree)
  59.                                     {
  60.                                         seat.TakeSeat();
  61.                                         return new Ticket(train, wagonNumber, seatNumber);
  62.                                     }
  63.  
  64.                                     Print("Это место занято, выберите другое место.", ConsoleColor.Red);
  65.                                 }
  66.                             }
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.  
  73.         private static Train CreateTrain()
  74.         {
  75.             string departurePoint = GetCity("Пункт отправления поезда: ");
  76.             string destination = GetCity("Пункт назначения поезда: ");
  77.             List<Wagon> wagons = CreateWagons();
  78.  
  79.             return new Train(departurePoint, destination, wagons);
  80.         }
  81.  
  82.         private static List<Wagon> CreateWagons()
  83.         {
  84.             Random rand = new Random();
  85.  
  86.             int wagonCount = rand.Next(1, 6);
  87.             List<Wagon> wagons = new List<Wagon>(wagonCount);
  88.  
  89.             for (int i = 0; i < wagonCount; i++)
  90.             {
  91.                 int seatCount = rand.Next(20, 31);
  92.                 List<Seat> seats = new List<Seat>(seatCount);
  93.  
  94.                 for (int j = 0; j < seatCount; j++)
  95.                 {
  96.                     int temp = rand.Next(0, 2);
  97.                     seats.Add(new Seat(j, temp == 0));
  98.                 }
  99.  
  100.                 wagons.Add(new Wagon(i, seats));
  101.             }
  102.  
  103.             return wagons;
  104.         }
  105.  
  106.         private static string GetCity(string cityDescription)
  107.         {
  108.             Console.Write(cityDescription);
  109.             Console.ForegroundColor = ConsoleColor.Green;
  110.             string userInput = Console.ReadLine();
  111.             Console.ForegroundColor = ConsoleColor.White;
  112.  
  113.             if (userInput != null)
  114.                 return userInput;
  115.  
  116.             throw new ArgumentException("Некорректное название города.");
  117.         }
  118.  
  119.         private static void ShowActualInfo(Ticket ticket)
  120.         {
  121.             if (ticket == null)
  122.             {
  123.                 Print("\nПоезд ещё не сформирован и не отправлен.\n", ConsoleColor.DarkRed);
  124.                 Console.Write("Нажмите любую клавишу для того, чтобы перейти к формированию поезда...");
  125.             }
  126.             else
  127.             {
  128.                 Console.Write($"Маршрут: ");
  129.                 Print($"{ticket.Train.DeparturePoint} --> {ticket.Train.Destination}", ConsoleColor.Green);
  130.                 Console.Write("Ваш вагон: ");
  131.                 Print($"{ticket.WagonNumber}", ConsoleColor.Green);
  132.                 Console.Write("Ваше место: ");
  133.                 Print($"{ticket.SeatNumber}", ConsoleColor.Green);
  134.                 Console.Write("Нажмите любую клавишу для того, чтобы отправить поезд...");
  135.             }
  136.  
  137.             Console.ReadKey();
  138.             Console.Clear();
  139.         }
  140.  
  141.         private static void Print(string text, ConsoleColor color = ConsoleColor.White)
  142.         {
  143.             ConsoleColor defaultColor = Console.ForegroundColor;
  144.  
  145.             Console.ForegroundColor = color;
  146.             Console.WriteLine(text);
  147.             Console.ForegroundColor = defaultColor;
  148.         }
  149.     }
  150.  
  151.     class Train
  152.     {
  153.         public string DeparturePoint { get; }
  154.         public string Destination { get; }
  155.         public int FreeSeats => CalculateFreeSeats();
  156.         public int WagonsCount => _wagons.Count;
  157.         private List<Wagon> _wagons;
  158.  
  159.         public Train(string departurePoint, string destination, List<Wagon> wagons)
  160.         {
  161.             DeparturePoint = departurePoint;
  162.             Destination = destination;
  163.             _wagons = wagons;
  164.         }
  165.  
  166.         private int CalculateFreeSeats()
  167.         {
  168.             int freeSeats = 0;
  169.  
  170.             foreach (Wagon wagon in _wagons)
  171.             {
  172.                 for (int i = 0; i < wagon.SeatsCount; i++)
  173.                 {
  174.                     if (wagon.GetSeat(i).IsFree)
  175.                         freeSeats++;
  176.                 }
  177.             }
  178.  
  179.             return freeSeats;
  180.         }
  181.  
  182.         public Wagon GetWagon(int index)
  183.         {
  184.             if (index < 0 || index >= _wagons.Count)
  185.                 throw new ArgumentException("Некорректный номер вагона.");
  186.  
  187.             return _wagons[index];
  188.         }
  189.  
  190.         public void ShowTrain()
  191.         {
  192.             foreach (Wagon wagon in _wagons)
  193.                 wagon.ShowWagon();
  194.         }
  195.     }
  196.  
  197.     class Wagon
  198.     {
  199.         public int WagonNumber { get; }
  200.         public int SeatsCount => _seats.Count;
  201.         private List<Seat> _seats;
  202.  
  203.         public Wagon(int wagonNumber, List<Seat> seats)
  204.         {
  205.             WagonNumber = wagonNumber;
  206.             _seats = seats;
  207.         }
  208.  
  209.         public Seat GetSeat(int index)
  210.         {
  211.             if (index < 0 || index >= _seats.Count)
  212.                 throw new ArgumentException("Некорректный номер сиденья.");
  213.  
  214.             return _seats[index];
  215.         }
  216.  
  217.         public void ShowWagon()
  218.         {
  219.             ConsoleColor defaultBackGroundColor = Console.BackgroundColor;
  220.             ConsoleColor defaultForeGroundColor = Console.ForegroundColor;
  221.  
  222.             Console.BackgroundColor = ConsoleColor.White;
  223.             Console.ForegroundColor = ConsoleColor.Black;
  224.  
  225.             Console.WriteLine($"\nВагон №{WagonNumber + 1}\n");
  226.             int top = Console.CursorTop;
  227.             int left = Console.CursorLeft;
  228.  
  229.             for (int j = 0; j < SeatsCount; j++)
  230.             {
  231.                 Seat seat = GetSeat(j);
  232.  
  233.                 if (j != 0 && j % 4 == 0)
  234.                 {
  235.                     top++;
  236.                     left = 0;
  237.                 }
  238.                 else if (j != 0 && j % 2 == 0)
  239.                     left += 4;
  240.  
  241.                 Console.SetCursorPosition(left, top);
  242.  
  243.                 Console.BackgroundColor = ConvertSeatInfoToColor(seat);
  244.                 Console.WriteLine(seat.Number + 1);
  245.                 left += 3;
  246.             }
  247.  
  248.             Console.BackgroundColor = defaultBackGroundColor;
  249.             Console.ForegroundColor = defaultForeGroundColor;
  250.         }
  251.  
  252.         private ConsoleColor ConvertSeatInfoToColor(Seat seat)
  253.         {
  254.             switch (seat.IsFree)
  255.             {
  256.                 case false:
  257.                     return ConsoleColor.Red;
  258.                 case true:
  259.                     return ConsoleColor.Green;
  260.                 default:
  261.                     return ConsoleColor.Red;
  262.             }
  263.         }
  264.     }
  265.  
  266.     class Seat
  267.     {
  268.         public int Number { get; }
  269.         public bool IsFree { get; private set; }
  270.  
  271.         public Seat(int number, bool isFree)
  272.         {
  273.             Number = number;
  274.             IsFree = isFree;
  275.         }
  276.  
  277.         public void TakeSeat()
  278.         {
  279.             IsFree = false;
  280.         }
  281.  
  282.         public void ClearSeat()
  283.         {
  284.             IsFree = true;
  285.         }
  286.     }
  287.  
  288.     class Ticket
  289.     {
  290.         public Train Train { get; }
  291.         public int WagonNumber { get; }
  292.         public int SeatNumber { get; }
  293.  
  294.         public Ticket(Train train, int wagonNumber, int seatNumber)
  295.         {
  296.             Train = train;
  297.             WagonNumber = wagonNumber;
  298.             SeatNumber = seatNumber;
  299.         }
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement