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 CSLight
- {
- class Program
- {
- static void Main()
- {
- while (true)
- {
- Console.Clear();
- Console.WriteLine("Сконфигурируйте новый поезд!");
- ShowActualInfo(null);
- Train train = CreateTrain();
- train.ShowTrain();
- Ticket ticket = CreateTicket(train);
- ShowActualInfo(ticket);
- }
- }
- private static Ticket CreateTicket(Train train)
- {
- while (true)
- {
- Console.Write("\nВыберите вагон: ");
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int wagonNumber))
- {
- if (wagonNumber > train.WagonsCount || wagonNumber < 1)
- Print("Такого вагона в поезде нет, попробуйте ещё раз.", ConsoleColor.Red);
- else
- {
- Console.Clear();
- while (true)
- {
- Wagon wagon = train.GetWagon(wagonNumber - 1);
- wagon.ShowWagon();
- Console.Write("\nВыберите свободное место: ");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int seatNumber))
- {
- Console.Clear();
- if (seatNumber > wagon.SeatsCount || seatNumber < 0)
- Print("Такого места в вагоне нет, попробуйте ещё раз.", ConsoleColor.Red);
- else
- {
- Seat seat = wagon.GetSeat(seatNumber - 1);
- if (seat.IsFree)
- {
- seat.TakeSeat();
- return new Ticket(train, wagonNumber, seatNumber);
- }
- Print("Это место занято, выберите другое место.", ConsoleColor.Red);
- }
- }
- }
- }
- }
- }
- }
- private static Train CreateTrain()
- {
- string departurePoint = GetCity("Пункт отправления поезда: ");
- string destination = GetCity("Пункт назначения поезда: ");
- List<Wagon> wagons = CreateWagons();
- return new Train(departurePoint, destination, wagons);
- }
- private static List<Wagon> CreateWagons()
- {
- Random rand = new Random();
- int wagonCount = rand.Next(1, 6);
- List<Wagon> wagons = new List<Wagon>(wagonCount);
- for (int i = 0; i < wagonCount; i++)
- {
- int seatCount = rand.Next(20, 31);
- List<Seat> seats = new List<Seat>(seatCount);
- for (int j = 0; j < seatCount; j++)
- {
- int temp = rand.Next(0, 2);
- seats.Add(new Seat(j, temp == 0));
- }
- wagons.Add(new Wagon(i, seats));
- }
- return wagons;
- }
- private static string GetCity(string cityDescription)
- {
- Console.Write(cityDescription);
- Console.ForegroundColor = ConsoleColor.Green;
- string userInput = Console.ReadLine();
- Console.ForegroundColor = ConsoleColor.White;
- if (userInput != null)
- return userInput;
- throw new ArgumentException("Некорректное название города.");
- }
- private static void ShowActualInfo(Ticket ticket)
- {
- if (ticket == null)
- {
- Print("\nПоезд ещё не сформирован и не отправлен.\n", ConsoleColor.DarkRed);
- Console.Write("Нажмите любую клавишу для того, чтобы перейти к формированию поезда...");
- }
- else
- {
- Console.Write($"Маршрут: ");
- Print($"{ticket.Train.DeparturePoint} --> {ticket.Train.Destination}", ConsoleColor.Green);
- Console.Write("Ваш вагон: ");
- Print($"{ticket.WagonNumber}", ConsoleColor.Green);
- Console.Write("Ваше место: ");
- Print($"{ticket.SeatNumber}", ConsoleColor.Green);
- Console.Write("Нажмите любую клавишу для того, чтобы отправить поезд...");
- }
- Console.ReadKey();
- Console.Clear();
- }
- private static void Print(string text, ConsoleColor color = ConsoleColor.White)
- {
- ConsoleColor defaultColor = Console.ForegroundColor;
- Console.ForegroundColor = color;
- Console.WriteLine(text);
- Console.ForegroundColor = defaultColor;
- }
- }
- class Train
- {
- public string DeparturePoint { get; }
- public string Destination { get; }
- public int FreeSeats => CalculateFreeSeats();
- public int WagonsCount => _wagons.Count;
- private List<Wagon> _wagons;
- public Train(string departurePoint, string destination, List<Wagon> wagons)
- {
- DeparturePoint = departurePoint;
- Destination = destination;
- _wagons = wagons;
- }
- private int CalculateFreeSeats()
- {
- int freeSeats = 0;
- foreach (Wagon wagon in _wagons)
- {
- for (int i = 0; i < wagon.SeatsCount; i++)
- {
- if (wagon.GetSeat(i).IsFree)
- freeSeats++;
- }
- }
- return freeSeats;
- }
- public Wagon GetWagon(int index)
- {
- if (index < 0 || index >= _wagons.Count)
- throw new ArgumentException("Некорректный номер вагона.");
- return _wagons[index];
- }
- public void ShowTrain()
- {
- foreach (Wagon wagon in _wagons)
- wagon.ShowWagon();
- }
- }
- class Wagon
- {
- public int WagonNumber { get; }
- public int SeatsCount => _seats.Count;
- private List<Seat> _seats;
- public Wagon(int wagonNumber, List<Seat> seats)
- {
- WagonNumber = wagonNumber;
- _seats = seats;
- }
- public Seat GetSeat(int index)
- {
- if (index < 0 || index >= _seats.Count)
- throw new ArgumentException("Некорректный номер сиденья.");
- return _seats[index];
- }
- public void ShowWagon()
- {
- ConsoleColor defaultBackGroundColor = Console.BackgroundColor;
- ConsoleColor defaultForeGroundColor = Console.ForegroundColor;
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.WriteLine($"\nВагон №{WagonNumber + 1}\n");
- int top = Console.CursorTop;
- int left = Console.CursorLeft;
- for (int j = 0; j < SeatsCount; j++)
- {
- Seat seat = GetSeat(j);
- if (j != 0 && j % 4 == 0)
- {
- top++;
- left = 0;
- }
- else if (j != 0 && j % 2 == 0)
- left += 4;
- Console.SetCursorPosition(left, top);
- Console.BackgroundColor = ConvertSeatInfoToColor(seat);
- Console.WriteLine(seat.Number + 1);
- left += 3;
- }
- Console.BackgroundColor = defaultBackGroundColor;
- Console.ForegroundColor = defaultForeGroundColor;
- }
- private ConsoleColor ConvertSeatInfoToColor(Seat seat)
- {
- switch (seat.IsFree)
- {
- case false:
- return ConsoleColor.Red;
- case true:
- return ConsoleColor.Green;
- default:
- return ConsoleColor.Red;
- }
- }
- }
- class Seat
- {
- public int Number { get; }
- public bool IsFree { get; private set; }
- public Seat(int number, bool isFree)
- {
- Number = number;
- IsFree = isFree;
- }
- public void TakeSeat()
- {
- IsFree = false;
- }
- public void ClearSeat()
- {
- IsFree = true;
- }
- }
- class Ticket
- {
- public Train Train { get; }
- public int WagonNumber { get; }
- public int SeatNumber { get; }
- public Ticket(Train train, int wagonNumber, int seatNumber)
- {
- Train = train;
- WagonNumber = wagonNumber;
- SeatNumber = seatNumber;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement