Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace CLight
- {
- class Program
- {
- public static void Main()
- {
- List<Carriage> carriage = new List<Carriage>() {new Carriage(10, "Малый вагон"), new Carriage(20, "Средний вагон"), new Carriage(30, "Большой вагон") };
- Depo depo = new Depo(carriage);
- depo.Work();
- }
- }
- public static class RandomStatic
- {
- static private Random _rand = new Random();
- static public int GetNext(int min, int max)
- {
- return _rand.Next(min, max);
- }
- }
- public static class Messager
- {
- static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
- {
- ConsoleColor defaultColor = Console.ForegroundColor;
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ForegroundColor = defaultColor;
- if (delay)
- Console.ReadKey();
- }
- }
- class Depo
- {
- private List<Carriage> _availableCarriages;
- private Direction _direction;
- public Depo(List<Carriage> availableCarriages)
- {
- _availableCarriages = availableCarriages;
- }
- public void Work()
- {
- bool isOpen = true;
- int step = 0;
- while (isOpen)
- {
- Console.Clear();
- if(step == 0)
- {
- Messager.ShowMessageWithColor("Ещё нет созданного направления. \n\n\n", ConsoleColor.White, false);
- Console.WriteLine("Enter - Создать направление \nEsc - выход");
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.Enter:
- Messager.ShowMessageWithColor("Вы получили новое направление. Оформите его!", ConsoleColor.Green, true);
- _direction = new Direction();
- step = 1;
- break;
- case ConsoleKey.Escape:
- isOpen = false;
- break;
- }
- }
- else if(step == 1)
- {
- Messager.ShowMessageWithColor("Направление есть. \n\n\n", ConsoleColor.White, false);
- Console.WriteLine("Enter - Создать поезд");
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.Enter:
- _direction.CreateTrain();
- Messager.ShowMessageWithColor("Вы создали поезд", ConsoleColor.Green, true);
- step = 2;
- break;
- }
- }
- else if (step == 2)
- {
- ShowInfo();
- Messager.ShowMessageWithColor("\n\n\nМеню продажи билетов", ConsoleColor.White, false);
- Console.WriteLine("Enter - Продать билеты");
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.Enter:
- _direction.SoldTicket();
- step = 3;
- break;
- }
- }
- else if (step == 3)
- {
- ShowInfo();
- Messager.ShowMessageWithColor("\n\nМеню создание вагонов поезда", ConsoleColor.White, false);
- Console.WriteLine("Enter - создать вагоны поезда");
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.Enter:
- _direction.Train.FormTrain(_availableCarriages,_direction.Passenger);
- Messager.ShowMessageWithColor("Вы сформировали состав", ConsoleColor.Green, true);
- step = 4;
- break;
- }
- }
- else if (step == 4)
- {
- ShowInfo();
- Messager.ShowMessageWithColor("\n\n\nМеню запуска поезда", ConsoleColor.White, false);
- Console.WriteLine("Enter - Запустить поезд");
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.Enter:
- _direction.Train.Start();
- step = 0;
- _direction = null;
- break;
- }
- }
- }
- }
- private void ShowInfo()
- {
- Console.WriteLine($"Направление {_direction.DirectionFrom} - {_direction.DirectionTo}");
- Messager.ShowMessageWithColor($"Вагоны в составе", ConsoleColor.Yellow, false);
- Console.WriteLine(_direction.Train.GetInfoCarriage());
- Messager.ShowMessageWithColor($"Пассажиров/Доступных мест {_direction.Passenger}/{_direction.Train.GetAvailableSeats()}", ConsoleColor.White, false);
- }
- }
- class Direction
- {
- public int Passenger { get; private set; }
- public string DirectionFrom { get; private set; } = "";
- public string DirectionTo { get; private set; } = "";
- public Train Train { get; private set; }
- public Direction()
- {
- while (DirectionFrom == "" || DirectionTo == "")
- {
- Console.Clear();
- Messager.ShowMessageWithColor("Введите направление откуда: ", ConsoleColor.White, false);
- DirectionFrom = Console.ReadLine();
- Messager.ShowMessageWithColor("Введите направление куда: ", ConsoleColor.White, false);
- DirectionTo = Console.ReadLine();
- if (DirectionFrom == "" || DirectionTo == "")
- Messager.ShowMessageWithColor("Было введено пустое направление. Введите ещё раз.", ConsoleColor.Red, true);
- }
- }
- public void CreateTrain()
- {
- Train = new Train();
- }
- public void SoldTicket()
- {
- Passenger = RandomStatic.GetNext(50, 300);
- Messager.ShowMessageWithColor("Билет проданы", ConsoleColor.Green, true);
- }
- }
- class Train
- {
- private List<Carriage> _listCarriage = new List<Carriage>();
- public void Start()
- {
- Messager.ShowMessageWithColor("Поезд запущен!", ConsoleColor.Green, true);
- }
- public int GetAvailableSeats()
- {
- int result = 0;
- foreach (Carriage car in _listCarriage)
- {
- result += car.AmountPassengers;
- }
- return result;
- }
- public string GetInfoCarriage()
- {
- string result = "";
- for (int i = 0; i < _listCarriage.Count; i++)
- {
- result += $"{i}:{_listCarriage[i].Name}\n";
- }
- return result;
- }
- public void FormTrain(List<Carriage> carriages, int passenger)
- {
- carriages.Sort(delegate (Carriage x, Carriage y)
- {
- if (x.AmountPassengers > y.AmountPassengers) return -1;
- else if (x.AmountPassengers < y.AmountPassengers) return 1;
- else return 0;
- });
- int index = 0;
- while (GetAvailableSeats() < passenger)
- {
- if (carriages[index].AmountPassengers <= passenger - GetAvailableSeats())
- _listCarriage.Add(carriages[index]);
- else
- index++;
- if (index >= carriages.Count)
- break;
- }
- if(passenger-GetAvailableSeats()>0)
- _listCarriage.Add(carriages[carriages.Count - 1]);
- }
- }
- class Carriage
- {
- public string Name { get; private set; }
- public int AmountPassengers { get; private set; }
- public Carriage(int amountPassengers, string name)
- {
- AmountPassengers = amountPassengers;
- Name = name;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement