RedFlys

Home work - train manager

Dec 14th, 2021 (edited)
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.77 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 Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             LogisticsManager manager = new LogisticsManager();
  14.  
  15.             Console.WriteLine("Загрузка системы.");
  16.             Console.WriteLine("Нажмите любую клавишу, что бы продолжить.");
  17.             Console.ReadKey();
  18.             Console.Clear();
  19.  
  20.             manager.Work();
  21.         }
  22.     }
  23.  
  24.     class LogisticsManager
  25.     {        
  26.         private List<Train> _trains;
  27.  
  28.         public LogisticsManager()
  29.         {
  30.             _trains = new List<Train>();
  31.         }
  32.  
  33.         public void Work()
  34.         {
  35.             bool isWorking = true;
  36.  
  37.             while (isWorking)
  38.             {
  39.                 Console.Write("1. Сформировать поезд. \n" +
  40.                     "2. Посмотреть все отправленные поезда.\n" +
  41.                     "3. Выход.\n" +
  42.                     "Введите номер команды: ");
  43.  
  44.                 switch (Console.ReadLine())
  45.                 {
  46.                     case "1":
  47.                         CreateTrain();
  48.                         break;
  49.  
  50.                     case "2":
  51.                         ShowAllTrains();
  52.                         break;
  53.  
  54.                     case "3":
  55.                         isWorking = false;
  56.                         break;
  57.  
  58.                     default:
  59.                         Console.WriteLine("Команда не распознана.");
  60.                         Console.ReadKey();
  61.                         break;
  62.                 }
  63.                
  64.                 Console.Clear();
  65.             }
  66.         }
  67.  
  68.         private void CreateTrain()
  69.         {
  70.             Direction direction = new Direction();
  71.             CashierOffice cashierOffice = new CashierOffice();
  72.             Console.Clear();
  73.  
  74.             direction.Create();
  75.  
  76.             cashierOffice.ApproveTicketPrice();
  77.             cashierOffice.SellTickets();
  78.  
  79.             Train train = new Train(direction, cashierOffice);
  80.             train.AddVagons();
  81.  
  82.             train.ShowInfo();
  83.  
  84.             _trains.Add(train);
  85.             SendTrain(train);            
  86.         }
  87.  
  88.         private void SendTrain(Train train)
  89.         {
  90.             Console.WriteLine("Введите любую клавишу для отправки поезда.");
  91.             Console.ReadKey();
  92.             Console.Clear();
  93.  
  94.             train.Send();
  95.  
  96.             Console.WriteLine("Поезд отправлен!");
  97.             Console.ReadKey();
  98.         }      
  99.        
  100.         private void ShowAllTrains()
  101.         {
  102.             Console.Clear();
  103.  
  104.             int index = 1;
  105.  
  106.             if (_trains.Count > 0)
  107.             {
  108.                 foreach (Train train in _trains)
  109.                 {
  110.                     Console.WriteLine($"Поезд {index}.");
  111.                     train.ShowInfo();
  112.                     Console.WriteLine("______________________________________________________________________\n");
  113.                     index++;
  114.                 }
  115.             }
  116.             else
  117.             {
  118.                 Console.WriteLine("Вы ещё не создали ни ондого поезда.\n" +
  119.                     "Приступайте к работе.");
  120.             }
  121.  
  122.             Console.ReadKey();
  123.         }
  124.     }
  125.  
  126.     class Direction
  127.     {
  128.         private static IReadOnlyList<string> _stations;
  129.         private string _pointOfDeparture;
  130.         private string _pointOfArrival;
  131.  
  132.         static Direction ()
  133.         {
  134.             _stations = new List<string> { "Москва", "Санкт-Петербург", "Новосибирск", "Казань", "Хабаровск", "Владивосток", "Томск", "Калининград", "Самара" };            
  135.         }
  136.  
  137.         public Direction()
  138.         {
  139.             _pointOfDeparture = "Отсутствует";
  140.             _pointOfArrival = "Отсутсвует";
  141.         }
  142.  
  143.         public string PointOfDeparture => _pointOfDeparture;
  144.         public string PointOfArrival => _pointOfArrival;
  145.  
  146.         public void Create()
  147.         {
  148.             bool isCreate = false;
  149.  
  150.             while (isCreate == false)
  151.             {
  152.                 ShowStations();
  153.                 Console.WriteLine();
  154.  
  155.                 _pointOfDeparture = ChooseStation("с которой отправиться поезд");
  156.                 Console.WriteLine($"Станция отправления поезда - {_pointOfDeparture}. \n");
  157.  
  158.                 _pointOfArrival = ChooseStation("на которую прибудет поезд");
  159.                 Console.WriteLine($"Станция прибытия поезда - {_pointOfArrival}. \n");
  160.  
  161.                 if (_pointOfDeparture == _pointOfArrival)
  162.                 {
  163.                     Console.WriteLine("Ошибка, ты не можешь уехать и приехать в один и тот же город, это бессмыслено! Давай по новой!");
  164.                 }
  165.                 else
  166.                 {
  167.                     isCreate = true;
  168.  
  169.                     Console.WriteLine("");
  170.                 }
  171.                 Console.ReadKey();
  172.                 Console.Clear();
  173.             }
  174.         }
  175.  
  176.         private void ShowStations()
  177.         {
  178.             int index = 1;
  179.  
  180.             foreach (string station in _stations)
  181.             {                
  182.                 Console.WriteLine(index + ". " + station);
  183.                 index++;
  184.             }
  185.         }
  186.  
  187.         private string ChooseStation(string messageForChoose)
  188.         {
  189.             bool isSelected = false;
  190.             string userInput;
  191.  
  192.             while (isSelected == false)
  193.             {
  194.                 Console.Write($"Введите номер станции, {messageForChoose}: ");
  195.                 userInput = Console.ReadLine();
  196.  
  197.                 if (Int32.TryParse(userInput, out int index))
  198.                 {
  199.                     if (index > 0 && index <= _stations.Count)
  200.                     {
  201.                         return _stations[index - 1];                        
  202.                     }
  203.                     else
  204.                     {
  205.                         Console.WriteLine("Станции с таким индексом нет.");
  206.                     }
  207.                 }
  208.                 else
  209.                 {
  210.                     Console.WriteLine("Это не число.");
  211.                 }
  212.             }
  213.  
  214.             return null;
  215.         }        
  216.     }
  217.  
  218.     class CashierOffice
  219.     {
  220.         static private int _maxPassengers;
  221.         private int _passengers;
  222.         private int _ticketPrice;
  223.  
  224.         static CashierOffice()
  225.         {
  226.             _maxPassengers = 1000;
  227.         }
  228.  
  229.         public CashierOffice()
  230.         {
  231.             _passengers = 0;
  232.             _ticketPrice = 0;
  233.         }
  234.  
  235.         public int Passengers => _passengers;
  236.         public int Cash => _passengers * _ticketPrice;
  237.  
  238.         public void SellTickets()
  239.         {
  240.             Random random = new Random();
  241.  
  242.             _passengers = random.Next(_maxPassengers);
  243.         }
  244.  
  245.         public void ApproveTicketPrice()
  246.         {
  247.             bool isSetPrice = false;
  248.  
  249.             while (isSetPrice == false)
  250.             {
  251.                 Console.Clear();
  252.  
  253.                 Console.Write("Задайте цену билета: ");
  254.  
  255.                 if (Int32.TryParse(Console.ReadLine(), out int price))
  256.                 {
  257.                     if (price > 0)
  258.                     {
  259.                         Console.WriteLine($"Цена билета на этот поезд - {price} рублей.");
  260.                         _ticketPrice = price;
  261.                         isSetPrice = true;
  262.                     }
  263.                     else
  264.                     {
  265.                         Console.WriteLine("Цена не может быть отрицательна или равна 0.");
  266.                     }
  267.                 }
  268.                 else
  269.                 {
  270.                     Console.WriteLine("Введите целое число больше 0.");
  271.                 }
  272.             }
  273.  
  274.             Console.ReadKey();
  275.             Console.Clear();
  276.         }
  277.  
  278.         public void ShowInfo()
  279.         {
  280.             Console.WriteLine($"Продано {_passengers} билетов по {_ticketPrice} рублей на общую сумму {Cash} рублей.");
  281.         }
  282.     }
  283.  
  284.     class Train
  285.     {
  286.         private List<Carriage> _carriages;
  287.         private Direction _direction;
  288.         private CashierOffice _cashierOffice;
  289.         private int _passengers;
  290.         private int _numberOfSeats;
  291.         private bool _isSend;
  292.  
  293.         public Train(Direction direction, CashierOffice cashierOffice)
  294.         {
  295.             _carriages = new List<Carriage>();
  296.             _direction = direction;
  297.             _cashierOffice = cashierOffice;
  298.             _passengers = _cashierOffice.Passengers;
  299.             _isSend = false;
  300.         }
  301.        
  302.         public bool IsEnoughPlaces => _numberOfSeats - _passengers >= 0;
  303.  
  304.         public void AddVagons()
  305.         {
  306.             int capacitySmallCarriage = 20;
  307.             int capacityMediumCarriage = 60;
  308.             int capacityLargeCarriage = 100;
  309.  
  310.             while (IsEnoughPlaces == false)
  311.             {
  312.                 Console.WriteLine($"Необходимы места для {_passengers} пассажиров.");
  313.                 ShowCapacity();
  314.                 Console.WriteLine("\nНужны ещё вагоны.");
  315.                 Console.WriteLine($"1. Добавить маленький вагон. Вместимость - {capacitySmallCarriage} мест.\n" +
  316.                     $"2. Добавить средний вагон. Вместимость - {capacityMediumCarriage} мест.\n" +
  317.                     $"3. Добавить большой вагон. Вместимость - {capacityLargeCarriage} мест.\n");
  318.                 Console.Write("Введите, какой вагон добавить: ");
  319.  
  320.                 switch (Console.ReadLine())
  321.                 {
  322.                     case "1":
  323.                         _carriages.Add(new SmallCarriage(capacitySmallCarriage));
  324.                         break;
  325.  
  326.                     case "2":
  327.                         _carriages.Add(new MediumCarriage(capacityMediumCarriage));
  328.                         break;
  329.  
  330.                     case "3":
  331.                         _carriages.Add(new LargeCarriage(capacityLargeCarriage));
  332.                         break;
  333.  
  334.                     default:
  335.                         Console.WriteLine("Такого вагона нет.");
  336.                         break;
  337.                 }
  338.  
  339.                 SumPlaces();
  340.  
  341.                 Console.WriteLine("Нажмите любую клавишу для продолжения.");
  342.                 Console.ReadKey();
  343.                 Console.Clear();
  344.             }
  345.         }
  346.  
  347.         public void ShowInfo()
  348.         {
  349.             if (IsEnoughPlaces && _isSend == false)
  350.             {
  351.                 Console.WriteLine($"Поезд готов к отправке.");
  352.                 ShowCapacity();
  353.             }
  354.             else if (_isSend)
  355.             {
  356.                 Console.WriteLine("Поезд отправлен.");
  357.                 ShowCapacity();
  358.                 _cashierOffice.ShowInfo();
  359.                 Console.WriteLine($"Маршрут {_direction.PointOfDeparture} - {_direction.PointOfArrival}.");
  360.             }
  361.         }
  362.  
  363.         public void Send()
  364.         {
  365.             _isSend = true;
  366.         }
  367.  
  368.         private void SumPlaces()
  369.         {
  370.             int countPlaces = 0;
  371.  
  372.             foreach (var carriage in _carriages)
  373.             {
  374.                 countPlaces += carriage.NumberPlaces;
  375.             }
  376.  
  377.             _numberOfSeats = countPlaces;
  378.         }
  379.  
  380.         private void ShowCapacity()
  381.         {
  382.             Console.WriteLine($"Вы подготовили {_carriages.Count} вагон(ов), общей вместительностью на {_numberOfSeats} мест.");
  383.         }
  384.     }
  385.  
  386.     class Carriage
  387.     {
  388.         protected int Capacity;        
  389.  
  390.         public Carriage (int capacity)
  391.         {
  392.             Capacity = capacity;
  393.         }
  394.  
  395.         public int NumberPlaces => Capacity;        
  396.     }
  397.  
  398.     class SmallCarriage : Carriage
  399.     {
  400.         public SmallCarriage(int capacity) : base(capacity) { }
  401.     }
  402.  
  403.     class MediumCarriage : Carriage
  404.     {
  405.         public MediumCarriage(int capacity) : base(capacity) { }
  406.     }
  407.  
  408.     class LargeCarriage : Carriage
  409.     {
  410.         public LargeCarriage(int capacity) : base(capacity) { }
  411.     }
  412. }
Add Comment
Please, Sign In to add comment