Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace trains
- {
- class Program
- {
- static void Main(string[] args)
- {
- TicketOffice ticketOffice = new TicketOffice();
- ticketOffice.Work();
- }
- }
- class TicketOffice
- {
- private Passengers _passengers = new Passengers();
- private List<Train> _trains = new List<Train>()
- {
- new Train(1, 0, false),
- new Train(2, 0, false)
- };
- private List<Directions> _directions = new List<Directions>();
- public void Work()
- {
- bool isWork = true;
- while (isWork)
- {
- ShowInfo();
- Console.WriteLine($"\n[1] - Создать поезд;\n" +
- $"[2] - Сформировать направление\n" +
- $"[3] - Продать билеты\n" +
- $"[4] - Отправить поезд\n" +
- $"[5] - Выход");
- Console.Write("Ввод: ");
- switch (Console.ReadLine())
- {
- case "1":
- CreateTrain();
- break;
- case "2":
- CreateDirection();
- break;
- case "3":
- SellTickets();
- break;
- case "4":
- SendTrain();
- break;
- case "5":
- isWork = false;
- break;
- default:
- Console.WriteLine("Ошибка!");
- break;
- }
- }
- }
- private void CreateTrain()
- {
- Train train = new Train(_trains.Count + 1, 0);
- _trains.Add(train);
- }
- private void CreateDirection()
- {
- Console.WriteLine("Выберите поезд из депо: ");
- if (TryGetNumber(out int userNumber, _trains.Count) && _trains[userNumber].СheckOutDepature == false)
- {
- Console.Write("Поезд отправится из города: ");
- string directionFrom = Console.ReadLine();
- Console.Write("Поезд отправится с платформы: ");
- string platformFrom = Console.ReadLine();
- Console.Write("Поезд отправится в город: ");
- string directionToThe = Console.ReadLine();
- Console.Write("Поезд прибывает на платформу: ");
- string platformToThe = Console.ReadLine();
- Directions direction = new Directions(_trains[userNumber], _passengers, directionFrom, platformFrom, directionToThe, platformToThe);
- _directions.Add(direction);
- }
- }
- private void SellTickets()
- {
- if (CheckDirections() == false)
- return;
- Console.Write("Укажите номер поезда: ");
- if(TryGetNumber(out int userNumber, _directions.Count) == false)
- return;
- if (CheckTickets(userNumber) == false)
- return;
- else if (_directions[userNumber].Train.СheckOutDepature)
- {
- Console.WriteLine("Нельзя повторно продать билеты на рейс!");
- return;
- }
- Random rand = new Random();
- _passengers = new Passengers(rand.Next(1, 20));
- _directions[userNumber].GetClients(_passengers);
- _directions[userNumber].GetDirection(_directions[userNumber]);
- }
- private void SendTrain()
- {
- if (CheckDirections() == false)
- return;
- Console.Write("Укажите номер поезда: ");
- if (TryGetNumber(out int userNumber, _directions.Count) == false)
- return;
- if (_directions[userNumber].Train.СheckOutDepature)
- {
- Console.WriteLine("Поезд уже отправлен!");
- return;
- }
- _directions[userNumber].Train.SetSendStatus(true);
- }
- private void ShowInfo()
- {
- Console.Write("Для продолжения нажмите любую клавишу...");
- Console.ReadKey(true);
- Console.Clear();
- Console.WriteLine("Расписание поездов: ");
- Console.WriteLine("==============");
- if (_trains.Count == 0)
- {
- Console.Write("В депо нет поездов!");
- }
- else if (_trains.Count > 0)
- {
- Console.WriteLine("В депо находятся находятся:");
- foreach (var train in _trains)
- {
- Console.WriteLine($"Поезд №{train.Number}");
- }
- }
- Console.WriteLine("==============");
- if (CheckDirections())
- {
- foreach (var direction in _directions)
- {
- Console.Write($"№{direction.Train.Number}, из гор.{direction.DirectionFrom} платформа {direction.PlatformFrom} " +
- $"скоро направится в город {direction.DirectionToThe} платформа {direction.PlatformToThe}." +
- $"Поезд не отправлен.\n");
- if (direction.Train.Wagons == 0)
- Console.Write("Без вагонов. Поезд не готов к отправке.\n");
- else if(direction.Train.Wagons > 0)
- {
- Console.Write("Вагонов: " + direction.Train.Wagons + " шт. ");
- Console.Write("Билетов продано: " + direction.Clients.Tickets +"\n");
- if (direction.Train.СheckOutDepature)
- Console.Write("Поезд отправлен.\n");
- else
- Console.Write("Поезд не отправлен.\n");
- }
- }
- }
- Console.WriteLine("==============");
- }
- private bool TryGetNumber(out int userInput, int count)
- {
- if (int.TryParse(Console.ReadLine(), out userInput) && userInput <= count && userInput > 0)
- {
- --userInput;
- return true;
- }
- else
- {
- Console.WriteLine("Ошибка! Такого номера нет.");
- }
- return false;
- }
- private bool CheckDirections()
- {
- if (_directions.Count == 0)
- {
- Console.WriteLine("К отправке нет готовых поездов!");
- return false;
- }
- return true;
- }
- private bool CheckTickets(int userNumber)
- {
- if (_directions[userNumber].Clients.Tickets > 0)
- {
- Console.WriteLine("На рейс уже куплены билеты!");
- return false;
- }
- return true;
- }
- }
- class Train
- {
- public bool СheckOutDepature { get; private set; }
- public int Number { get; private set; }
- public int Wagons { get; private set; }
- public Train(int number, int wagons, bool isTrainSent = false)
- {
- Number = number;
- Wagons = wagons;
- СheckOutDepature = isTrainSent;
- }
- public void CreateWagons(int tickets)
- {
- int wagonSeats;
- Random rand = new Random();
- for (; tickets >= 0; tickets--)
- {
- wagonSeats = rand.Next(30, 80);
- tickets -= wagonSeats;
- ++Wagons;
- }
- }
- public void SetSendStatus(bool flag)
- {
- СheckOutDepature = flag;
- }
- }
- class Directions
- {
- public string DirectionFrom { get; private set; }
- public string PlatformFrom { get; private set; }
- public string DirectionToThe { get; private set; }
- public string PlatformToThe { get; private set; }
- public Passengers Clients { get; private set; }
- public Train Train { get; private set; }
- public Directions(Train train, Passengers clients, string directionFrom, string platformFrom, string directionToThe, string platformToThe)
- {
- Train = train;
- Clients = clients;
- DirectionFrom = directionFrom;
- PlatformFrom = platformFrom;
- PlatformToThe = platformToThe;
- DirectionToThe = directionToThe;
- }
- public void GetDirection(Directions direction)
- {
- DirectionFrom = direction.DirectionFrom;
- PlatformFrom = direction.PlatformFrom;
- DirectionToThe = direction.DirectionToThe;
- PlatformToThe = direction.PlatformToThe;
- }
- public void GetClients(Passengers clients)
- {
- Clients = clients;
- Train.CreateWagons(Clients.Tickets);
- }
- }
- class Passengers
- {
- public int Tickets { get; private set; }
- public Passengers()
- {
- Tickets = 0;
- }
- public Passengers(int tickets)
- {
- Tickets = tickets;
- }
- }
- }
Add Comment
Please, Sign In to add comment