Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Dipatcher dipatcher = new Dipatcher();
- dipatcher.Work();
- }
- }
- class Dipatcher
- {
- private List<Train> _trains = new List<Train>();
- public void Work()
- {
- const string CommandCreateTrain = "1";
- const string CommandExit = "2";
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine($"Меню:\n" +
- $"{CommandCreateTrain}. Создать поезд\n" +
- $"{CommandExit}. Выход");
- Console.WriteLine("\nТекущие поезда:");
- DisplayTrains();
- Console.Write("\nВаш выбор: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandCreateTrain:
- CreateTrain();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Нет такой команды");
- break;
- }
- }
- Console.WriteLine("Работа завершена!");
- }
- private string GetArrivalPoint(string departurePoint)
- {
- string arrivalPoint = string.Empty;
- do
- {
- Console.Write("Куда: ");
- arrivalPoint = Console.ReadLine();
- if (arrivalPoint == departurePoint)
- {
- Console.WriteLine("Введенный пункт назначения совпадает с пунктом отправления");
- }
- } while (arrivalPoint == departurePoint || string.IsNullOrWhiteSpace(arrivalPoint));
- return arrivalPoint;
- }
- private void DisplayTrains()
- {
- if (_trains.Count == 0)
- {
- Console.WriteLine("Нет созданных поездов");
- return;
- }
- for (int i = 0; i < _trains.Count; i++)
- {
- Console.WriteLine($"{i + 1}. {_trains[i].GetShortInfo()}");
- }
- }
- private void CreateTrain()
- {
- int minNumber = 50;
- int maxNumber = 200;
- RailcarFactory railcarFactory = new RailcarFactory();
- List<Railcar> railcars;
- Console.WriteLine("\nШаг 1. Создать направление");
- Console.WriteLine("Введите направление (например, Бийск - Барнаул): ");
- Console.Write("Откуда: ");
- string departurePoint = Console.ReadLine();
- string arrivalPoint = GetArrivalPoint(departurePoint);
- Route route = new Route(departurePoint, arrivalPoint);
- Console.WriteLine("\nШаг 2. Продать билеты");
- int passengers = UserUtils.GenerateRandomNumber(minNumber, maxNumber);
- Console.WriteLine($"Продано билетов: {passengers}\n");
- Console.WriteLine("Шаг 3. Сформировать поезд");
- railcars = railcarFactory.CreateRailcarsForPassengers(passengers);
- Train train = new Train(route, railcars, passengers);
- Console.WriteLine("\nШаг 4. Информация о поезде");
- Console.WriteLine(train.GetFullInfo());
- _trains.Add(train);
- Console.WriteLine("\nНажмите Enter, чтобы вернуться в меню");
- Console.ReadLine();
- }
- }
- class Train
- {
- private List<Railcar> _railcars;
- public Train(Route route, List<Railcar> railcars, int totalPassengers)
- {
- _railcars = new List<Railcar>(railcars);
- TotalPassengers = totalPassengers;
- Route = route;
- Console.WriteLine("Поезд сформирован!");
- }
- public Route Route { get; private set; }
- public int TotalPassengers { get; private set; }
- public string GetShortInfo()
- {
- return $"{Route.FullInfo} - {_railcars.Count} вагонов, {TotalPassengers} пассажиров";
- }
- public string GetFullInfo()
- {
- string info = $"Поезд по маршруту: {Route.FullInfo}\n" +
- $"Общее число пассажиров: {TotalPassengers}\n" +
- $"Количество вагонов: {_railcars.Count}";
- for (int i = 0; i < _railcars.Count; i++)
- {
- info += $"\n Вагон {i + 1}: Вместимость {_railcars[i].Capacity}, Пассажиров {_railcars[i].Passengers}";
- }
- return info;
- }
- }
- class RailcarFactory
- {
- private const int MinRailcarCapacity = 20;
- private const int MaxRailcarCapacity = 50;
- public List<Railcar> CreateRailcarsForPassengers(int passengers)
- {
- List<Railcar> railcars = new List<Railcar>();
- while (passengers > 0)
- {
- int capacity = UserUtils.GenerateRandomNumber(MinRailcarCapacity, MaxRailcarCapacity);
- int passengersInRailcar = Math.Min(capacity, passengers);
- railcars.Add(new Railcar(capacity, passengersInRailcar));
- passengers -= passengersInRailcar;
- }
- return railcars;
- }
- }
- class Railcar
- {
- public Railcar(int capacity, int passengers)
- {
- Passengers = passengers;
- Capacity = capacity;
- }
- public int Capacity { get; private set; }
- public int Passengers { get; private set; }
- }
- class Route
- {
- public Route(string departurePoint, string arrivalPoint)
- {
- DeparturePoint = departurePoint;
- ArrivalPoint = arrivalPoint;
- }
- public string DeparturePoint { get; private set; }
- public string ArrivalPoint { get; private set; }
- public string FullInfo => DeparturePoint + " - " + ArrivalPoint;
- }
- class UserUtils
- {
- private static Random _random = new Random();
- public static int GenerateRandomNumber(int minValue, int maxValue)
- {
- return _random.Next(minValue, maxValue + 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement