Last active
November 14, 2023 07:50
-
-
Save talatari/67f27e6add946facf2f663681c9174d5 to your computer and use it in GitHub Desktop.
44. Task: 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
public static class Program | |
{ | |
public static void Main() | |
{ | |
Station station = new Station(); | |
station.Work(); | |
} | |
} | |
public class Station | |
{ | |
private Train _currentTrain; | |
private Dictionary<string, Train> _allTrainsInDay = new Dictionary<string, Train>(); | |
private TrainFactory _factory = new TrainFactory(); | |
private bool _isDirectionOpen = false; | |
private bool _isTrainCreated = false; | |
private string _departurePoint = ""; | |
private string _destinationPoint = ""; | |
private int _selledTickets = 0; | |
private string _yesText = "yes"; | |
private string _noText = "no"; | |
public void Work() | |
{ | |
bool isWork = true; | |
while (isWork) | |
{ | |
Console.Clear(); | |
ShowInfoAboutTrain(); | |
if (_isDirectionOpen == false) | |
{ | |
isWork = RequestOpenDirection(); | |
} | |
else if (_selledTickets == 0) | |
{ | |
RequestSellTickets(); | |
} | |
else if (_isTrainCreated == false) | |
{ | |
_isTrainCreated = CreateTrain(); | |
} | |
else | |
{ | |
if (RequestCloseDirection() == true) | |
{ | |
CloseDirection(); | |
ResetParametersDirection(); | |
} | |
} | |
} | |
} | |
private void ShowInfoAboutTrain() | |
{ | |
Console.WriteLine($"[{this} work]\n"); | |
if (_allTrainsInDay.Count > 0) | |
{ | |
Console.WriteLine("Sended trains in day:"); | |
foreach (var train in _allTrainsInDay) | |
{ | |
Console.WriteLine( | |
$"\nTrain on direction: {train.Key}\nWagons: " + | |
$"Large = {train.Value.GetCountLargeWagons()}, " + | |
$"Medium = {train.Value.GetCountMediumWagons()}, " + | |
$"Small = {train.Value.GetCountSmallWagons()}\n" + | |
$"Total passengers: {train.Value.GetPassengers()}"); | |
} | |
Console.WriteLine(); | |
} | |
if (_isDirectionOpen) | |
{ | |
Console.WriteLine($"\nOpen direction: {_departurePoint} - {_destinationPoint}"); | |
} | |
else | |
{ | |
Console.WriteLine("\nOpen direction not."); | |
} | |
if (_selledTickets > 0) | |
{ | |
Console.WriteLine($"Selled tickets = {_selledTickets}"); | |
} | |
Console.WriteLine(); | |
} | |
private bool RequestOpenDirection() | |
{ | |
Console.Write($"Open direction? ({_yesText}/{_noText}): "); | |
string userInput = Console.ReadLine().ToLower(); | |
if (userInput == _yesText) | |
{ | |
OpenDirection(); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
private void OpenDirection() | |
{ | |
Console.Write("Enter departure point: "); | |
_departurePoint = Console.ReadLine(); | |
Console.Write("Enter destination point: "); | |
_destinationPoint = Console.ReadLine(); | |
if (_departurePoint.ToLower() == _destinationPoint.ToLower()) | |
{ | |
_departurePoint = ""; | |
_destinationPoint = ""; | |
Console.WriteLine("Direction not open. Try again.\n"); | |
} | |
else | |
{ | |
_isDirectionOpen = true; | |
} | |
} | |
private void RequestSellTickets() | |
{ | |
Console.Write($"Sell tickets for this direction? ({_yesText}/{_noText}): "); | |
string userInput = Console.ReadLine().ToLower(); | |
if (userInput == _yesText) | |
{ | |
SellTickets(); | |
} | |
} | |
private void SellTickets() | |
{ | |
Random random = new Random(); | |
int minRange = 100; | |
int maxRange = 10000; | |
_selledTickets = random.Next(minRange, maxRange); | |
} | |
private bool CreateTrain() | |
{ | |
_currentTrain = _factory.CreateTrain(_selledTickets); | |
_currentTrain.FillWagon(_selledTickets); | |
return true; | |
} | |
private bool RequestCloseDirection() | |
{ | |
Console.Write($"Close direction? ({_yesText}/{_noText}): "); | |
string userInput = Console.ReadLine().ToLower(); | |
if (userInput == _yesText) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
private void CloseDirection() | |
{ | |
Console.WriteLine( | |
$"\nTrain on direction: {_departurePoint} - {_destinationPoint} " + | |
$"\nwith {_selledTickets} passengers - departed!"); | |
Console.WriteLine("\nTrain composition: "); | |
_currentTrain.ShowAllWagons(); | |
Console.ReadKey(); | |
} | |
private void ResetParametersDirection() | |
{ | |
_allTrainsInDay.Add(_departurePoint + " - " + _destinationPoint, _currentTrain); | |
_isDirectionOpen = false; | |
_isTrainCreated = false; | |
_departurePoint = ""; | |
_destinationPoint = ""; | |
_selledTickets = 0; | |
} | |
} | |
public class TrainFactory | |
{ | |
private Dictionary<WagonType, int> _capacity = new Dictionary<WagonType, int>() | |
{ | |
{ WagonType.Small, 18 }, | |
{ WagonType.Medium, 36 }, | |
{ WagonType.Large, 54 } | |
}; | |
public Train CreateTrain(int passengers) | |
{ | |
int _passengers = passengers; | |
float percentLargeTrain = 0.6f; | |
int halfPassengersSmallTrain = 2; | |
int extroverts = (int)(passengers * percentLargeTrain); | |
int introverts = (passengers - extroverts) / halfPassengersSmallTrain; | |
List<Wagon> wagons = new List<Wagon>(); | |
while (passengers >= extroverts) | |
{ | |
wagons.Add(CreateLargeWagon()); | |
passengers -= _capacity[WagonType.Large]; | |
} | |
while (passengers > introverts) | |
{ | |
wagons.Add(CreateMediumWagon()); | |
passengers -= _capacity[WagonType.Medium]; | |
} | |
while (passengers > 0) | |
{ | |
wagons.Add(CreateSmallWagon()); | |
passengers -= _capacity[WagonType.Small]; | |
} | |
return new Train(wagons, _passengers); | |
} | |
public Wagon CreateSmallWagon() | |
{ | |
return new Wagon(WagonType.Small, _capacity[WagonType.Small]); | |
} | |
public Wagon CreateMediumWagon() | |
{ | |
return new Wagon(WagonType.Medium, _capacity[WagonType.Medium]); | |
} | |
public Wagon CreateLargeWagon() | |
{ | |
return new Wagon(WagonType.Large, _capacity[WagonType.Large]); | |
} | |
} | |
public class Train | |
{ | |
private List<Wagon> _wagons; | |
private int _passengers; | |
public Train(List<Wagon> wagons, int passengers) | |
{ | |
_wagons = wagons; | |
_passengers = passengers; | |
} | |
public void ShowAllWagons() | |
{ | |
for (int i = 0; i < _wagons.Count; i++) | |
{ | |
Console.Write(i + 1); | |
_wagons[i].ShowInfoWagon(); | |
} | |
Console.WriteLine(); | |
} | |
public int GetCountLargeWagons() | |
{ | |
int count = 0; | |
for (int i = 0; i < _wagons.Count; i++) | |
{ | |
count += _wagons[i].GetCountLargeWagons(); | |
} | |
return count; | |
} | |
public int GetCountMediumWagons() | |
{ | |
int count = 0; | |
for (int i = 0; i < _wagons.Count; i++) | |
{ | |
count += _wagons[i].GetCountMediumWagons(); | |
} | |
return count; | |
} | |
public int GetCountSmallWagons() | |
{ | |
int count = 0; | |
for (int i = 0; i < _wagons.Count; i++) | |
{ | |
count += _wagons[i].GetCountSmallWagons(); | |
} | |
return count; | |
} | |
public int GetPassengers() | |
{ | |
return _passengers; | |
} | |
public void FillWagon(int passengers) | |
{ | |
foreach (var wagon in _wagons) | |
{ | |
passengers = wagon.BoardingPassengers(passengers); | |
} | |
} | |
} | |
public class Wagon | |
{ | |
private WagonType _wagonType; | |
private int _capacity; | |
private int _passengers; | |
public Wagon(WagonType wagonType, int capacity) | |
{ | |
_wagonType = wagonType; | |
_capacity = capacity; | |
} | |
public int GetCountLargeWagons() | |
{ | |
if (_wagonType == WagonType.Large) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
public int GetCountMediumWagons() | |
{ | |
if (_wagonType == WagonType.Medium) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
public int GetCountSmallWagons() | |
{ | |
if (_wagonType == WagonType.Small) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
public void ShowInfoWagon() | |
{ | |
Console.WriteLine($" {_wagonType}, Passengers: {_passengers} / {_capacity}"); | |
} | |
public int BoardingPassengers(int passengers) | |
{ | |
if ((passengers - _capacity) < 0) | |
{ | |
_passengers = passengers; | |
passengers = 0; | |
} | |
else | |
{ | |
_passengers = _capacity; | |
passengers -= _capacity; | |
} | |
return passengers; | |
} | |
} | |
public enum WagonType | |
{ | |
Small, | |
Medium, | |
Large | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment