Forked from Avet1k/PassengerTrainConfigurator.cs
Last active
November 14, 2023 07:48
-
-
Save talatari/edacef241df26b22f91e111828530e3d to your computer and use it in GitHub Desktop.
Passenger train configurator
This file contains 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 RandomIncomingData _randomData; | |
public TrainConfigurator() | |
{ | |
_randomData = new RandomIncomingData(); | |
} | |
public int TicketPrice { get; private set; } | |
public void Work() | |
{ | |
const char QuitCommand = '1'; | |
bool isWorking = true; | |
while (isWorking) | |
{ | |
int passengersCount; | |
TicketPrice = _randomData.GetPrice(); | |
SetDirection(); | |
passengersCount = _randomData.GetPassengersCount(); | |
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) | |
{ | |
ShowInfo(); | |
_train = new Train(); | |
while (_train.Capacity < passengersCount) | |
{ | |
_train.AttachCoach(new Coach(_randomData.GetCoachCapacity())); | |
} | |
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; | |
_train.Move(); | |
Console.ReadKey(); | |
Console.WriteLine("Поезд отправляется в путь!"); | |
_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.Write("Пассажиры заняли свои места, провожающие вышли из вагонов.\n" + | |
"Нажмите любую кнопку для отправления поезда в путь..."); | |
} | |
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} ]"); | |
} | |
} | |
class RandomIncomingData | |
{ | |
private Random _random; | |
public RandomIncomingData() | |
{ | |
_random = new Random(); | |
} | |
public int GetNumber(int minPrice, int maxPrice) | |
{ | |
return _random.Next(minPrice, maxPrice); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment