Advertisement
Torgach

tempTrains

Apr 7th, 2021
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.49 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),
  25.             new Train(2, 0)
  26.         };
  27.  
  28.         private List<Directions> _directions = new List<Directions>()
  29.         {
  30.             new Directions("msk", "4", "spb", "6")
  31.         };
  32.  
  33.         public void Work()
  34.         {
  35.             bool isWork = true;
  36.             while (isWork)
  37.             {
  38.                 ShowInfo();
  39.  
  40.                 Console.WriteLine($"\n[1] - Создать поезд;\n" +
  41.                 $"[2] - Сформировать направление\n" +
  42.                 $"[3] - Продать билеты\n" +
  43.                 $"[4] - Выбрать направление\n" +
  44.                 $"[5] - Отправить поезд\n" +
  45.                 $"[6] - Выход");
  46.                 Console.Write("Ввод: ");
  47.  
  48.                 switch (Console.ReadLine())
  49.                 {
  50.                     case "1":
  51.                         CreateTrain();
  52.                         break;
  53.                     case "2":
  54.                         CreateDirection();
  55.                         break;
  56.                     case "3":
  57.                         SellTickets();
  58.                         break;
  59.                     case "4":
  60.                         ChooseDirection();
  61.                         break;
  62.                     case "5":
  63.                         SendTrain();
  64.                         break;
  65.                     case "6":
  66.                         isWork = false;
  67.                         break;
  68.                     default:
  69.                         Console.WriteLine("Ошибка!");
  70.                         break;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         private void CreateTrain()
  76.         {
  77.             Train train = new Train(_trains.Count + 1, 0);
  78.             _trains.Add(train);
  79.         }
  80.  
  81.         private void CreateDirection()
  82.         {
  83.             Console.Write("Поезд отправится из города: ");
  84.             string directionFrom = Console.ReadLine();
  85.  
  86.             Console.Write("Поезд отправится с платформы: ");
  87.             string platformFrom = Console.ReadLine();
  88.  
  89.             Console.Write("Поезд отправится в город: ");
  90.             string directionToThe = Console.ReadLine();
  91.  
  92.             Console.Write("Поезд прибывает на платформу: ");
  93.             string platformToThe = Console.ReadLine();
  94.  
  95.             Directions direction = new Directions(directionFrom, platformFrom, directionToThe, platformToThe);
  96.             _directions.Add(direction);
  97.  
  98.             Console.WriteLine("Выберите поезд из депо: ");
  99.             if (TryGetNumber(out int userNumber, _trains.Count) && _trains[userNumber].IsDirectionSet == false)
  100.             {
  101.                 _trains[userNumber] = new Train(direction, _trains[userNumber]);
  102.             }
  103.         }
  104.  
  105.         private void ChooseDirection()
  106.         {
  107.             if (CheckDirections() == false)
  108.             {
  109.                 return;
  110.             }
  111.  
  112.             Console.WriteLine("Выберите направление: ");
  113.             if (TryGetNumber(out int userDirection, _directions.Count) == false)
  114.             {
  115.                 return;
  116.             }
  117.  
  118.             Console.Write("Укажите номер поезда: ");
  119.             if (TryGetNumber(out int userTrain, _trains.Count) == false)
  120.             {
  121.                 return;
  122.             }
  123.  
  124.             if (_trains[userTrain].IsTrainSent)
  125.             {
  126.                 Console.WriteLine("Поезд уже отправлен!");
  127.                 return;
  128.             }
  129.             else if (_trains[userTrain].IsDirectionSet)
  130.             {
  131.                 Console.WriteLine("Этому поезду уже задано направление!");
  132.                 return;
  133.             }
  134.  
  135.             foreach (Train train in _trains)
  136.             {
  137.                 foreach (Directions direction in _directions)
  138.                 {
  139.                     if(train.Direction == direction)
  140.                     {
  141.                         Console.WriteLine("Нельзя задать такое же направление другим поездам!");
  142.                         return;
  143.                     }
  144.                 }
  145.             }
  146.  
  147.  
  148.             _trains[userTrain] = new Train(_directions[userDirection], _trains[userTrain]);
  149.             _trains[userTrain].SetDirection();
  150.         }
  151.  
  152.         private void SellTickets()
  153.         {
  154.             if (CheckDirections() == false)
  155.             {
  156.                 return;
  157.             }
  158.  
  159.             Console.Write("Укажите номер поезда: ");
  160.             if (TryGetNumber(out int userNumber, _trains.Count) == false)
  161.             {
  162.                 return;
  163.             }
  164.             else if (CheckTickets(userNumber) == false)
  165.             {
  166.                 return;
  167.             }
  168.             else if (_trains[userNumber].IsTrainSent)
  169.             {
  170.                 Console.WriteLine("Нельзя повторно продать билеты на рейс!");
  171.                 return;
  172.             }
  173.  
  174.             Random rand = new Random();
  175.             _passengers = new Passengers(rand.Next(1, 20));
  176.             _directions[userNumber] = new Directions(_directions[userNumber], _passengers);
  177.             _passengers = null;
  178.         }
  179.  
  180.         private void SendTrain()
  181.         {
  182.             if (CheckDirections() == false)
  183.             {
  184.                 return;
  185.             }
  186.  
  187.             Console.Write("Укажите номер поезда: ");
  188.             if (TryGetNumber(out int userNumber, _trains.Count) == false)
  189.             {
  190.                 return;
  191.             }
  192.             else if(_trains[userNumber].IsDirectionSet == false)
  193.             {
  194.                 Console.WriteLine("Задайте поезду направление!");
  195.                 return;
  196.             }
  197.             else if (_trains[userNumber].IsTrainSent)
  198.             {
  199.                 Console.WriteLine("Поезд уже отправлен!");
  200.                 return;
  201.             }
  202.  
  203.             _trains[userNumber].SetDispatch();
  204.  
  205.             _trains[userNumber] = new Train(_directions[userNumber], _trains[userNumber]);
  206.             _trains[userNumber].CreateWagons();
  207.         }
  208.  
  209.         private void ShowInfo()
  210.         {
  211.             Console.Write("Для продолжения нажмите любую клавишу...");
  212.             Console.ReadKey(true);
  213.             Console.Clear();
  214.  
  215.             Console.WriteLine("Расписание поездов: ");
  216.  
  217.             Console.WriteLine("==============");
  218.  
  219.             if (_trains.Count == 0)
  220.             {
  221.                 Console.Write("В депо нет поездов!");
  222.             }
  223.             else if (_trains.Count > 0)
  224.             {
  225.                 Console.WriteLine("В депо находятся находятся:");
  226.                 foreach (var train in _trains)
  227.                 {
  228.                     if(train.IsDirectionSet)
  229.                     {
  230.                         Console.Write($"№{train.Number}, из гор.{train.Direction.DirectionFrom} платформа {train.Direction.PlatformFrom} " +
  231.                         $"скоро направится в город {train.Direction.DirectionToThe} платформа {train.Direction.PlatformToThe}." +
  232.                         $"Поезд не отправлен.\n");
  233.                         if (train.Wagons == 0)
  234.                             Console.Write("Без вагонов. Поезд не готов к отправке.\n");
  235.                         else if (train.Wagons > 0)
  236.                         {
  237.                             Console.Write("Вагонов: " + train.Wagons + " шт. ");
  238.                             Console.Write("Билетов продано: " + train.Direction.Clients + "\n");
  239.                             if (train.IsTrainSent)
  240.                             {
  241.                                 Console.Write("Поезд отправлен.\n");
  242.                             }    
  243.                             else
  244.                             {
  245.                                 Console.Write("Поезд не отправлен.\n");
  246.                             }
  247.                         }
  248.                     }
  249.                     else if(train.IsDirectionSet == false)
  250.                     {
  251.                         Console.WriteLine($"Поезд №{train.Number}. Без вагонов. Поезд не готов к отправке.\n");
  252.                     }
  253.                 }
  254.             }
  255.  
  256.             Console.WriteLine("Направления: ");
  257.             Console.WriteLine("==============");
  258.  
  259.             if (CheckDirections())
  260.             {
  261.                 foreach (var direction in _directions)
  262.                 {
  263.                     Console.Write($"Направление из гор.{direction.DirectionFrom} платформа {direction.PlatformFrom} " +
  264.                         $"в город {direction.DirectionToThe} платформа {direction.PlatformToThe}\n");
  265.                 }
  266.             }
  267.             Console.WriteLine("==============");
  268.         }
  269.  
  270.         private bool TryGetNumber(out int userInput, int count)
  271.         {
  272.             if (int.TryParse(Console.ReadLine(), out userInput) && userInput <= count && userInput > 0)
  273.             {
  274.                 --userInput;
  275.                 return true;
  276.             }
  277.             else
  278.             {
  279.                 Console.WriteLine("Ошибка! Такого номера нет.");
  280.             }
  281.             return false;
  282.         }
  283.  
  284.         private bool CheckDirections()
  285.         {
  286.             if (_directions.Count == 0)
  287.             {
  288.                 Console.WriteLine("Нет заданных направлений!");
  289.                 return false;
  290.             }
  291.             return true;
  292.         }
  293.  
  294.         private bool CheckTickets(int userNumber)
  295.         {
  296.             if (_directions[userNumber].ArePassengersCome)
  297.             {
  298.                 Console.WriteLine("На рейс уже куплены билеты!");
  299.                 return false;
  300.             }
  301.             return true;
  302.         }
  303.     }
  304.  
  305.     class Train
  306.     {
  307.         public bool IsTrainSent { get; private set; }
  308.         public bool IsDirectionSet { get; private set; }
  309.         public int Number { get; private set; }
  310.         public int Wagons { get; private set; }
  311.         public Directions Direction { get; private set; }
  312.  
  313.         public Train(int number, int wagons, bool isTrainSent = false, bool isDirectionSet = false, bool arePassengersCome = false)
  314.         {
  315.             Number = number;
  316.             Wagons = wagons;
  317.             IsTrainSent = isTrainSent;
  318.             IsDirectionSet = isDirectionSet;
  319.         }
  320.  
  321.         public Train(Directions direction, Train train)
  322.         {
  323.             Direction = direction;
  324.             Number = train.Number;
  325.             Wagons = train.Wagons;
  326.             IsTrainSent = train.IsTrainSent;
  327.             IsDirectionSet = train.IsDirectionSet;
  328.         }
  329.  
  330.         public void CreateWagons()
  331.         {
  332.             int wagonSeats;
  333.             Random rand = new Random();
  334.  
  335.             while(Direction.Clients.Tickets >= 0)
  336.             {
  337.  
  338.                 wagonSeats = rand.Next(30, 80);
  339.                 Direction.Clients.ActivateTickets(wagonSeats);
  340.                 ++Wagons;
  341.             }
  342.         }
  343.        
  344.         public void SetDispatch()
  345.         {
  346.             IsTrainSent = true;
  347.         }
  348.        
  349.         public void SetDirection()
  350.         {
  351.             IsDirectionSet = true;
  352.         }
  353.     }
  354.  
  355.     class Directions
  356.     {
  357.         public bool ArePassengersCome { get; private set; }
  358.         public string DirectionFrom { get; private set; }
  359.         public string PlatformFrom { get; private set; }
  360.         public string DirectionToThe { get; private set; }
  361.         public string PlatformToThe { get; private set; }
  362.         public Passengers Clients { get; private set; }
  363.  
  364.         public Directions(string directionFrom, string platformFrom, string directionToThe, string platformToThe, bool arePassengersCome = false)
  365.         {
  366.             DirectionFrom = directionFrom;
  367.             PlatformFrom = platformFrom;
  368.             PlatformToThe = platformToThe;
  369.             DirectionToThe = directionToThe;
  370.             ArePassengersCome = arePassengersCome;
  371.         }
  372.  
  373.         public Directions(Directions direction, Passengers passengers)
  374.         {
  375.             DirectionFrom = direction.DirectionFrom;
  376.             PlatformFrom = direction.PlatformFrom;
  377.             DirectionToThe = direction.DirectionToThe;
  378.             PlatformToThe = direction.PlatformToThe;
  379.             Clients = passengers;
  380.         }
  381.     }
  382.  
  383.     class Passengers
  384.     {
  385.         public int Tickets { get; private set; }
  386.  
  387.         public Passengers()
  388.         {
  389.             Tickets = 0;
  390.         }
  391.  
  392.         public Passengers(int tickets)
  393.         {
  394.             Tickets = tickets;
  395.         }
  396.  
  397.         public void ActivateTickets(int activateTickets)
  398.         {
  399.             Tickets -= activateTickets;
  400.         }
  401.  
  402.     }
  403.  
  404. }
  405.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement