Advertisement
Rodunskiy

Untitled

Aug 10th, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Depo depo = new Depo();
  9.  
  10.         depo.Work();
  11.     }
  12. }
  13.  
  14. class Depo
  15. {
  16.     private static Random _random = new Random();
  17.  
  18.     private List<Train> _trains;
  19.     private int _numberTrain = 1000;
  20.  
  21.     public Depo()
  22.     {
  23.         _trains = new List<Train>();
  24.     }
  25.  
  26.     public void Work()
  27.     {
  28.         string goCommand = "Go";
  29.         string exitCommand = "Exit";
  30.         bool isWorking = true;
  31.  
  32.         while (isWorking)
  33.         {
  34.             Console.WriteLine($"Введите {goCommand} чтобы начать программу или {exitCommand} для выхода.");
  35.             string userInput = Console.ReadLine();
  36.  
  37.             if (userInput == "Exit")
  38.             {
  39.                 isWorking = false;
  40.             }
  41.             else if(userInput == "Go")
  42.             {
  43.                 ShowInfo();
  44.                 CreateNewTrain();
  45.             }
  46.         }
  47.     }
  48.  
  49.     private int CreateNewTickets()
  50.     {
  51.         int minIndexRandom = 5;
  52.         int maxIndexRandom = 16;
  53.  
  54.         return _random.Next(minIndexRandom, maxIndexRandom);
  55.     }
  56.  
  57.     private Direction CreateNewDirection()
  58.     {
  59.         string source;
  60.         string destination;
  61.  
  62.         Console.WriteLine("Создание нового направления.\nВведите пункт отправления и пункт отбытия.");
  63.         source = Console.ReadLine();
  64.         destination = Console.ReadLine();
  65.  
  66.         while (String.IsNullOrWhiteSpace(source) || String.IsNullOrWhiteSpace(destination) || source.ToLower() == destination.ToLower())
  67.         {
  68.             Console.WriteLine("Ошибка. Введены не коректные данные");
  69.         }
  70.  
  71.         Console.WriteLine($"Направление {source}-{destination} создано.");
  72.  
  73.         return new Direction(source, destination);
  74.  
  75.     }
  76.  
  77.     private void CreateNewTrain()
  78.     {
  79.         Direction direction = CreateNewDirection();
  80.  
  81.         _trains.Add(new Train(_numberTrain, direction, CreateNewTickets()));
  82.  
  83.         _numberTrain++;
  84.     }
  85.  
  86.     private void ShowInfo()
  87.     {
  88.         foreach (var train in _trains)
  89.         {
  90.             train.ShowStatsTrain();
  91.         }
  92.     }
  93. }
  94.  
  95. class Train
  96. {
  97.     private List<Van> _vans;
  98.     private List<Van> _vansInTrain;
  99.     private Direction _direction;
  100.  
  101.     public Train(int number, Direction direction, int numberTickets)
  102.     {
  103.         Number = number;
  104.         _direction = direction;
  105.         _vans = new List<Van>() { new SmallVan(), new MediumVan(),new LargeVan() };
  106.         _vansInTrain = new List<Van>();
  107.  
  108.         AddVans(numberTickets);
  109.     }
  110.  
  111.     public int Number { get; private set; }
  112.  
  113.     public void AddVans(int numberTickets)
  114.     {
  115.         Console.WriteLine($"Кол-во купленных билетов:{numberTickets}");
  116.  
  117.         string vanName;
  118.  
  119.         Console.WriteLine("Какой вагон вы хотите добавить?");
  120.  
  121.         foreach (Van van in _vans)
  122.         {
  123.             van.ShowStatsVan();
  124.         }
  125.  
  126.         vanName = Console.ReadLine();
  127.  
  128.         foreach (Van van in _vans)
  129.         {
  130.             while (van.Name == vanName && numberTickets > 0)
  131.             {
  132.                 _vansInTrain.Add(van);
  133.                 numberTickets -= van.NumberSeats;
  134.             }
  135.         }
  136.     }
  137.  
  138.     public void ShowStatsTrain()
  139.     {
  140.         Console.WriteLine($"Направление {_direction.Source}-{_direction.Destination} |Номер поезда {Number}| Кол-во вагонов {_vansInTrain.Count}");
  141.     }
  142. }
  143.  
  144. class Van
  145. {
  146.     public Van(string name, int numberSeats)
  147.     {
  148.         Name = name;
  149.         NumberSeats = numberSeats;
  150.     }
  151.  
  152.     public string Name { get; private set; }
  153.     public int NumberSeats { get; private set; }
  154.  
  155.     public void ShowStatsVan()
  156.     {
  157.         Console.WriteLine($"{Name}|Кол-во мест:{NumberSeats}");
  158.     }
  159. }
  160.  
  161. class SmallVan : Van
  162. {
  163.     public SmallVan() : base ("Small", 5) { }
  164. }
  165.  
  166. class MediumVan : Van
  167. {
  168.     public MediumVan() : base("Medium", 10) { }
  169. }
  170.  
  171. class LargeVan : Van
  172. {
  173.     public LargeVan() : base("Large", 15) { }
  174. }
  175.  
  176. class Direction
  177. {
  178.     public Direction(string source, string destination)
  179.     {
  180.         Source = source;
  181.         Destination = destination;
  182.     }
  183.  
  184.     public string Source { get; private set; }
  185.     public string Destination { get; private set; }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement