Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- Dispatcher dispatcher = new Dispatcher();
- dispatcher.Work();
- }
- }
- class Dispatcher
- {
- private Random _random = new Random();
- private List<Train> _trains = new List<Train>();
- public void Work()
- {
- bool isWork = true;
- while (isWork)
- {
- const int ExitCommand = 9;
- int countAllPassengersForDirections;
- string directionName;
- string userInput;
- bool isCorrectInput;
- ShowDirectionInfo();
- directionName = CreateDirectionName();
- ShowDirectionInfo(directionName);
- countAllPassengersForDirections = GetAllPassengersForDirection();
- ShowDirectionInfo(directionName, countAllPassengersForDirections);
- CreateWagonsForFlight(countAllPassengersForDirections, directionName);
- ShowDirectionInfo(directionName, countAllPassengersForDirections);
- Console.WriteLine("Сейчас поезд выглядит так:");
- foreach (Train train in _trains)
- {
- train.ShowInfo();
- }
- Console.WriteLine($"\nВы ходите выйти из программы? Да - {ExitCommand}.");
- userInput = Console.ReadLine();
- isCorrectInput = int.TryParse(userInput, out int correctInput);
- if (isCorrectInput && correctInput == ExitCommand)
- {
- isWork = false;
- Console.WriteLine("Завершение программы...");
- Console.WriteLine("Для продолжения нажмите любую клавишу...");
- }
- else
- {
- Console.Clear();
- }
- }
- }
- private void CreateWagonsForFlight(int countAllPassengersForDirections, string directionName)
- {
- int totalCapacityWagons = 0;
- bool isSeatsForAllPassengers = false;
- List<Wagon> wagons = new List<Wagon>();
- Console.WriteLine("Формируем количество вагонов на рейс.");
- while (!isSeatsForAllPassengers)
- {
- Wagon userSelectedTypeWagon = GetTypeWagon();
- int countPassengersNotHaveSpace;
- Console.WriteLine($"Добавляем вагон {userSelectedTypeWagon.NameType}.");
- Console.ReadKey();
- totalCapacityWagons += userSelectedTypeWagon.MaximumCapacity;
- countPassengersNotHaveSpace = countAllPassengersForDirections - totalCapacityWagons;
- wagons.Add(new Wagon(userSelectedTypeWagon.NameType, userSelectedTypeWagon.MaximumCapacity));
- if (countPassengersNotHaveSpace <= 0)
- {
- isSeatsForAllPassengers = true;
- _trains.Add(new Train(directionName, wagons));
- }
- else
- {
- ShowDirectionInfo(directionName, countAllPassengersForDirections);
- foreach (Wagon wagon in wagons)
- {
- wagon.ShowInfo();
- }
- Console.WriteLine($"Нужно разместить еще {countPassengersNotHaveSpace} пассажир(ов).");
- }
- }
- }
- private string CreateDirectionName()
- {
- string directionName;
- Console.Write("Введите название направления: ");
- directionName = Console.ReadLine();
- if (directionName == "")
- directionName = "No name";
- return directionName;
- }
- private int GetCountPassengers()
- {
- int minimumCountPassenger = 20;
- int maximumCountPassenger = minimumCountPassenger * 7;
- int tryCountPassenger;
- tryCountPassenger = _random.Next(minimumCountPassenger, maximumCountPassenger);
- Console.WriteLine($"Общее количество пассажиров - {tryCountPassenger}.");
- return tryCountPassenger;
- }
- private int GetAllPassengersForDirection()
- {
- int countAllPassengersForDirection;
- Console.Write($"Введите любой символ для обновления данных о количестве пассажиров:");
- Console.ReadKey();
- countAllPassengersForDirection = GetCountPassengers();
- return countAllPassengersForDirection;
- }
- private void ShowDirectionInfo(string directionName = "(не задано)", int totalCountPassenger = 0)
- {
- Console.Clear();
- Console.WriteLine("\t\tКонфигуратор пасcажирских поездов. beta 0.000.0001");
- Console.WriteLine($"Направление движения - \"{directionName}\", количество пассажиров - {totalCountPassenger}.\n");
- }
- private Wagon GetTypeWagon()
- {
- List<Wagon> _typeWagons = new List<Wagon>();
- Wagon typeWagon = null;
- string userInput;
- bool isCorrectUserSelectedTypeWagon = false;
- int userSelectedTypeWagon = 1;
- Console.WriteLine("Выберите поезд. Показываю все типы вагонов:");
- Fill();
- ShowAllTypesWagons();
- Console.Write("\n\nКакой вагон добавляем? (введите порядковый номер): ");
- while (!isCorrectUserSelectedTypeWagon)
- {
- userInput = Console.ReadLine();
- isCorrectUserSelectedTypeWagon = int.TryParse(userInput, out userSelectedTypeWagon);
- if (isCorrectUserSelectedTypeWagon)
- {
- if (userSelectedTypeWagon > _typeWagons.Count || userSelectedTypeWagon <= 0)
- {
- Console.Write($"\aПорядкового номера вагона {userInput} не найдено. Попробуйте еще раз!");
- isCorrectUserSelectedTypeWagon = false;
- }
- }
- else
- {
- Console.Write("\aНужно вводить число! Попробуйте еще раз: ");
- }
- }
- for (int i = 0; i < _typeWagons.Count; i++)
- {
- if ((userSelectedTypeWagon - 1) == i)
- typeWagon = new Wagon(_typeWagons[i].NameType, _typeWagons[i].MaximumCapacity);
- }
- return typeWagon;
- void ShowAllTypesWagons()
- {
- for (int i = 0; i < _typeWagons.Count; i++)
- {
- Console.Write($"\n#{i + 1} - ");
- _typeWagons[i].ShowInfo();
- }
- }
- void Fill()
- {
- _typeWagons.Add(new Wagon("Сидячий", 67));
- _typeWagons.Add(new Wagon("Плацкартный", 54));
- _typeWagons.Add(new Wagon("Международный", 40));
- _typeWagons.Add(new Wagon("Купейный", 36));
- _typeWagons.Add(new Wagon("Люкс", 18));
- _typeWagons.Add(new Wagon("Мягкий", 8));
- }
- }
- }
- class Train
- {
- private List<Wagon> _wagons = new List<Wagon>();
- public Train(string directionName, List<Wagon> wagons)
- {
- DirectionName = directionName;
- _wagons = wagons;
- }
- public string DirectionName { get; private set; }
- public void ShowInfo()
- {
- foreach (Wagon wagon in _wagons)
- {
- Console.Write("u");
- wagon.ShowInfo();
- }
- }
- public void AddNewWagon(Wagon wagon)
- {
- _wagons.Add(new Wagon(wagon.NameType, wagon.MaximumCapacity));
- }
- }
- class Wagon
- {
- public Wagon(string nameType, int maximumCapacity)
- {
- NameType = nameType;
- MaximumCapacity = maximumCapacity;
- }
- public string NameType { get; private set; }
- public int MaximumCapacity { get; private set; }
- public void ShowInfo()
- {
- Console.Write($" [ {NameType} - {MaximumCapacity} ] ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement