Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Ijunior
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Dispatcher dispatcher = new Dispatcher();
- dispatcher.Work();
- }
- }
- class Dispatcher
- {
- private List<Train> _trains = new List<Train>();
- public void Work()
- {
- const string CommandCreateTrain = "1";
- const string CommandExit = "2";
- bool isWork = true;
- while (isWork)
- {
- ShowInfo();
- Console.WriteLine();
- Console.Write($"{CommandCreateTrain} - создать поезд" +
- $"\n{CommandExit} - выйти" +
- $"\nВведите номер: ");
- switch (Console.ReadLine())
- {
- case CommandCreateTrain:
- CreateTrain();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Некорректный ввод");
- break;
- }
- Console.Clear();
- }
- }
- private void CreateTrain()
- {
- Console.Write("Введите пункт отправления: ");
- string startStation = Console.ReadLine();
- Console.Write("Введите пункт прибытия: ");
- string finishStation = Console.ReadLine();
- Direction direction = new Direction(startStation, finishStation);
- int minPassengers = 100;
- int maxPassengers = 200;
- int passengers = Utils.GetRandomNumber(minPassengers, maxPassengers);
- _trains.Add(new Train(direction, passengers));
- }
- private void ShowInfo()
- {
- if (_trains.Count > 0)
- {
- foreach (Train train in _trains)
- {
- train.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine("Поездов пока нет");
- }
- }
- }
- class Train
- {
- private Direction _direction;
- private int _passengers;
- private List<Car> _cars = new List<Car>();
- public Train(Direction direction, int passengers)
- {
- _direction = direction;
- _passengers = passengers;
- CreateCars();
- }
- public void ShowInfo()
- {
- string separator = new string('-', 10);
- Console.Write("Поезд направления ");
- _direction.ShowInfo();
- Console.WriteLine("\nОбщее количество пассажиров: " + _passengers);
- for (int i = 0; i < _cars.Count; i++)
- {
- Console.Write("\nВагон номер " + (i + 1) + " заполнен: ");
- _cars[i].ShowInfo();
- }
- Console.WriteLine("\n" + separator);
- }
- private void CreateCars()
- {
- Car car;
- int availablePlaces = 0;
- int availablePassengers = _passengers;
- while (availablePlaces <= _passengers)
- {
- car = new Car();
- _cars.Add(car);
- availablePlaces += car.Capacity;
- if (availablePassengers >= car.Capacity)
- {
- car.OccupyPlaces(car.Capacity);
- availablePassengers -= car.Capacity;
- }
- else
- {
- car.OccupyPlaces(availablePassengers);
- }
- }
- }
- }
- class Direction
- {
- private string _startStation;
- private string _finishStation;
- public Direction(string startStation, string finishStation)
- {
- _startStation = startStation;
- _finishStation = finishStation;
- }
- public void ShowInfo()
- {
- Console.Write(_startStation + " - " + _finishStation);
- }
- }
- class Car
- {
- private int _availablePlaces;
- private int _occupiedPlaces;
- public Car()
- {
- Capacity = GetCarCapacity();
- _availablePlaces = Capacity;
- _occupiedPlaces = 0;
- }
- public int Capacity { get; }
- public void ShowInfo()
- {
- Console.Write(_occupiedPlaces + " / " + Capacity);
- }
- public void OccupyPlaces(int places)
- {
- if (_availablePlaces >= places)
- {
- _availablePlaces -= places;
- _occupiedPlaces += places;
- }
- }
- private int GetCarCapacity()
- {
- int[] carsCapacuty = new int[] { 18, 30, 36, 38, 54 };
- int minIndex = 0;
- int maxIndex = carsCapacuty.Length;
- return carsCapacuty[Utils.GetRandomNumber(minIndex, maxIndex)];
- }
- }
- class Utils
- {
- private static Random s_random = new Random();
- public static int GetRandomNumber(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment