Advertisement
Torgach

testrain

Apr 8th, 2021
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.87 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.                         ChooseDirection();
  58.                         break;
  59.                     case "4":
  60.                         SellTickets();
  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 userDirection = new Directions(directionFrom, platformFrom, directionToThe, platformToThe);
  96.  
  97.             foreach (Directions direction in _directions)
  98.             {
  99.                 //if (direction == userDirection)
  100.                 if(direction.DirectionFrom == userDirection.DirectionFrom &&
  101.                     direction.PlatformFrom == userDirection.PlatformFrom &&
  102.                     direction.DirectionToThe == userDirection.DirectionToThe &&
  103.                     direction.PlatformToThe == userDirection.PlatformToThe)
  104.                 {
  105.                     Console.WriteLine("Нельзя задать такое же направление другим поездам!");
  106.                     return;
  107.                 }
  108.             }
  109.  
  110.             _directions.Add(userDirection);
  111.  
  112.             Console.WriteLine("Выберите поезд из депо: ");
  113.             if (TryGetNumber(out int userNumber, _trains.Count) && _trains[userNumber].IsDirectionSet == false)
  114.             {
  115.                 _trains[userNumber] = new Train(userDirection, _trains[userNumber]);
  116.                 _trains[userNumber].SetDirection();
  117.             }
  118.             else if(_trains[userNumber].IsDirectionSet)
  119.             {
  120.                 Console.WriteLine("Этому поезду уже задано направление! ");
  121.                 return;
  122.             }
  123.         }
  124.  
  125.         private void ChooseDirection()
  126.         {
  127.             if (AreDirectionsEmpty() == false)
  128.             {
  129.                 return;
  130.             }
  131.  
  132.             Console.WriteLine("Выберите направление: ");
  133.             if (TryGetNumber(out int userDirection, _directions.Count) == false)
  134.             {
  135.                 return;
  136.             }
  137.  
  138.             Console.Write("Укажите номер поезда: ");
  139.             if (TryGetNumber(out int userTrain, _trains.Count) == false)
  140.             {
  141.                 return;
  142.             }
  143.  
  144.             if (_trains[userTrain].IsTrainSent)
  145.             {
  146.                 Console.WriteLine("Поезд уже отправлен!");
  147.                 return;
  148.             }
  149.             else if (_trains[userTrain].IsDirectionSet)
  150.             {
  151.                 Console.WriteLine("Этому поезду уже задано направление!");
  152.                 return;
  153.             }
  154.  
  155.             foreach (Train train in _trains)
  156.             {
  157.                 foreach (Directions direction in _directions)
  158.                 {
  159.                     if(train.Direction == direction)
  160.                     {
  161.                         Console.WriteLine("Нельзя задать такое же направление другим поездам!");
  162.                         return;
  163.                     }
  164.                 }
  165.             }
  166.  
  167.  
  168.             _trains[userTrain] = new Train(_directions[userDirection], _trains[userTrain]);
  169.             _trains[userTrain].SetDirection();
  170.         }
  171.  
  172.         private void SellTickets()
  173.         {
  174.             if (AreDirectionsEmpty() == false && AreTrainsEmpty() == false)
  175.             {
  176.                 return;
  177.             }
  178.  
  179.             Console.Write("Укажите номер поезда: ");
  180.             if (TryGetNumber(out int userNumber, _trains.Count) == false)
  181.             {
  182.                 return;
  183.             }
  184.             else if(_trains[userNumber].IsDirectionSet == false)
  185.             {
  186.                 Console.WriteLine("Задайте поезду направление!");
  187.                 return;
  188.             }
  189.             else if (CheckTickets(userNumber) == false)
  190.             {
  191.                 return;
  192.             }
  193.             else if (_trains[userNumber].IsTrainSent)
  194.             {
  195.                 Console.WriteLine("Поезд уже в рейсе!");
  196.                 return;
  197.             }
  198.  
  199.             Random rand = new Random();
  200.             _passengers = new Passengers(rand.Next(200,400));
  201.             _directions[userNumber] = new Directions(_directions[userNumber], _passengers);
  202.  
  203.             _passengers = null;
  204.         }
  205.  
  206.         private void SendTrain()
  207.         {
  208.             if (AreDirectionsEmpty() == false && AreTrainsEmpty() == false)
  209.             {
  210.                 return;
  211.             }
  212.  
  213.             Console.Write("Укажите номер поезда: ");
  214.             if (TryGetNumber(out int userNumber, _trains.Count) == false)
  215.             {
  216.                 return;
  217.             }
  218.             else if(_trains[userNumber].IsDirectionSet == false)
  219.             {
  220.                 Console.WriteLine("Задайте поезду направление!");
  221.                 return;
  222.             }
  223.             else if (_trains[userNumber].IsTrainSent)
  224.             {
  225.                 Console.WriteLine("Поезд уже отправлен!");
  226.                 return;
  227.             }
  228.  
  229.             _trains[userNumber].SetDispatch();
  230.  
  231.             _trains[userNumber] = new Train(_directions[userNumber], _trains[userNumber]);
  232.             _trains[userNumber].CreateWagons();
  233.             //
  234.         }
  235.  
  236.         private void ShowInfo()
  237.         {
  238.             Console.Write("Для продолжения нажмите любую клавишу...");
  239.             Console.ReadKey(true);
  240.             Console.Clear();
  241.  
  242.             Console.WriteLine("Расписание поездов: ");
  243.  
  244.             Console.WriteLine("==============");
  245.  
  246.             if (_trains.Count == 0)
  247.             {
  248.                 Console.Write("В депо нет поездов!");
  249.             }
  250.             else if (_trains.Count > 0)
  251.             {
  252.                 Console.WriteLine("В депо находятся находятся:");
  253.                 foreach (var train in _trains)
  254.                 {
  255.                     if(train.IsDirectionSet)
  256.                     {
  257.                         Console.WriteLine($"№{train.Number}, из гор.{train.Direction.DirectionFrom} платформа {train.Direction.PlatformFrom} " +
  258.                         $"скоро направится в город {train.Direction.DirectionToThe} платформа {train.Direction.PlatformToThe}.");
  259.  
  260.                         if(train.Direction.Clients.ArePassengersCome == false)
  261.                         {
  262.                             Console.WriteLine("Билеты не проданы!");
  263.                         }
  264.  
  265.                         if (train.Wagons == 0)
  266.                             Console.Write("Поезд не выехал из депо. Поезд не готов к отправке.\n");
  267.                         else if (train.Wagons > 0)
  268.                         {
  269.                             Console.Write("Вагонов: " + train.Wagons + " шт. ");
  270.                             Console.Write("Билетов продано: " + train.Direction.Clients.Tickets + "\n");
  271.  
  272.                             if (train.IsTrainSent)
  273.                             {
  274.                                 Console.Write("Поезд отправлен.\n");
  275.                             }
  276.                             else if (train.IsTrainSent == false)
  277.                             {
  278.                                 Console.Write("Поезд не отправлен.\n");
  279.                             }
  280.                         }
  281.                     }
  282.                     else if(train.IsDirectionSet == false)
  283.                     {
  284.                         Console.WriteLine($"Поезд №{train.Number}. Без направления. Поезд не готов к отправке.\n");
  285.                     }
  286.                 }
  287.             }
  288.  
  289.             Console.WriteLine("Направления: ");
  290.             Console.WriteLine("==============");
  291.  
  292.             if (AreDirectionsEmpty())
  293.             {
  294.                 foreach (var direction in _directions)
  295.                 {
  296.                     Console.Write($"Направление из гор.{direction.DirectionFrom} платформа {direction.PlatformFrom} " +
  297.                         $"в город {direction.DirectionToThe} платформа {direction.PlatformToThe}\n");
  298.                     if (direction.Clients.ArePassengersCome == false)
  299.                     {
  300.                         Console.WriteLine("Билеты не проданы!");
  301.                     }
  302.                     else if (direction.Clients.ArePassengersCome)
  303.                     {
  304.                         Console.WriteLine($"Количество проданных билетов: {direction.Clients.Tickets}");
  305.                     }
  306.                 }
  307.             }
  308.             Console.WriteLine("==============");
  309.         }
  310.  
  311.         private bool TryGetNumber(out int userInput, int count)
  312.         {
  313.             if (int.TryParse(Console.ReadLine(), out userInput) && userInput <= count && userInput > 0)
  314.             {
  315.                 --userInput;
  316.                 return true;
  317.             }
  318.             else
  319.             {
  320.                 Console.WriteLine("Ошибка! Такого номера нет.");
  321.             }
  322.             return false;
  323.         }
  324.  
  325.         private bool AreDirectionsEmpty()
  326.         {
  327.             if (_directions.Count == 0)
  328.             {
  329.                 Console.WriteLine("Нет заданных направлений!");
  330.                 return false;
  331.             }
  332.             return true;
  333.         }
  334.  
  335.         private bool AreTrainsEmpty()
  336.         {
  337.             if (_trains.Count == 0)
  338.             {
  339.                 Console.WriteLine("Нет созданных поездов!");
  340.                 return false;
  341.             }
  342.             return true;
  343.         }
  344.  
  345.         private bool CheckTickets(int userNumber)
  346.         {
  347.             if (_directions[userNumber].Clients.ArePassengersCome)
  348.             {
  349.                 Console.WriteLine("На рейс не куплены билеты!");
  350.                 return false;
  351.             }
  352.             return true;
  353.         }
  354.     }
  355.  
  356.     class Train
  357.     {
  358.         public bool IsTrainSent { get; private set; }
  359.         public bool IsDirectionSet { get; private set; }
  360.         public int Number { get; private set; }
  361.         public int Wagons { get; private set; }
  362.         public Directions Direction { get; private set; }
  363.  
  364.         public Train(int number, int wagons, bool isTrainSent = false, bool isDirectionSet = false)
  365.         {
  366.             Number = number;
  367.             Wagons = wagons;
  368.             IsTrainSent = isTrainSent;
  369.             IsDirectionSet = isDirectionSet;
  370.         }
  371.  
  372.         public Train(Directions direction, Train train)
  373.         {
  374.             Direction = direction;
  375.             Number = train.Number;
  376.             Wagons = train.Wagons;
  377.             IsTrainSent = train.IsTrainSent;
  378.             IsDirectionSet = train.IsDirectionSet;
  379.         }
  380.  
  381.         public void CreateWagons()
  382.         {
  383.             int wagonSeats;
  384.             int activateTickets = Direction.Clients.Tickets;
  385.             Random rand = new Random();
  386.  
  387.             while(activateTickets >= 0)
  388.             {
  389.                 wagonSeats = rand.Next(30, 80);
  390.                 activateTickets -= wagonSeats;
  391.                 ++Wagons;
  392.             }
  393.            
  394.         }
  395.        
  396.         public void SetDispatch()
  397.         {
  398.             IsTrainSent = true;
  399.         }
  400.        
  401.         public void SetDirection()
  402.         {
  403.             IsDirectionSet = true;
  404.         }
  405.     }
  406.  
  407.     class Directions
  408.     {
  409.         public string DirectionFrom { get; private set; }
  410.         public string PlatformFrom { get; private set; }
  411.         public string DirectionToThe { get; private set; }
  412.         public string PlatformToThe { get; private set; }
  413.         public Passengers Clients { get; private set; }
  414.  
  415.         public Directions(string directionFrom, string platformFrom, string directionToThe, string platformToThe)
  416.         {
  417.             DirectionFrom = directionFrom;
  418.             PlatformFrom = platformFrom;
  419.             PlatformToThe = platformToThe;
  420.             DirectionToThe = directionToThe;
  421.             Clients = new Passengers();
  422.         }
  423.  
  424.         public Directions(Directions direction, Passengers passengers)
  425.         {
  426.             DirectionFrom = direction.DirectionFrom;
  427.             PlatformFrom = direction.PlatformFrom;
  428.             DirectionToThe = direction.DirectionToThe;
  429.             PlatformToThe = direction.PlatformToThe;
  430.             Clients = passengers;
  431.         }
  432.     }
  433.  
  434.     class Passengers
  435.     {
  436.         private bool _arePassengersCome;
  437.         public bool ArePassengersCome
  438.         {
  439.             get
  440.             {
  441.                 return Tickets > 0;
  442.             }
  443.             private set
  444.             {
  445.                 _arePassengersCome = value;
  446.             }
  447.         }
  448.  
  449.         public int Tickets { get; private set; }
  450.  
  451.         public Passengers()
  452.         {
  453.             Tickets = 0;
  454.             ArePassengersCome = false;
  455.         }
  456.  
  457.         public Passengers(int tickets)
  458.         {
  459.             Tickets = tickets;
  460.             ArePassengersCome = true;
  461.         }
  462.  
  463.         //public void FixateTickets()
  464.         //{
  465.         //    Tickets = 0;
  466.         //    ArePassengersCome = false;
  467.         //}
  468.  
  469.     }
  470.  
  471. }
  472.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement