-
-
Save Avet1k/1e880f5f6993063ef9ea46fc8acdde03 to your computer and use it in GitHub Desktop.
Passenger train configurator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Junior; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
TrainConfigurator trainConfigurator = new TrainConfigurator(); | |
trainConfigurator.Work(); | |
} | |
} | |
interface IMovable | |
{ | |
public void Move(); | |
} | |
class Direction | |
{ | |
public Direction(string beginning, string end) | |
{ | |
Beginning = beginning; | |
End = end; | |
} | |
public string Beginning { get; } | |
public string End { get; } | |
public string GetInfo() | |
{ | |
return $"{Beginning} - {End}"; | |
} | |
} | |
class TrainConfigurator | |
{ | |
private int _money; | |
private Direction? _currentDirection; | |
private Train? _train; | |
private Random _random; | |
public TrainConfigurator() | |
{ | |
_random = new Random(); | |
} | |
public int TicketPrice { get; private set; } | |
public void Work() | |
{ | |
const char QuitCommand = '1'; | |
int minPrice = 25; | |
int maxPrice = 250; | |
int minPassengersCount = 200; | |
int maxPassengersCount = 801; | |
bool isWorking = true; | |
while (isWorking) | |
{ | |
int passengersCount; | |
TicketPrice = _random.Next(minPrice, maxPrice); | |
SetDirection(); | |
passengersCount = _random.Next(minPassengersCount, maxPassengersCount); | |
SellTickets(passengersCount); | |
AddCoaches(passengersCount); | |
SendOnWay(); | |
ShowInfo(); | |
Console.SetCursorPosition(0, 4); | |
Console.WriteLine("Для создания нового направления нажмите любую кнопку\n\n" + | |
$"Для выхода из программы нажмите \"{QuitCommand}\""); | |
if (Console.ReadKey(true).KeyChar == QuitCommand) | |
isWorking = false; | |
} | |
} | |
public void ShowInfo() | |
{ | |
Console.Clear(); | |
Console.Write($"Цена билета: {TicketPrice}. Денег в кассе: {_money}."); | |
if (_currentDirection is null) | |
return; | |
Console.Write($" Направление {_currentDirection.GetInfo()}."); | |
if (_train is null) | |
return; | |
_train.DrawCoaches(); | |
} | |
private void SetDirection() | |
{ | |
string firstCity; | |
string lastCity; | |
ShowInfo(); | |
Console.SetCursorPosition(0, 4); | |
Console.WriteLine("Создание направления\n"); | |
Console.Write("Введите город отправления: "); | |
firstCity = HandleCityInput(); | |
Console.Write("Введите город прибытия: "); | |
lastCity = HandleCityInput(firstCity); | |
_currentDirection = new Direction(firstCity, lastCity); | |
Console.WriteLine($"Направление {_currentDirection.GetInfo()} создано\n\n" + | |
"Нажмите любую кнопку для перехода к следующему шагу..."); | |
} | |
private string HandleCityInput(string? firstCity = null) | |
{ | |
string? userInput = string.Empty; | |
while (userInput == string.Empty || userInput == firstCity) | |
{ | |
userInput = Console.ReadLine().Trim(); | |
if (userInput == string.Empty) | |
Console.Write("Название города не может быть пустым. Введите город: "); | |
else if (userInput == firstCity) | |
Console.Write("Название городов отбытия и прибытия не должны совпадать. Введите другой город: "); | |
} | |
return userInput; | |
} | |
private void SellTickets(int passengersCount) | |
{ | |
int salesIncome = passengersCount * TicketPrice; | |
ShowInfo(); | |
Console.SetCursorPosition(0, 4); | |
Console.WriteLine($"Продажи билетов ожидают {passengersCount} людей.\n\n" + | |
"Нажмите любую кнопку для продажи билетов..."); | |
Console.ReadKey(); | |
_money += salesIncome; | |
Console.WriteLine($"\nПо цене {TicketPrice} продано билетов: {passengersCount}.\n" + | |
$"Касса увеличилась на {salesIncome}.\n\n" + | |
"Нажмите любую кнопку для перехода к следующему шагу..."); | |
Console.ReadKey(); | |
} | |
private void AddCoaches(int passengersCount) | |
{ | |
int minCoachCapacity = 20; | |
int maxCoachCapacity = 101; | |
ShowInfo(); | |
_train = new Train(); | |
while (_train.Capacity < passengersCount) | |
_train.AttachCoach(new Coach(_random.Next(minCoachCapacity, maxCoachCapacity))); | |
Console.SetCursorPosition(0, 4); | |
Console.WriteLine($"Для посадки {passengersCount} пассажиров в состав было присоединено " + | |
$"вагонов: {_train.GetCoachesCount()}.\n\nНажмите на любую кнопку для продолжения..."); | |
Console.ReadKey(); | |
} | |
private void SendOnWay() | |
{ | |
ShowInfo(); | |
Console.SetCursorPosition(0, 4); | |
if (_train is null) | |
return; | |
Console.WriteLine("Пассажиры заняли свои места, провожающие вышли из вагонов.\n" + | |
"Нажмите любую кнопку для отправления поезда в путь...\n"); | |
Console.ReadKey(); | |
_train.Move(); | |
_train = null; | |
_currentDirection = null; | |
Console.ReadKey(); | |
} | |
} | |
class Train : IMovable | |
{ | |
private List<Coach> _coaches; | |
public Train() | |
{ | |
_coaches = new List<Coach>(); | |
} | |
public int Capacity { get; private set; } | |
public void Move() | |
{ | |
Console.WriteLine("Поезд отправляется в путь!"); | |
} | |
public int GetCoachesCount() | |
{ | |
return _coaches.Count; | |
} | |
public void AttachCoach(Coach coach) | |
{ | |
_coaches.Add(coach); | |
Capacity += coach.Capacity; | |
} | |
public void DrawCoaches() | |
{ | |
Console.Write("\n\n<g8gg[]"); | |
foreach (var coach in _coaches) | |
coach.DrawCoach(); | |
Console.WriteLine(); | |
} | |
} | |
class Coach | |
{ | |
public Coach(int capacity) | |
{ | |
Capacity = capacity; | |
} | |
public int Capacity { get; } | |
public void DrawCoach() | |
{ | |
Console.Write($"_[ {Capacity} ]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment