Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Classes
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- TrainDispatcher dispatcher = new TrainDispatcher();
- dispatcher.WorkMenu();
- }
- }
- class TrainDispatcher
- {
- private List<Train> _trains = new List<Train>();
- private Random _random = new Random();
- public void WorkMenu()
- {
- Console.CursorVisible = false;
- Console.WriteLine($"To create a train press anything...\nTo exit press 0.");
- while (Console.ReadKey().Key != ConsoleKey.D0)
- {
- Console.Clear();
- CreateTrain();
- Console.WriteLine($"\nTo create a train press anything...\nTo exit press 0.\n");
- ShowTrains();
- }
- }
- private int SellTickets()
- {
- int maxPassengers = 100;
- int minPassengers = 1;
- int numberPassengers = _random.Next(minPassengers, maxPassengers + 1);
- Console.WriteLine($"{numberPassengers} tickets sold\n");
- return numberPassengers;
- }
- private List<Car> AttachCars()
- {
- List<Car> cars = new List<Car>();
- int numberPassengers = SellTickets();
- int index = 1;
- int capacityCar = 10;
- while (numberPassengers > 0)
- {
- int maxPassengerInCar = capacityCar;
- int minPassengerInCar = 5;
- int passengerInCar = _random.Next(minPassengerInCar, maxPassengerInCar + 1);
- passengerInCar = numberPassengers - passengerInCar < 0 ? numberPassengers : passengerInCar;
- numberPassengers -= passengerInCar;
- Console.WriteLine($"in {index} car added {passengerInCar} passengers");
- index++;
- cars.Add(new Car(passengerInCar, capacityCar));
- }
- Console.WriteLine($"\n{cars.Count} attached cars");
- return cars;
- }
- private void CreateTrain()
- {
- Direction _direction = new Direction();
- string direction = _direction.CreateDirection();
- Console.WriteLine($"{direction} train created\n");
- _trains.Add(new Train(direction, AttachCars()));
- }
- private void ShowTrains()
- {
- string train = null;
- string trainSign = ">>";
- int trainsShownInDispatcher = 10;
- for (int i = 0; i < trainsShownInDispatcher; i++)
- {
- int index = _trains.Count - 1 - i;
- i = _trains.Count - 1 - (i + 1) >= 0 ? i : trainsShownInDispatcher;
- List<Car> cars = new List<Car>(index);
- for (int k = 0; k < _trains[index].Count; k++)
- {
- cars.Add(_trains[index].GetCarElement(k));
- }
- for (int j = _trains[index].Count - 1; j >= 0; j--)
- {
- train += cars[j].Content + ".";
- }
- Console.WriteLine(_trains[index].Direction + " " + train + trainSign);
- train = null;
- }
- }
- }
- class Train
- {
- private List<Car> _cars = new List<Car>();
- public Train(string direction, List<Car> cars)
- {
- Direction = direction;
- _cars = cars;
- }
- public string Direction { get; private set; }
- public Car GetCarElement(int index)
- {
- return _cars.ElementAt(index);
- }
- public int Count => _cars.Count;
- }
- class Car
- {
- public Car(int content, int capacity)
- {
- Capacity = capacity;
- Content = content;
- }
- public int Capacity { get; private set; }
- public int Content { get; private set; }
- }
- class Direction
- {
- public Direction()
- {
- PointStart = CreatePointStart();
- PointFinish = CreatePointFinish();
- }
- public char PointStart { get; private set; }
- public char PointFinish { get; private set; }
- private char[] _points = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
- private Random _random = new Random();
- public char CreatePointStart()
- {
- PointStart = _points[_random.Next(_points.Length)];
- return PointStart;
- }
- public char CreatePointFinish()
- {
- bool isEqual = true;
- while (isEqual)
- {
- PointFinish = _points[_random.Next(_points.Length)];
- isEqual = PointStart == PointFinish;
- }
- return PointFinish;
- }
- public string CreateDirection()
- {
- string direction = PointStart + " - " + PointFinish;
- return direction;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement