Last active
September 17, 2025 10:29
-
-
Save Strelok78/4ad0296eb89f0ee87bcd127bd89f3deb to your computer and use it in GitHub Desktop.
Railway manager terminal app
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
| using iJuniorPractice; | |
| namespace iJuniorPractice | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Dispatcher dispatcher = new Dispatcher(); | |
| dispatcher.Work(); | |
| } | |
| } | |
| static class Utils | |
| { | |
| public static int ReadInt(string message = "") | |
| { | |
| int number; | |
| string input; | |
| Console.WriteLine(message); | |
| input = Console.ReadLine(); | |
| while (int.TryParse(input, out number) == false) | |
| { | |
| Console.WriteLine("Incorrect input. Try again."); | |
| input = Console.ReadLine(); | |
| } | |
| return number; | |
| } | |
| public static string ReadString(string message = "") | |
| { | |
| string input; | |
| Console.WriteLine(message); | |
| input = Console.ReadLine(); | |
| while (input == "") | |
| { | |
| Console.WriteLine("Incorrect input, line is empty. Try again."); | |
| input = Console.ReadLine(); | |
| } | |
| return input; | |
| } | |
| } | |
| } | |
| class Dispatcher | |
| { | |
| public void Work() | |
| { | |
| Path path = null; | |
| int ticketsCount = 0; | |
| bool isWork = true; | |
| List<Train> trains = new List<Train>(); | |
| while (isWork) | |
| { | |
| if (CanProceed()) | |
| { | |
| path = CreatePath(); | |
| ticketsCount = SellTickets(); | |
| CreateTrain(trains, path, ticketsCount); | |
| ShowTrainInfo(trains); | |
| trains.Last().Depart(); | |
| Console.WriteLine("Train starts depart."); | |
| Console.WriteLine("Press any key to create new train."); | |
| Console.ReadKey(); | |
| Console.Clear(); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Exiting."); | |
| isWork = false; | |
| } | |
| } | |
| } | |
| private bool CanProceed() | |
| { | |
| const int ProceedCommand = 1; | |
| int command = Utils.ReadInt( | |
| $"Enter - \"{ProceedCommand}\" to start, or any other digit to exit."); | |
| return command == ProceedCommand; | |
| } | |
| private Path CreatePath() | |
| { | |
| Path path = new Path(); | |
| path.SetDepartCity(); | |
| path.SetDestinationCity(); | |
| return path; | |
| } | |
| private int SellTickets() | |
| { | |
| int minTicketsValue = 100; | |
| int maxTicketsValue = 5000; | |
| Random random = new Random(); | |
| return random.Next(minTicketsValue, maxTicketsValue); | |
| } | |
| private void CreateTrain(List<Train> trains, Path path, int ticketsCount) | |
| { | |
| List<Wagon> wagons = CreateWagons(ticketsCount); | |
| Train train = new Train(path, wagons, ticketsCount); | |
| trains.Add(train); | |
| } | |
| private List<Wagon> CreateWagons(int ticketsCount) | |
| { | |
| int seatsCount = 0; | |
| List<Wagon> wagons = new List<Wagon>(); | |
| while (seatsCount < ticketsCount) | |
| { | |
| Wagon wagon = new Wagon(); | |
| wagons.Add(wagon); | |
| seatsCount += wagon.WagonCapacity; | |
| } | |
| return wagons; | |
| } | |
| private void ShowTrainInfo(List<Train> trains) | |
| { | |
| Console.Clear(); | |
| Console.WriteLine(new string('_', 10)); | |
| int counter = 0; | |
| foreach (var train in trains) | |
| { | |
| Console.WriteLine($"Train {++counter}:"); | |
| train.ShowInfo(); | |
| } | |
| Console.WriteLine(new string('_', 10)); | |
| } | |
| } | |
| enum TrainStatus | |
| { | |
| Created, | |
| Departed | |
| } | |
| class Train | |
| { | |
| private List<Wagon> _wagons; | |
| private Path _path; | |
| private string _trainState; | |
| private int _ticketsCount; | |
| public Train(Path path, List<Wagon> wagons, int ticketsCount) | |
| { | |
| _ticketsCount = ticketsCount; | |
| _path = path; | |
| _wagons = wagons; | |
| _trainState = TrainStatus.Created.ToString(); | |
| } | |
| public void ShowInfo() | |
| { | |
| Console.WriteLine($"Depart City: {_path.DepartCityName}\n" + | |
| $"Destination City: {_path.DestinationCityName}\n" + | |
| $"Wagon count: {_wagons.Count}, " + | |
| $"Wagon capacity: {_wagons.First().WagonCapacity}, " + | |
| $"Total seats count: {_wagons.Count * _wagons.First().WagonCapacity}\n" + | |
| $"Tickets sold: {_ticketsCount}\n\n" + | |
| $"Train state: {_trainState}\n"); | |
| } | |
| public void Depart() | |
| { | |
| _trainState = TrainStatus.Departed.ToString(); | |
| } | |
| } | |
| class Path | |
| { | |
| public string DepartCityName { get; private set; } | |
| public string DestinationCityName { get; private set; } | |
| public void SetDepartCity() | |
| { | |
| string departCityName = Utils.ReadString("DepartCityName: "); | |
| DepartCityName = departCityName; | |
| } | |
| public void SetDestinationCity() | |
| { | |
| string destinationCityName = Utils.ReadString("DestinationCityName: "); | |
| while (destinationCityName == DepartCityName) | |
| { | |
| Console.WriteLine($"Error! Destination City name should differ from DepartCityName - {DepartCityName}."); | |
| destinationCityName = Utils.ReadString("DestinationCityName: "); | |
| } | |
| DestinationCityName = destinationCityName; | |
| } | |
| } | |
| class Wagon | |
| { | |
| public int WagonCapacity => 50; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment