lovelyvook

Unit_43

Jul 29th, 2024 (edited)
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Ijunior
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Dispatcher dispatcher = new Dispatcher();
  11.             dispatcher.Work();
  12.         }
  13.     }
  14.  
  15.     class Dispatcher
  16.     {
  17.         private List<Train> _trains = new List<Train>();
  18.  
  19.         public void Work()
  20.         {
  21.             const string CommandCreateTrain = "1";
  22.             const string CommandExit = "2";
  23.  
  24.             bool isWork = true;
  25.  
  26.             while (isWork)
  27.             {
  28.                 ShowInfo();
  29.                 Console.WriteLine();
  30.  
  31.                 Console.Write($"{CommandCreateTrain} - создать поезд" +
  32.                     $"\n{CommandExit} - выйти" +
  33.                     $"\nВведите номер: ");
  34.  
  35.                 switch (Console.ReadLine())
  36.                 {
  37.                     case CommandCreateTrain:
  38.                         CreateTrain();
  39.                         break;
  40.  
  41.                     case CommandExit:
  42.                         isWork = false;
  43.                         break;
  44.  
  45.                     default:
  46.                         Console.WriteLine("Некорректный ввод");
  47.                         break;
  48.                 }
  49.  
  50.                 Console.Clear();
  51.             }
  52.         }
  53.  
  54.         private void CreateTrain()
  55.         {
  56.             Console.Write("Введите пункт отправления: ");
  57.             string startStation = Console.ReadLine();
  58.             Console.Write("Введите пункт прибытия: ");
  59.             string finishStation = Console.ReadLine();
  60.  
  61.             Direction direction = new Direction(startStation, finishStation);
  62.  
  63.             int minPassengers = 100;
  64.             int maxPassengers = 200;
  65.             int passengers = Utils.GetRandomNumber(minPassengers, maxPassengers);
  66.  
  67.             _trains.Add(new Train(direction, passengers));
  68.         }
  69.  
  70.         private void ShowInfo()
  71.         {
  72.             if (_trains.Count > 0)
  73.             {
  74.                 foreach (Train train in _trains)
  75.                 {
  76.                     train.ShowInfo();
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 Console.WriteLine("Поездов пока нет");
  82.             }
  83.         }
  84.     }
  85.  
  86.  
  87.     class Train
  88.     {
  89.         private Direction _direction;
  90.         private int _passengers;
  91.         private List<Car> _cars = new List<Car>();
  92.  
  93.         public Train(Direction direction, int passengers)
  94.         {
  95.             _direction = direction;
  96.             _passengers = passengers;
  97.             CreateCars();
  98.         }
  99.  
  100.         public void ShowInfo()
  101.         {
  102.             string separator = new string('-', 10);
  103.  
  104.             Console.Write("Поезд направления ");
  105.             _direction.ShowInfo();
  106.             Console.WriteLine("\nОбщее количество пассажиров: " + _passengers);
  107.  
  108.             for (int i = 0; i < _cars.Count; i++)
  109.             {
  110.                 Console.Write("\nВагон номер " + (i + 1) + " заполнен: ");
  111.                 _cars[i].ShowInfo();
  112.             }
  113.  
  114.             Console.WriteLine("\n" + separator);
  115.         }
  116.  
  117.         private void CreateCars()
  118.         {
  119.             Car car;
  120.             int availablePlaces = 0;
  121.             int availablePassengers = _passengers;
  122.  
  123.             while (availablePlaces <= _passengers)
  124.             {
  125.                 car = new Car();
  126.                 _cars.Add(car);
  127.                 availablePlaces += car.Capacity;
  128.  
  129.                 if (availablePassengers >= car.Capacity)
  130.                 {
  131.                     car.OccupyPlaces(car.Capacity);
  132.                     availablePassengers -= car.Capacity;
  133.                 }
  134.                 else
  135.                 {
  136.                     car.OccupyPlaces(availablePassengers);
  137.                 }
  138.             }
  139.         }
  140.     }
  141.  
  142.     class Direction
  143.     {
  144.         private string _startStation;
  145.         private string _finishStation;
  146.  
  147.         public Direction(string startStation, string finishStation)
  148.         {
  149.             _startStation = startStation;
  150.             _finishStation = finishStation;
  151.         }
  152.  
  153.         public void ShowInfo()
  154.         {
  155.             Console.Write(_startStation + " - " + _finishStation);
  156.         }
  157.     }
  158.  
  159.     class Car
  160.     {
  161.         private int _availablePlaces;
  162.         private int _occupiedPlaces;
  163.  
  164.         public Car()
  165.         {
  166.             Capacity = GetCarCapacity();
  167.             _availablePlaces = Capacity;
  168.             _occupiedPlaces = 0;
  169.         }
  170.  
  171.         public int Capacity { get; }
  172.  
  173.         public void ShowInfo()
  174.         {
  175.             Console.Write(_occupiedPlaces + " / " + Capacity);
  176.         }
  177.  
  178.         public void OccupyPlaces(int places)
  179.         {
  180.             if (_availablePlaces >= places)
  181.             {
  182.                 _availablePlaces -= places;
  183.                 _occupiedPlaces += places;
  184.             }
  185.         }
  186.  
  187.         private int GetCarCapacity()
  188.         {
  189.             int[] carsCapacuty = new int[] { 18, 30, 36, 38, 54 };
  190.             int minIndex = 0;
  191.             int maxIndex = carsCapacuty.Length;
  192.  
  193.             return carsCapacuty[Utils.GetRandomNumber(minIndex, maxIndex)];
  194.         }
  195.     }
  196.  
  197.     class Utils
  198.     {
  199.         private static Random s_random = new Random();
  200.  
  201.         public static int GetRandomNumber(int minValue, int maxValue)
  202.         {
  203.             return s_random.Next(minValue, maxValue);
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment