Advertisement
holllowknight

ДЗ Поезда 3

Jul 22nd, 2020 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace TrainPlanner
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const int CommandNewTrainPlan = 1;
  11.             const int CommandExit = 2;
  12.  
  13.             TrainPlan planner = new TrainPlan();
  14.             bool isRunning = true;
  15.  
  16.             while (isRunning)
  17.             {
  18.                 Console.Clear();
  19.                 planner.ShowCurrentRoute();
  20.                 Console.WriteLine($"{CommandNewTrainPlan} - Задать новый рейс\n{CommandExit} - Выход");
  21.                 int userInput = UserUtils.GetNumberFromUser();
  22.  
  23.                 switch (userInput)
  24.                 {
  25.                     case CommandNewTrainPlan:
  26.                         planner.CreateTrainPlan();
  27.                         break;
  28.  
  29.                     case CommandExit:
  30.                         isRunning = false;
  31.                         break;
  32.                 }
  33.             }
  34.         }
  35.  
  36.         class UserUtils
  37.         {
  38.             public static int GetNumberFromUser()
  39.             {
  40.                 string userInput;
  41.                 int userNumber;
  42.                 bool isNumber = false;
  43.  
  44.                 do
  45.                 {
  46.                     userInput = Console.ReadLine();
  47.                     isNumber = int.TryParse(userInput, out userNumber);
  48.                 }
  49.                 while (isNumber == false);
  50.  
  51.                 return userNumber;
  52.             }
  53.         }
  54.  
  55.         class Car
  56.         {
  57.             public Car(int carCapacity)
  58.             {
  59.                 Capacity = carCapacity;
  60.             }
  61.  
  62.             public int Capacity { get; private set; }
  63.         }
  64.  
  65.         class Train
  66.         {
  67.             private string _route;
  68.             private List<Car> _cars;
  69.             private bool _isEnRoute;
  70.             private int _occupiedPlaces;
  71.  
  72.             public Train()
  73.             {
  74.                 _cars = new List<Car>();
  75.             }
  76.  
  77.             public int Capacity { get; private set; }
  78.  
  79.             public void SetRoute(string routeTitle)
  80.             {
  81.                 _route = routeTitle;
  82.             }
  83.  
  84.             public void InsertCar(Car car)
  85.             {
  86.                 Capacity += car.Capacity;
  87.                 _cars.Add(car);
  88.             }
  89.  
  90.             public void Run()
  91.             {
  92.                 _isEnRoute = true;
  93.                 Console.WriteLine("Поезд отправлен");
  94.             }
  95.  
  96.             public void RegisterPassengers(int passengersNum)
  97.             {
  98.                 _occupiedPlaces = passengersNum;
  99.             }
  100.  
  101.             public void ShowInfo()
  102.             {
  103.                 if (_isEnRoute)
  104.                 {
  105.                     Console.WriteLine($"Поезд <{_route}> - всего мест:{Capacity} - из них занято: {_occupiedPlaces}");
  106.                 }
  107.                 else
  108.                 {
  109.                     Console.WriteLine("Состав не сформирован");
  110.                 }
  111.             }
  112.         }
  113.  
  114.         class TrainPlan
  115.         {
  116.             private Train _processedTrain;
  117.             private int _soldTickets;
  118.             private int _maxCarCapacity = 50;
  119.             private int _minCarCapacity = 30;
  120.             private int _maxTickets = 1000;
  121.             private int _minTickets = 10;
  122.  
  123.             public void CreateRoute()
  124.             {
  125.                 _processedTrain = new Train();
  126.                 Console.WriteLine("Введите маршрут");
  127.                 _processedTrain.SetRoute(Console.ReadLine());
  128.             }
  129.  
  130.             public void SellTickets()
  131.             {
  132.                 Random rand = new Random();
  133.                 _soldTickets = rand.Next(_minTickets, _maxTickets);
  134.                 Console.WriteLine($"Продано {_soldTickets} билетов");
  135.             }
  136.  
  137.             public void FormTrain()
  138.             {
  139.                 Random rand = new Random();
  140.                 int carCapacity;
  141.  
  142.                 while (_processedTrain.Capacity < _soldTickets)
  143.                 {
  144.                     carCapacity = rand.Next(_minCarCapacity, _maxCarCapacity);
  145.                     _processedTrain.InsertCar(new Car(carCapacity));
  146.                 }
  147.  
  148.                 _processedTrain.RegisterPassengers(_soldTickets);
  149.             }
  150.  
  151.             public Train RunTrain()
  152.             {
  153.                 _processedTrain.Run();
  154.                 _processedTrain.ShowInfo();
  155.                 return _processedTrain;
  156.             }
  157.  
  158.             public void CreateTrainPlan()
  159.             {
  160.                 CreateRoute();
  161.                 SellTickets();
  162.                 FormTrain();
  163.                 RunTrain();
  164.                 Console.ReadKey();
  165.             }
  166.  
  167.             public void ShowCurrentRoute()
  168.             {
  169.                 if(_processedTrain == null)
  170.                     Console.WriteLine("Рейсы отсутствуют");
  171.                 else
  172.                    _processedTrain.ShowInfo();
  173.             }
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement