Advertisement
JohnJuly

Homework44

Jun 16th, 2025 (edited)
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Homework44
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Dispather dispather = new Dispather();
  11.             dispather.isWork();
  12.         }
  13.     }
  14.  
  15.     class Dispather
  16.     {
  17.         private List<Train> _trains = new List<Train>();
  18.  
  19.         public void isWork()
  20.         {
  21.             const string CommandCreateTrain = "go";
  22.             const string CommandExitMenu = "exit";
  23.             const string CommandShowTrains = "show";
  24.  
  25.             bool isWork = true;
  26.  
  27.             while (isWork)
  28.             {
  29.                 ShowShortTrainInfo();
  30.  
  31.                 Console.WriteLine("\nВы находитесь в меню создания поездов");
  32.                 Console.WriteLine($"Чтобы создать поезд введите {CommandCreateTrain}");
  33.                 Console.WriteLine($"Чтобы показать список поездов введите {CommandShowTrains}");
  34.                 Console.WriteLine($"Чтобы выйти из меню введите {CommandExitMenu}");
  35.                 Console.Write("Выберите действие: ");
  36.  
  37.                 string userInput = Console.ReadLine();
  38.  
  39.                 switch (userInput)
  40.                 {
  41.                     case CommandCreateTrain:
  42.                         CreateTrain();
  43.                         break;
  44.  
  45.                     case CommandShowTrains:
  46.                         ShowTrainInformation();
  47.                         break;
  48.  
  49.                     case CommandExitMenu:
  50.                         isWork = ExitProgram();
  51.                         break;
  52.  
  53.                     default:
  54.                         Console.WriteLine("Некорректный ввод, попробуйте еще раз.");
  55.                         break;
  56.                 }
  57.  
  58.                 Console.ReadKey();
  59.                 Console.Clear();
  60.             }
  61.         }
  62.  
  63.         public void ShowTrainInformation()
  64.         {
  65.             if (_trains.Count == 0)
  66.             {
  67.                 Console.WriteLine("Поездов пока нет.");
  68.                 return;
  69.             }
  70.  
  71.             Console.WriteLine("Список поездов:");
  72.  
  73.             for (int i = 0; i < _trains.Count; i++)
  74.             {
  75.                 Console.WriteLine($"Поезд {i + 1}: {_trains[i].Direction}, " +
  76.                 $"Пассажиров: {_trains[i].PassengerCount}, Вагонов: {_trains[i].Wagons.Count}");
  77.             }
  78.         }
  79.  
  80.         private bool ExitProgram()
  81.         {
  82.             Console.WriteLine("Вы вышли из программы!");
  83.             return false;
  84.         }
  85.  
  86.         private void CreateTrain()
  87.         {
  88.             string direction = CreateDirection();
  89.             int passengerCount = SellTickets();
  90.  
  91.             if (passengerCount == -1)
  92.             {
  93.                 Console.WriteLine("Продажа билетов отменена.");
  94.                 return;
  95.             }
  96.  
  97.             Train train = new Train(direction, passengerCount);
  98.             FormTrain(train);
  99.  
  100.             Console.WriteLine("\nИнформация о созданном поезде:");
  101.             train.ShowInfo();
  102.  
  103.             _trains.Add(train);
  104.         }
  105.  
  106.         private string CreateDirection()
  107.         {
  108.             string departure, arrival;
  109.             TrainRouteGeneration locationGenerator = new TrainRouteGeneration();
  110.             locationGenerator.GetDifferentLocations(out departure, out arrival);
  111.             return $"{departure} - {arrival}";
  112.         }
  113.  
  114.         private int SellTickets()
  115.         {
  116.             Random random = new Random();
  117.             int passengerCount = random.Next(50, 201);
  118.  
  119.             Console.WriteLine($"Продано билетов: {passengerCount}");
  120.             return passengerCount;
  121.         }
  122.  
  123.         private void FormTrain(Train train)
  124.         {
  125.             int wagonCapacity = 24;
  126.             int remainingPassengers = train.PassengerCount;
  127.  
  128.             while (remainingPassengers > 0)
  129.             {
  130.                 Wagon wagon = new Wagon();
  131.  
  132.                 int passengersForThisWagon = Math.Min(wagonCapacity, remainingPassengers);
  133.  
  134.                 wagon.Fill(passengersForThisWagon);
  135.                 train.Wagons.Add(wagon);
  136.                 remainingPassengers -= passengersForThisWagon;
  137.             }
  138.         }
  139.  
  140.         private void ShowShortTrainInfo()
  141.         {
  142.             if (_trains.Count == 0)
  143.             {
  144.                 Console.WriteLine("Поездов пока нет.");
  145.                 return;
  146.             }
  147.  
  148.             Console.WriteLine("Существующие поезда:");
  149.  
  150.             for (int i = 0; i < _trains.Count; i++)
  151.             {
  152.                 Console.WriteLine($"Поезд {i + 1}: {_trains[i].Direction}," +
  153.                 $" Пассажиров: {_trains[i].PassengerCount}, Вагонов: {_trains[i].Wagons.Count}");
  154.             }
  155.         }
  156.     }
  157.  
  158.     class Train
  159.     {
  160.         public List<Wagon> Wagons = new List<Wagon>();
  161.  
  162.         public Train(string direction, int passengerCount)
  163.         {
  164.             Direction = direction;
  165.             PassengerCount = passengerCount;
  166.         }
  167.  
  168.         public string Direction { get; private set; }
  169.         public int PassengerCount { get; private set; }
  170.  
  171.         public void ShowInfo()
  172.         {
  173.             Console.WriteLine($"Направление: {Direction}");
  174.             Console.WriteLine($"Количество пассажиров: {PassengerCount}");
  175.             Console.WriteLine($"Количество вагонов: {Wagons.Count}");
  176.             Console.WriteLine("Информация по вагонам:");
  177.  
  178.             for (int i = 0; i < Wagons.Count; i++)
  179.             {
  180.                 Console.WriteLine($"  Вагон {i + 1}: Занято мест: {Wagons[i].OccupiedSeats}");
  181.             }
  182.         }
  183.     }
  184.  
  185.     public class Wagon
  186.     {
  187.         public Wagon()
  188.         {
  189.             Capacity = 24;
  190.         }
  191.  
  192.         public int Capacity { get; private set; }
  193.         public int OccupiedSeats { get; private set; }
  194.  
  195.         public void Fill(int passengers)
  196.         {
  197.             if (passengers <= Capacity)
  198.             {
  199.                 OccupiedSeats = passengers;
  200.             }
  201.             else
  202.             {
  203.                 OccupiedSeats = Capacity;
  204.                 Console.WriteLine("Превышена вместимость вагона!");
  205.             }
  206.         }
  207.     }
  208.  
  209.     class TrainRouteGeneration
  210.     {
  211.         private Random _random = new Random();
  212.  
  213.         private string[] _locations = new string[]
  214.         {
  215.         "Москва", "Санкт-Петербург", "Калуга", "Тверь", "Ярославль", "Краснодар", "Ростов", "Вологда",
  216.         "Нижний Новгород", "Влаливосток", "Екатеринбург", "Самара", "Сочи",
  217.         "Белгород", "Брянск", "Сергиев Посад", "Козельск","Иваново"
  218.         };
  219.  
  220.         public string GetRandomLocation()
  221.         {
  222.             return _locations[_random.Next(0, _locations.Length)];
  223.         }
  224.  
  225.         public void GetDifferentLocations(out string departure, out string arrival)
  226.         {
  227.             departure = GetRandomLocation();
  228.  
  229.             List<string> possibleArrivals = new List<string>();
  230.  
  231.             foreach (string city in _locations)
  232.             {
  233.                 if (city != departure)
  234.                 {
  235.                     possibleArrivals.Add(city);
  236.                 }
  237.             }
  238.  
  239.             if (possibleArrivals.Count == 0)
  240.             {
  241.                 arrival = departure;
  242.                 return;
  243.             }
  244.  
  245.             arrival = possibleArrivals[_random.Next(possibleArrivals.Count)];
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement