Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Reflection;
- internal class Program
- {
- static void Main(string[] args)
- {
- Dispatcher dispatcher = new Dispatcher();
- dispatcher.Work();
- }
- }
- public class Dispatcher
- {
- private List<Train> _trains = new List<Train>();
- public void Work()
- {
- string commandCreateTrain = "1";
- string commandExit = "2";
- bool isWork = true;
- while (isWork)
- {
- Console.Clear();
- if (_trains.Count > 0)
- {
- ShowInfoTrain();
- }
- else
- {
- Console.WriteLine("Поездов пока нет.");
- }
- Console.WriteLine(new string('-', 30));
- Console.WriteLine($"{commandCreateTrain} - Создать поезд.\n{commandExit} - Завершить работу.");
- string userInput = Console.ReadLine();
- if (userInput == commandCreateTrain)
- {
- CreateTrain();
- }
- else if (userInput == commandExit)
- {
- isWork = false;
- }
- else
- {
- Console.WriteLine("Ошибка ввода.");
- Console.ReadKey();
- }
- }
- Console.WriteLine("Программа завершена.");
- }
- private void CreateTrain()
- {
- Console.WriteLine("Назначим направление поезда!");
- Direction direction = FormDirection();
- Thread.Sleep(1000);
- Console.Write($"Продаем билеты на направление {direction}");
- int passengers = GeneratePassengers();
- WaitLittle();
- Console.WriteLine($"Продали {passengers} билетов.");
- Console.Write("Собирам поезд согластно проданым билетам");
- WaitLittle();
- int capacityWagon = CalculateCapacityWagon(passengers);
- int numberWagons = CountNumberWagons(capacityWagon, passengers);
- Wagon[] wagons = new Wagon[numberWagons];
- for (int i = 0; i < wagons.Length; i++)
- {
- wagons[i] = new Wagon(capacityWagon);
- }
- Train train = (new Train(passengers, direction, wagons));
- _trains.Add(train);
- Console.WriteLine("Поезд готов!");
- Console.ReadKey();
- }
- private void ShowInfoTrain()
- {
- foreach (Train train in _trains)
- {
- train.ShowInfo();
- }
- }
- private Direction FormDirection()
- {
- Console.Write("Город отправления: ");
- string from = Console.ReadLine();
- Console.Write("Город прибытия: ");
- string to = Console.ReadLine();
- return new Direction(from, to);
- }
- private int GeneratePassengers()
- {
- int minRandom = 10;
- int maxRandom = 300;
- Random random = new Random();
- return random.Next(minRandom, maxRandom + 1); ;
- }
- private int CalculateCapacityWagon(int passengers)
- {
- int maxPassengersForSmallTrain = 200;
- int smallWagon = 20;
- int bigWagon = 30;
- if (passengers > maxPassengersForSmallTrain)
- {
- return bigWagon;
- }
- else
- {
- return smallWagon;
- }
- }
- private int CountNumberWagons(int capacityWagon, int passengers)
- {
- return (int)Math.Ceiling((double)passengers / capacityWagon);
- }
- private void WaitLittle()
- {
- int dotCount = 5;
- int delayMilliseconds = 700;
- for (int i = 0; i < dotCount; i++)
- {
- Console.Write(".");
- Thread.Sleep(delayMilliseconds);
- }
- Console.WriteLine();
- }
- }
- public class Train
- {
- private List<Wagon> _wagons = new List<Wagon>();
- private Direction _direction;
- private int _passengers;
- public Train(int passengers, Direction direction, Wagon[] wagon)
- {
- _passengers = passengers;
- _direction = direction;
- _wagons.AddRange(wagon);
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Поезд {_direction}.\n{_wagons[0]}.\nВсего вагонов: {_wagons.Count}.\nПассажиров: {_passengers}.");
- }
- }
- public class Direction
- {
- private string _from;
- private string _to;
- public Direction(string from, string to)
- {
- _from = from;
- _to = to;
- }
- public override string ToString()
- {
- return $"{_from} - {_to}";
- }
- }
- public class Wagon
- {
- public Wagon(int capacity)
- {
- Capacity = capacity;
- }
- public int Capacity { get; private set; }
- public override string ToString()
- {
- return $"Вагон рассчитан {Capacity} пассажиров";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment