NikaBang

ДЗ: Конфигуратор пассажирских поездов

Nov 24th, 2025 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.01 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Reflection;
  5.  
  6. internal class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         Dispatcher dispatcher = new Dispatcher();
  11.  
  12.         dispatcher.Work();
  13.     }
  14. }
  15.  
  16. public class Dispatcher
  17. {
  18.     private List<Train> _trains = new List<Train>();
  19.  
  20.     public void Work()
  21.     {
  22.         string commandCreateTrain = "1";
  23.         string commandExit = "2";
  24.         bool isWork = true;
  25.  
  26.         while (isWork)
  27.         {
  28.             Console.Clear();
  29.  
  30.             if (_trains.Count > 0)
  31.             {
  32.                 ShowInfoTrain();
  33.             }
  34.             else
  35.             {
  36.                 Console.WriteLine("Поездов пока нет.");
  37.             }
  38.  
  39.             Console.WriteLine(new string('-', 30));
  40.             Console.WriteLine($"{commandCreateTrain} - Создать поезд.\n{commandExit} - Завершить работу.");
  41.             string userInput = Console.ReadLine();
  42.  
  43.             if (userInput == commandCreateTrain)
  44.             {
  45.                 CreateTrain();
  46.             }
  47.             else if (userInput == commandExit)
  48.             {
  49.                 isWork = false;
  50.             }
  51.             else
  52.             {
  53.                 Console.WriteLine("Ошибка ввода.");
  54.                 Console.ReadKey();
  55.             }
  56.         }
  57.  
  58.         Console.WriteLine("Программа завершена.");
  59.     }
  60.  
  61.     private void CreateTrain()
  62.     {
  63.         Console.WriteLine("Назначим направление поезда!");
  64.         Direction direction = FormDirection();
  65.         Thread.Sleep(1000);
  66.  
  67.         Console.Write($"Продаем билеты на направление {direction}");
  68.         int passengers = GeneratePassengers();
  69.         WaitLittle();
  70.  
  71.         Console.WriteLine($"Продали {passengers} билетов.");
  72.         Console.Write("Собирам поезд согластно проданым билетам");
  73.         WaitLittle();
  74.  
  75.         int capacityWagon = CalculateCapacityWagon(passengers);
  76.         int numberWagons = CountNumberWagons(capacityWagon, passengers);
  77.  
  78.         Wagon[] wagons = new Wagon[numberWagons];
  79.  
  80.         for (int i = 0; i < wagons.Length; i++)
  81.         {
  82.             wagons[i] = new Wagon(capacityWagon);
  83.         }
  84.  
  85.         Train train = (new Train(passengers, direction, wagons));
  86.  
  87.         _trains.Add(train);
  88.  
  89.         Console.WriteLine("Поезд готов!");
  90.         Console.ReadKey();
  91.     }
  92.  
  93.     private void ShowInfoTrain()
  94.     {
  95.         foreach (Train train in _trains)
  96.         {
  97.             train.ShowInfo();
  98.         }
  99.     }
  100.  
  101.     private Direction FormDirection()
  102.     {
  103.         Console.Write("Город отправления: ");
  104.         string from = Console.ReadLine();
  105.         Console.Write("Город прибытия: ");
  106.         string to = Console.ReadLine();
  107.  
  108.         return new Direction(from, to);
  109.     }
  110.  
  111.     private int GeneratePassengers()
  112.     {
  113.         int minRandom = 10;
  114.         int maxRandom = 300;
  115.         Random random = new Random();
  116.  
  117.         return random.Next(minRandom, maxRandom + 1); ;
  118.     }
  119.  
  120.     private int CalculateCapacityWagon(int passengers)
  121.     {
  122.         int maxPassengersForSmallTrain = 200;
  123.         int smallWagon = 20;
  124.         int bigWagon = 30;
  125.  
  126.         if (passengers > maxPassengersForSmallTrain)
  127.         {
  128.             return bigWagon;
  129.         }
  130.         else
  131.         {
  132.             return smallWagon;
  133.         }
  134.     }
  135.  
  136.     private int CountNumberWagons(int capacityWagon, int passengers)
  137.     {
  138.         return (int)Math.Ceiling((double)passengers / capacityWagon);
  139.     }
  140.  
  141.     private void WaitLittle()
  142.     {
  143.         int dotCount = 5;
  144.         int delayMilliseconds = 700;
  145.  
  146.         for (int i = 0; i < dotCount; i++)
  147.         {
  148.             Console.Write(".");
  149.             Thread.Sleep(delayMilliseconds);
  150.         }
  151.  
  152.         Console.WriteLine();
  153.     }
  154. }
  155.  
  156. public class Train
  157. {
  158.     private List<Wagon> _wagons = new List<Wagon>();
  159.     private Direction _direction;
  160.     private int _passengers;
  161.  
  162.     public Train(int passengers, Direction direction, Wagon[] wagon)
  163.     {
  164.         _passengers = passengers;
  165.         _direction = direction;
  166.         _wagons.AddRange(wagon);
  167.     }
  168.  
  169.     public void ShowInfo()
  170.     {
  171.         Console.WriteLine($"Поезд {_direction}.\n{_wagons[0]}.\nВсего вагонов: {_wagons.Count}.\nПассажиров: {_passengers}.");
  172.     }
  173. }
  174.  
  175. public class Direction
  176. {
  177.     private string _from;
  178.     private string _to;
  179.  
  180.     public Direction(string from, string to)
  181.     {
  182.         _from = from;
  183.         _to = to;
  184.     }
  185.  
  186.     public override string ToString()
  187.     {
  188.         return $"{_from} - {_to}";
  189.     }
  190. }
  191.  
  192. public class Wagon
  193. {
  194.     public Wagon(int capacity)
  195.     {
  196.         Capacity = capacity;
  197.     }
  198.  
  199.     public int Capacity { get; private set; }
  200.  
  201.     public override string ToString()
  202.     {
  203.         return $"Вагон рассчитан {Capacity} пассажиров";
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment