Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Homework44
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Dispather dispather = new Dispather();
- dispather.isWork();
- }
- }
- class Dispather
- {
- private List<Train> _trains = new List<Train>();
- public void isWork()
- {
- const string CommandCreateTrain = "go";
- const string CommandExitMenu = "exit";
- const string CommandShowTrains = "show";
- bool isWork = true;
- while (isWork)
- {
- ShowShortTrainInfo();
- Console.WriteLine("\nВы находитесь в меню создания поездов");
- Console.WriteLine($"Чтобы создать поезд введите {CommandCreateTrain}");
- Console.WriteLine($"Чтобы показать список поездов введите {CommandShowTrains}");
- Console.WriteLine($"Чтобы выйти из меню введите {CommandExitMenu}");
- Console.Write("Выберите действие: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandCreateTrain:
- CreateTrain();
- break;
- case CommandShowTrains:
- ShowTrainInformation();
- break;
- case CommandExitMenu:
- isWork = ExitProgram();
- break;
- default:
- Console.WriteLine("Некорректный ввод, попробуйте еще раз.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- public void ShowTrainInformation()
- {
- if (_trains.Count == 0)
- {
- Console.WriteLine("Поездов пока нет.");
- return;
- }
- Console.WriteLine("Список поездов:");
- for (int i = 0; i < _trains.Count; i++)
- {
- Console.WriteLine($"Поезд {i + 1}: {_trains[i].Direction}, " +
- $"Пассажиров: {_trains[i].PassengerCount}, Вагонов: {_trains[i].Wagons.Count}");
- }
- }
- private bool ExitProgram()
- {
- Console.WriteLine("Вы вышли из программы!");
- return false;
- }
- private void CreateTrain()
- {
- string direction = CreateDirection();
- int passengerCount = SellTickets();
- if (passengerCount == -1)
- {
- Console.WriteLine("Продажа билетов отменена.");
- return;
- }
- Train train = new Train(direction, passengerCount);
- FormTrain(train);
- Console.WriteLine("\nИнформация о созданном поезде:");
- train.ShowInfo();
- _trains.Add(train);
- }
- private string CreateDirection()
- {
- string departure, arrival;
- TrainRouteGeneration locationGenerator = new TrainRouteGeneration();
- locationGenerator.GetDifferentLocations(out departure, out arrival);
- return $"{departure} - {arrival}";
- }
- private int SellTickets()
- {
- Random random = new Random();
- int passengerCount = random.Next(50, 201);
- Console.WriteLine($"Продано билетов: {passengerCount}");
- return passengerCount;
- }
- private void FormTrain(Train train)
- {
- int wagonCapacity = 24;
- int remainingPassengers = train.PassengerCount;
- while (remainingPassengers > 0)
- {
- Wagon wagon = new Wagon();
- int passengersForThisWagon = Math.Min(wagonCapacity, remainingPassengers);
- wagon.Fill(passengersForThisWagon);
- train.Wagons.Add(wagon);
- remainingPassengers -= passengersForThisWagon;
- }
- }
- private void ShowShortTrainInfo()
- {
- if (_trains.Count == 0)
- {
- Console.WriteLine("Поездов пока нет.");
- return;
- }
- Console.WriteLine("Существующие поезда:");
- for (int i = 0; i < _trains.Count; i++)
- {
- Console.WriteLine($"Поезд {i + 1}: {_trains[i].Direction}," +
- $" Пассажиров: {_trains[i].PassengerCount}, Вагонов: {_trains[i].Wagons.Count}");
- }
- }
- }
- class Train
- {
- public List<Wagon> Wagons = new List<Wagon>();
- public Train(string direction, int passengerCount)
- {
- Direction = direction;
- PassengerCount = passengerCount;
- }
- public string Direction { get; private set; }
- public int PassengerCount { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"Направление: {Direction}");
- Console.WriteLine($"Количество пассажиров: {PassengerCount}");
- Console.WriteLine($"Количество вагонов: {Wagons.Count}");
- Console.WriteLine("Информация по вагонам:");
- for (int i = 0; i < Wagons.Count; i++)
- {
- Console.WriteLine($" Вагон {i + 1}: Занято мест: {Wagons[i].OccupiedSeats}");
- }
- }
- }
- public class Wagon
- {
- public Wagon()
- {
- Capacity = 24;
- }
- public int Capacity { get; private set; }
- public int OccupiedSeats { get; private set; }
- public void Fill(int passengers)
- {
- if (passengers <= Capacity)
- {
- OccupiedSeats = passengers;
- }
- else
- {
- OccupiedSeats = Capacity;
- Console.WriteLine("Превышена вместимость вагона!");
- }
- }
- }
- class TrainRouteGeneration
- {
- private Random _random = new Random();
- private string[] _locations = new string[]
- {
- "Москва", "Санкт-Петербург", "Калуга", "Тверь", "Ярославль", "Краснодар", "Ростов", "Вологда",
- "Нижний Новгород", "Влаливосток", "Екатеринбург", "Самара", "Сочи",
- "Белгород", "Брянск", "Сергиев Посад", "Козельск","Иваново"
- };
- public string GetRandomLocation()
- {
- return _locations[_random.Next(0, _locations.Length)];
- }
- public void GetDifferentLocations(out string departure, out string arrival)
- {
- departure = GetRandomLocation();
- List<string> possibleArrivals = new List<string>();
- foreach (string city in _locations)
- {
- if (city != departure)
- {
- possibleArrivals.Add(city);
- }
- }
- if (possibleArrivals.Count == 0)
- {
- arrival = departure;
- return;
- }
- arrival = possibleArrivals[_random.Next(possibleArrivals.Count)];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement