Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace OOPTask7TrainDispatcher
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- TrainDispatcher trainDispatcher = new TrainDispatcher();
- trainDispatcher.Work();
- }
- }
- public class TrainDispatcher
- {
- private List<Train> _trains = new List<Train>();
- public void Work()
- {
- const string CommandAdd = "add";
- const string CommandExit = "exit";
- bool isWorking = true;
- string userInput = "";
- while (isWorking)
- {
- ShowAllTrains();
- Console.WriteLine($"\n{CommandAdd} - зарегистрировать новый поезд");
- Console.WriteLine($"{CommandExit} - выход");
- Console.Write("Введите команду: ");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandAdd:
- AddTrain();
- break;
- case CommandExit:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Неизвестная команда");
- break;
- }
- Console.WriteLine("\nДля продолжения нажмите любую кнопку");
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void AddTrain()
- {
- Console.WriteLine("Введите пункт отбытия: ");
- string startPoint = Console.ReadLine();
- Console.WriteLine("Введите пункт назначения: ");
- string endPoint = Console.ReadLine();
- _trains.Add(new Train(startPoint, endPoint));
- Console.WriteLine("Поезд успешно зарегистрирован");
- }
- private void ShowAllTrains()
- {
- if (_trains.Count > 0)
- {
- Console.WriteLine($"Зарегистрировано {_trains.Count} поездов:");
- int number = 1;
- foreach (Train train in _trains)
- {
- Console.Write($"{number++} - ");
- train.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine("Пока что нет зарегистрированных поездов");
- }
- }
- }
- public class Train
- {
- private List<Wagon> _wagons = new List<Wagon>();
- private string _startPoint;
- private string _endPoint;
- private int _countPasangers;
- public Train(string startPoint, string endPoint)
- {
- _startPoint = startPoint;
- _endPoint = endPoint;
- Fill();
- }
- private void Fill()
- {
- const int MinimumWagonPlaces = 10;
- const int MaximumWagonPlaces = 60;
- const int MinimumTrainPassangers = 100;
- const int MaximumTrainPassangers = 1000;
- Random random = new Random();
- _countPasangers = random.Next(MinimumTrainPassangers, MaximumTrainPassangers);
- Console.WriteLine($"На поезд купили билеты {_countPasangers} пассажиров");
- Console.WriteLine("Их разместили по вагонам:");
- int countPassangers = _countPasangers;
- int wagonNumber = 1;
- while (countPassangers > 0)
- {
- int wagonPlaces = random.Next(MinimumWagonPlaces, MaximumWagonPlaces);
- Console.Write($"{wagonNumber++} - ");
- if (countPassangers >= wagonPlaces)
- {
- _wagons.Add(new Wagon(wagonPlaces, wagonPlaces));
- }
- else
- {
- _wagons.Add(new Wagon(wagonPlaces, countPassangers));
- }
- countPassangers -= wagonPlaces;
- }
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Поезд следует \"{_startPoint}\" - \"{_endPoint}\", в нем едут {_countPasangers} пассажиров в {_wagons.Count} вагонах");
- }
- }
- public class Wagon
- {
- private int _places;
- private int _passangers;
- public Wagon(int places, int passangers)
- {
- _places = places;
- _passangers = passangers;
- ShowInfo();
- }
- private void ShowInfo()
- {
- Console.WriteLine($"В вагоне из {_places} мест занято {_passangers}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement