Advertisement
Rodunskiy

Untitled

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