Advertisement
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),
- new Train(2, 0)
- };
- private List<Directions> _directions = new List<Directions>()
- {
- new Directions("msk", "4", "spb", "6")
- };
- public void Work()
- {
- bool isWork = true;
- while (isWork)
- {
- ShowInfo();
- Console.WriteLine($"\n[1] - Создать поезд;\n" +
- $"[2] - Сформировать направление\n" +
- $"[3] - Продать билеты\n" +
- $"[4] - Выбрать направление\n" +
- $"[5] - Отправить поезд\n" +
- $"[6] - Выход");
- Console.Write("Ввод: ");
- switch (Console.ReadLine())
- {
- case "1":
- CreateTrain();
- break;
- case "2":
- CreateDirection();
- break;
- case "3":
- SellTickets();
- break;
- case "4":
- ChooseDirection();
- break;
- case "5":
- SendTrain();
- break;
- case "6":
- 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.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(directionFrom, platformFrom, directionToThe, platformToThe);
- _directions.Add(direction);
- Console.WriteLine("Выберите поезд из депо: ");
- if (TryGetNumber(out int userNumber, _trains.Count) && _trains[userNumber].IsDirectionSet == false)
- {
- _trains[userNumber] = new Train(direction, _trains[userNumber]);
- }
- }
- private void ChooseDirection()
- {
- if (CheckDirections() == false)
- {
- return;
- }
- Console.WriteLine("Выберите направление: ");
- if (TryGetNumber(out int userDirection, _directions.Count) == false)
- {
- return;
- }
- Console.Write("Укажите номер поезда: ");
- if (TryGetNumber(out int userTrain, _trains.Count) == false)
- {
- return;
- }
- if (_trains[userTrain].IsTrainSent)
- {
- Console.WriteLine("Поезд уже отправлен!");
- return;
- }
- else if (_trains[userTrain].IsDirectionSet)
- {
- Console.WriteLine("Этому поезду уже задано направление!");
- return;
- }
- foreach (Train train in _trains)
- {
- foreach (Directions direction in _directions)
- {
- if(train.Direction == direction)
- {
- Console.WriteLine("Нельзя задать такое же направление другим поездам!");
- return;
- }
- }
- }
- _trains[userTrain] = new Train(_directions[userDirection], _trains[userTrain]);
- _trains[userTrain].SetDirection();
- }
- private void SellTickets()
- {
- if (CheckDirections() == false)
- {
- return;
- }
- Console.Write("Укажите номер поезда: ");
- if (TryGetNumber(out int userNumber, _trains.Count) == false)
- {
- return;
- }
- else if (CheckTickets(userNumber) == false)
- {
- return;
- }
- else if (_trains[userNumber].IsTrainSent)
- {
- Console.WriteLine("Нельзя повторно продать билеты на рейс!");
- return;
- }
- Random rand = new Random();
- _passengers = new Passengers(rand.Next(1, 20));
- _directions[userNumber] = new Directions(_directions[userNumber], _passengers);
- _passengers = null;
- }
- private void SendTrain()
- {
- if (CheckDirections() == false)
- {
- return;
- }
- Console.Write("Укажите номер поезда: ");
- if (TryGetNumber(out int userNumber, _trains.Count) == false)
- {
- return;
- }
- else if(_trains[userNumber].IsDirectionSet == false)
- {
- Console.WriteLine("Задайте поезду направление!");
- return;
- }
- else if (_trains[userNumber].IsTrainSent)
- {
- Console.WriteLine("Поезд уже отправлен!");
- return;
- }
- _trains[userNumber].SetDispatch();
- _trains[userNumber] = new Train(_directions[userNumber], _trains[userNumber]);
- _trains[userNumber].CreateWagons();
- }
- 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)
- {
- if(train.IsDirectionSet)
- {
- Console.Write($"№{train.Number}, из гор.{train.Direction.DirectionFrom} платформа {train.Direction.PlatformFrom} " +
- $"скоро направится в город {train.Direction.DirectionToThe} платформа {train.Direction.PlatformToThe}." +
- $"Поезд не отправлен.\n");
- if (train.Wagons == 0)
- Console.Write("Без вагонов. Поезд не готов к отправке.\n");
- else if (train.Wagons > 0)
- {
- Console.Write("Вагонов: " + train.Wagons + " шт. ");
- Console.Write("Билетов продано: " + train.Direction.Clients + "\n");
- if (train.IsTrainSent)
- {
- Console.Write("Поезд отправлен.\n");
- }
- else
- {
- Console.Write("Поезд не отправлен.\n");
- }
- }
- }
- else if(train.IsDirectionSet == false)
- {
- Console.WriteLine($"Поезд №{train.Number}. Без вагонов. Поезд не готов к отправке.\n");
- }
- }
- }
- Console.WriteLine("Направления: ");
- Console.WriteLine("==============");
- if (CheckDirections())
- {
- foreach (var direction in _directions)
- {
- Console.Write($"Направление из гор.{direction.DirectionFrom} платформа {direction.PlatformFrom} " +
- $"в город {direction.DirectionToThe} платформа {direction.PlatformToThe}\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].ArePassengersCome)
- {
- Console.WriteLine("На рейс уже куплены билеты!");
- return false;
- }
- return true;
- }
- }
- class Train
- {
- public bool IsTrainSent { get; private set; }
- public bool IsDirectionSet { get; private set; }
- public int Number { get; private set; }
- public int Wagons { get; private set; }
- public Directions Direction { get; private set; }
- public Train(int number, int wagons, bool isTrainSent = false, bool isDirectionSet = false, bool arePassengersCome = false)
- {
- Number = number;
- Wagons = wagons;
- IsTrainSent = isTrainSent;
- IsDirectionSet = isDirectionSet;
- }
- public Train(Directions direction, Train train)
- {
- Direction = direction;
- Number = train.Number;
- Wagons = train.Wagons;
- IsTrainSent = train.IsTrainSent;
- IsDirectionSet = train.IsDirectionSet;
- }
- public void CreateWagons()
- {
- int wagonSeats;
- Random rand = new Random();
- while(Direction.Clients.Tickets >= 0)
- {
- wagonSeats = rand.Next(30, 80);
- Direction.Clients.ActivateTickets(wagonSeats);
- ++Wagons;
- }
- }
- public void SetDispatch()
- {
- IsTrainSent = true;
- }
- public void SetDirection()
- {
- IsDirectionSet = true;
- }
- }
- class Directions
- {
- public bool ArePassengersCome { get; private set; }
- 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 Directions(string directionFrom, string platformFrom, string directionToThe, string platformToThe, bool arePassengersCome = false)
- {
- DirectionFrom = directionFrom;
- PlatformFrom = platformFrom;
- PlatformToThe = platformToThe;
- DirectionToThe = directionToThe;
- ArePassengersCome = arePassengersCome;
- }
- public Directions(Directions direction, Passengers passengers)
- {
- DirectionFrom = direction.DirectionFrom;
- PlatformFrom = direction.PlatformFrom;
- DirectionToThe = direction.DirectionToThe;
- PlatformToThe = direction.PlatformToThe;
- Clients = passengers;
- }
- }
- class Passengers
- {
- public int Tickets { get; private set; }
- public Passengers()
- {
- Tickets = 0;
- }
- public Passengers(int tickets)
- {
- Tickets = tickets;
- }
- public void ActivateTickets(int activateTickets)
- {
- Tickets -= activateTickets;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement