Torgach

Trains

Apr 6th, 2021 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.96 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 trains
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             TicketOffice ticketOffice = new TicketOffice();
  14.             ticketOffice.Work();
  15.         }
  16.     }
  17.  
  18.     class TicketOffice
  19.     {
  20.         private Passengers _passengers = new Passengers();
  21.  
  22.         private List<Train> _trains = new List<Train>()
  23.         {
  24.             new Train(1, 0, false),
  25.             new Train(2, 0, false)
  26.         };
  27.  
  28.         private List<Directions> _directions = new List<Directions>();
  29.  
  30.         public void Work()
  31.         {
  32.             bool isWork = true;
  33.             while (isWork)
  34.             {
  35.                 ShowInfo();
  36.  
  37.                 Console.WriteLine($"\n[1] - Создать поезд;\n" +
  38.                 $"[2] - Сформировать направление\n" +
  39.                 $"[3] - Продать билеты\n" +
  40.                 $"[4] - Отправить поезд\n" +
  41.                 $"[5] - Выход");
  42.                 Console.Write("Ввод: ");
  43.  
  44.                 switch (Console.ReadLine())
  45.                 {
  46.                     case "1":
  47.                         CreateTrain();
  48.                         break;
  49.                     case "2":
  50.                         CreateDirection();
  51.                         break;
  52.                     case "3":
  53.                         SellTickets();
  54.                         break;
  55.                     case "4":
  56.                         SendTrain();
  57.                         break;
  58.                     case "5":
  59.                         isWork = false;
  60.                         break;
  61.                     default:
  62.                         Console.WriteLine("Ошибка!");
  63.                         break;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         private void CreateTrain()
  69.         {
  70.             Train train = new Train(_trains.Count + 1, 0);
  71.             _trains.Add(train);
  72.         }
  73.  
  74.         private void CreateDirection()
  75.         {
  76.             Console.WriteLine("Выберите поезд из депо: ");
  77.             if (TryGetNumber(out int userNumber, _trains.Count) && _trains[userNumber].СheckOutDepature == false)
  78.             {
  79.                 Console.Write("Поезд отправится из города: ");
  80.                 string directionFrom = Console.ReadLine();
  81.  
  82.                 Console.Write("Поезд отправится с платформы: ");
  83.                 string platformFrom = Console.ReadLine();
  84.  
  85.                 Console.Write("Поезд отправится в город: ");
  86.                 string directionToThe = Console.ReadLine();
  87.  
  88.                 Console.Write("Поезд прибывает на платформу: ");
  89.                 string platformToThe = Console.ReadLine();
  90.  
  91.                 Directions direction = new Directions(_trains[userNumber], _passengers, directionFrom, platformFrom, directionToThe, platformToThe);
  92.                 _directions.Add(direction);
  93.             }
  94.         }
  95.  
  96.         private void SellTickets()
  97.         {
  98.             if (CheckDirections() == false)
  99.                 return;
  100.  
  101.             Console.Write("Укажите номер поезда: ");
  102.  
  103.             if(TryGetNumber(out int userNumber, _directions.Count) == false)
  104.                 return;
  105.  
  106.             if (CheckTickets(userNumber) == false)
  107.                 return;
  108.  
  109.             else if (_directions[userNumber].Train.СheckOutDepature)
  110.             {
  111.                 Console.WriteLine("Нельзя повторно продать билеты на рейс!");
  112.                 return;
  113.             }
  114.  
  115.             Random rand = new Random();
  116.             _passengers = new Passengers(rand.Next(1, 20));
  117.  
  118.             _directions[userNumber].GetClients(_passengers);
  119.             _directions[userNumber].GetDirection(_directions[userNumber]);        
  120.         }
  121.  
  122.         private void SendTrain()
  123.         {
  124.             if (CheckDirections() == false)
  125.                 return;
  126.  
  127.             Console.Write("Укажите номер поезда: ");
  128.  
  129.             if (TryGetNumber(out int userNumber, _directions.Count) == false)
  130.                 return;
  131.  
  132.             if (_directions[userNumber].Train.СheckOutDepature)
  133.             {
  134.                 Console.WriteLine("Поезд уже отправлен!");
  135.                 return;
  136.             }
  137.  
  138.             _directions[userNumber].Train.SetSendStatus(true);
  139.         }
  140.  
  141.         private void ShowInfo()
  142.         {
  143.             Console.Write("Для продолжения нажмите любую клавишу...");
  144.             Console.ReadKey(true);
  145.             Console.Clear();
  146.  
  147.             Console.WriteLine("Расписание поездов: ");
  148.  
  149.             Console.WriteLine("==============");
  150.  
  151.             if (_trains.Count == 0)
  152.             {
  153.                 Console.Write("В депо нет поездов!");
  154.             }
  155.             else if (_trains.Count > 0)
  156.             {
  157.                 Console.WriteLine("В депо находятся находятся:");
  158.                 foreach (var train in _trains)
  159.                 {
  160.                     Console.WriteLine($"Поезд №{train.Number}");
  161.                 }
  162.             }
  163.  
  164.             Console.WriteLine("==============");
  165.  
  166.             if (CheckDirections())
  167.             {
  168.                 foreach (var direction in _directions)
  169.                 {
  170.                     Console.Write($"№{direction.Train.Number}, из гор.{direction.DirectionFrom} платформа {direction.PlatformFrom} " +
  171.                         $"скоро направится в город {direction.DirectionToThe} платформа {direction.PlatformToThe}." +
  172.                         $"Поезд не отправлен.\n");
  173.                     if (direction.Train.Wagons == 0)
  174.                         Console.Write("Без вагонов. Поезд не готов к отправке.\n");
  175.                     else if(direction.Train.Wagons > 0)
  176.                     {
  177.                         Console.Write("Вагонов: " + direction.Train.Wagons + " шт. ");
  178.                         Console.Write("Билетов продано: " + direction.Clients.Tickets +"\n");
  179.                         if (direction.Train.СheckOutDepature)
  180.                             Console.Write("Поезд отправлен.\n");
  181.                         else
  182.                             Console.Write("Поезд не отправлен.\n");
  183.                     }
  184.                 }
  185.             }
  186.             Console.WriteLine("==============");
  187.         }
  188.  
  189.         private bool TryGetNumber(out int userInput, int count)
  190.         {
  191.             if (int.TryParse(Console.ReadLine(), out userInput) && userInput <= count && userInput > 0)
  192.             {
  193.                 --userInput;
  194.                 return true;
  195.             }
  196.             else
  197.             {
  198.                 Console.WriteLine("Ошибка! Такого номера нет.");
  199.             }
  200.             return false;
  201.         }
  202.  
  203.         private bool CheckDirections()
  204.         {
  205.             if (_directions.Count == 0)
  206.             {
  207.                 Console.WriteLine("К отправке нет готовых поездов!");
  208.                 return false;
  209.             }
  210.             return true;
  211.         }
  212.  
  213.         private bool CheckTickets(int userNumber)
  214.         {
  215.             if (_directions[userNumber].Clients.Tickets > 0)
  216.             {
  217.                 Console.WriteLine("На рейс уже куплены билеты!");
  218.                 return false;
  219.             }
  220.             return true;
  221.         }
  222.     }
  223.  
  224.     class Train
  225.     {
  226.         public bool СheckOutDepature { get; private set; }
  227.         public int Number { get; private set; }
  228.         public int Wagons { get; private set; }
  229.  
  230.         public Train(int number, int wagons, bool isTrainSent = false)
  231.         {
  232.             Number = number;
  233.             Wagons = wagons;
  234.             СheckOutDepature = isTrainSent;
  235.         }
  236.  
  237.         public void CreateWagons(int tickets)
  238.         {
  239.             int wagonSeats;
  240.             Random rand = new Random();
  241.  
  242.             for (; tickets >= 0; tickets--)
  243.             {
  244.  
  245.                 wagonSeats = rand.Next(30, 80);
  246.                 tickets -= wagonSeats;
  247.                 ++Wagons;
  248.             }
  249.         }
  250.  
  251.         public void SetSendStatus(bool flag)
  252.         {
  253.             СheckOutDepature = flag;
  254.         }
  255.     }
  256.  
  257.     class Directions
  258.     {
  259.         public string DirectionFrom { get; private set; }
  260.         public string PlatformFrom { get; private set; }
  261.         public string DirectionToThe { get; private set; }
  262.         public string PlatformToThe { get; private set; }
  263.  
  264.         public Passengers Clients { get; private set; }
  265.         public Train Train { get; private set; }
  266.  
  267.         public Directions(Train train, Passengers clients, string directionFrom, string platformFrom, string directionToThe, string platformToThe)
  268.         {
  269.             Train = train;
  270.             Clients = clients;
  271.             DirectionFrom = directionFrom;
  272.             PlatformFrom = platformFrom;
  273.             PlatformToThe = platformToThe;
  274.             DirectionToThe = directionToThe;
  275.         }
  276.  
  277.         public void GetDirection(Directions direction)
  278.         {
  279.             DirectionFrom = direction.DirectionFrom;
  280.             PlatformFrom = direction.PlatformFrom;
  281.             DirectionToThe = direction.DirectionToThe;
  282.             PlatformToThe = direction.PlatformToThe;
  283.         }
  284.  
  285.         public void GetClients(Passengers clients)
  286.         {
  287.             Clients = clients;
  288.             Train.CreateWagons(Clients.Tickets);
  289.         }
  290.     }
  291.  
  292.     class Passengers
  293.     {
  294.         public int Tickets { get; private set; }
  295.  
  296.         public Passengers()
  297.         {
  298.             Tickets = 0;
  299.         }
  300.  
  301.         public Passengers(int tickets)
  302.         {
  303.             Tickets = tickets;
  304.         }
  305.     }
  306.  
  307. }
  308.  
Add Comment
Please, Sign In to add comment