Last active
May 17, 2023 07:52
-
-
Save Strelok78/3ebf3871b6cbc58484c9ebcc8e15a75f to your computer and use it in GitHub Desktop.
The program helps the user to make a train plan. In the upper part of the program, full information about the current flight or its absence is displayed.
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
internal class Program | |
{ | |
public static void Main() | |
{ | |
Terminal userInterface = new Terminal(); | |
userInterface.ShowMainMenu(); | |
} | |
class Train | |
{ | |
private int _seats = 0; | |
private bool _status = false; | |
private List<Wagon> _wagons = new List<Wagon>(); | |
public Train() { } | |
public Train(int passangersNumber) | |
{ | |
while (_seats < passangersNumber) | |
{ | |
Wagon wagon = new Wagon(); | |
_seats += wagon.Seats; | |
_wagons.Add(wagon); | |
} | |
} | |
public void Depart() | |
{ | |
_status = true; | |
} | |
public string GetInfo() | |
{ | |
string info = $"Wagons info: "; | |
List<string> types = new List<string>(); | |
List<int> roominess = new List<int>(); | |
Dictionary<string, int> wagonCounter = new Dictionary<string, int>(); | |
if (_wagons.Any() == false) | |
{ | |
info += "No info.\n"; | |
} | |
else | |
{ | |
foreach (Wagon wagon in _wagons) | |
{ | |
if (types.Contains(wagon.Type) == false) | |
{ | |
types.Add(wagon.Type); | |
roominess.Add(wagon.Seats); | |
wagonCounter.Add(wagon.Type, 1); | |
} | |
else | |
{ | |
wagonCounter[wagon.Type] += 1; | |
} | |
} | |
for (int i = 0; i < types.Count(); i++) | |
{ | |
info += $"\n{i + 1}. Type: {types[i]}, seats: {roominess[i]}, count: {wagonCounter[types[i]]}.\n"; | |
} | |
info += $"Total number of seats in train: {_seats}\n"; | |
} | |
info += $"Departure status: {_status}.\n"; | |
return info; | |
} | |
} | |
class Wagon | |
{ | |
private string[] _types = { "Small", "Big", "VIP" }; | |
private int[] _roominess = { 10, 20, 4 }; | |
public Wagon() | |
{ | |
Random random = new Random(); | |
int value = random.Next(_types.Length - 1); | |
Seats = _roominess[value]; | |
Type = _types[value]; | |
} | |
public int Seats { get; private set; } | |
public string Type { get; private set; } | |
} | |
class Ticket | |
{ | |
private int _maxValue = 10000; | |
private Random _random = new Random(); | |
public Ticket() | |
{ | |
TicketsNumber = -1; | |
} | |
public Ticket(Path path) | |
{ | |
TicketsNumber = _random.Next(_maxValue); | |
} | |
public int TicketsNumber { get; private set; } | |
public string GetInfo() | |
{ | |
string ticketsInfo = "Tickets sold: "; | |
if (TicketsNumber < 0) | |
return null; | |
else | |
ticketsInfo += TicketsNumber < 0 ? "Start sellings first!\n" : $"{TicketsNumber}.\n"; | |
return ticketsInfo; | |
} | |
} | |
class Path | |
{ | |
private string _departure = string.Empty; | |
private string _arrival = string.Empty; | |
public Path() | |
{ | |
} | |
public Path(string departure, string arrival) | |
{ | |
_departure = departure; | |
_arrival = arrival; | |
} | |
public string GetRoute() | |
{ | |
if (_departure != string.Empty) | |
{ | |
string route = $"Trip: {_departure} - {_arrival}.\n"; | |
return route; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
} | |
class Terminal | |
{ | |
private ErrorLogger _error = new ErrorLogger(); | |
private Path _path = new Path(); | |
private Ticket _tickets = new Ticket(); | |
private Train _train = new Train(); | |
public void ShowMainMenu() | |
{ | |
const int CommandCreatePath = 1; | |
const int CommandSellTickets = 2; | |
const int CommandCreateTrain = 3; | |
const int CommandSendTrain = 4; | |
const int CommandExit = 5; | |
int command; | |
bool isOpen = true; | |
string menuText = $"Press {CommandCreatePath} - to create path\n" + | |
$"Press {CommandSellTickets} - to sell tickets\n" + | |
$"Press {CommandCreateTrain} - to create train\n" + | |
$"Press {CommandSendTrain} - to send train\n" + | |
$"Press {CommandExit} - to exit\n"; | |
while (isOpen) | |
{ | |
TripInfo(); | |
Console.WriteLine(menuText); | |
if (int.TryParse(Console.ReadLine(), out command)) | |
{ | |
switch (command) | |
{ | |
case CommandCreatePath: | |
CreatePath(); | |
break; | |
case CommandSellTickets: | |
SellTickets(); | |
break; | |
case CommandCreateTrain: | |
CreateTrain(); | |
break; | |
case CommandSendTrain: | |
SendTrain(); | |
break; | |
case CommandExit: | |
isOpen = false; | |
break; | |
default: | |
Console.WriteLine(_error.WrongCommandMessage); | |
break; | |
} | |
} | |
else | |
{ | |
Console.WriteLine(_error.ParsingMessage); | |
} | |
Console.ReadKey(true); | |
Console.Clear(); | |
} | |
} | |
private void TripInfo() | |
{ | |
string emptyDescription = "No trips prepared\n\n"; | |
string description; | |
description = _path.GetRoute() + _tickets.GetInfo() + _train.GetInfo(); | |
if (_path.GetRoute() != null) | |
{ | |
Console.WriteLine(description); | |
} | |
else | |
{ | |
Console.WriteLine(emptyDescription); | |
} | |
} | |
private void SendTrain() | |
{ | |
if (_train.GetInfo().Contains("No info") == false) | |
{ | |
_train.Depart(); | |
Console.Clear(); | |
TripInfo(); | |
_path = new Path(); | |
_tickets = new Ticket(); | |
_train = new Train(); | |
} | |
else | |
{ | |
Console.WriteLine(_error.OtherError + "First create train!"); | |
} | |
} | |
private void CreateTrain() | |
{ | |
if (_tickets.GetInfo() != null) | |
{ | |
_train = new Train(_tickets.TicketsNumber); | |
} | |
else | |
{ | |
Console.WriteLine(_error.OtherError + "Sell tickets first"); | |
} | |
} | |
private void SellTickets() | |
{ | |
if (_path.GetRoute() != null) | |
{ | |
_tickets = new Ticket(_path); | |
} | |
else | |
{ | |
Console.WriteLine(_error.OtherError + "Add trip first"); | |
} | |
} | |
private void CreatePath() | |
{ | |
string departure; | |
string arrival; | |
Console.Write("Enter departure city: "); | |
departure = Console.ReadLine(); | |
Console.Write("Enter arrival city: "); | |
arrival = Console.ReadLine(); | |
_path = new Path(departure, arrival); | |
} | |
} | |
class ErrorLogger | |
{ | |
public ErrorLogger() | |
{ | |
ParsingMessage = "Error! Enter a number, please."; | |
WrongCommandMessage = "Error, there is no such command!"; | |
OtherError = "Error! "; | |
} | |
public string ParsingMessage { get; private set; } | |
public string WrongCommandMessage { get; private set; } | |
public string OtherError { get; private set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment