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;
- while (isOpen)
- {
- Console.Clear();
- if (_direction != null)
- {
- ShowInfo();
- Console.SetCursorPosition(0, 13);
- Console.WriteLine("1 - Продать билеты\n2 - Установить вагон \n3 - Удалить вагон\nEnter - Старт поезда\nESC - выход");
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.D1:
- _direction.SellTicket();
- break;
- case ConsoleKey.D2:
- AddCarriage();
- break;
- case ConsoleKey.D3:
- RemoveCarriage();
- break;
- case ConsoleKey.Enter:
- if (_direction.Train.Start())
- _direction = null;
- break;
- case ConsoleKey.Escape:
- isOpen = false;
- break;
- }
- }
- else
- {
- Messager.ShowMessageWithColor("Ещё нет созданного направления. \n\n\n", ConsoleColor.White, false);
- Console.WriteLine("Enter - Создать направление");
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.Enter:
- Messager.ShowMessageWithColor("Вы получили новое направление. Оформите его!", ConsoleColor.Green, true);
- _direction = new Direction();
- break;
- }
- }
- }
- }
- private void ShowInfo()
- {
- int xPositionOfMenuEditor = 0;
- int xPositionOfTrain = 60;
- Console.SetCursorPosition(xPositionOfMenuEditor, 0);
- Console.Write("\nДоступно вагонов:\n");
- for (int i = 0; i < _availableCarriages.Count; i++)
- {
- Messager.ShowMessageWithColor(i + ":" + _availableCarriages[i].GetInfo(), ConsoleColor.Blue, false);
- }
- Console.SetCursorPosition(xPositionOfTrain, 0);
- Console.WriteLine($"Направление {_direction.DirectionFrom} - {_direction.DirectionTo}");
- Console.CursorLeft = xPositionOfTrain;
- Console.WriteLine($"Вагонов в составе {_direction.Train.ListCarriage.Count}:");
- for (int i = 0; i < _direction.Train.ListCarriage.Count; i++)
- {
- Console.CursorLeft = xPositionOfTrain;
- Messager.ShowMessageWithColor(i + ":" + _direction.Train.ListCarriage[i].Name, ConsoleColor.Yellow, false);
- }
- Console.CursorLeft = xPositionOfTrain;
- Messager.ShowMessageWithColor($"Пассажиров/Доступных мест {_direction.Train.Passenger}/{_direction.Train.GetAvailableSeats()}", ConsoleColor.White, false);
- }
- private void AddCarriage()
- {
- int index, amount;
- index = ReadInt("Введите индекс вагона для добавления: ");
- amount = ReadInt("Ввелите колличество добавляемых объектов: ");
- for (int i = 0; i < amount; i++)
- {
- _direction.Train.ListCarriage.Add(_availableCarriages[index]);
- }
- Messager.ShowMessageWithColor("Успешное добавление", ConsoleColor.Green, true);
- }
- private void RemoveCarriage()
- {
- int index = ReadInt("Введите индекс вагона для удаления: ");
- _direction.Train.ListCarriage.RemoveAt(index);
- Messager.ShowMessageWithColor("Успешное удаление", ConsoleColor.Green, true);
- }
- private int ReadInt(string massege)
- {
- Console.Write(massege);
- return Convert.ToInt32(Console.ReadLine());
- }
- }
- class Direction
- {
- 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);
- }
- Train = new Train();
- }
- public void SellTicket()
- {
- Train.SetPassenger(RandomStatic.GetNext(50, 300));
- Messager.ShowMessageWithColor("Билет проданы", ConsoleColor.Green, true);
- }
- }
- class Train
- {
- public List<Carriage> ListCarriage { get; private set; } = new List<Carriage>();
- public int Passenger { get; private set; }
- public bool Start()
- {
- if (CheckPossibilityStart())
- {
- Messager.ShowMessageWithColor("Поезд запущен!", ConsoleColor.Green, true);
- return true;
- }
- Console.ReadKey();
- return false;
- }
- public int GetAvailableSeats()
- {
- int result = 0;
- foreach (Carriage car in ListCarriage)
- {
- result += car.AmountPassengers;
- }
- return result;
- }
- public void SetPassenger(int passenger)
- {
- Passenger = passenger;
- }
- private bool CheckPossibilityStart()
- {
- bool result = true;
- if (Passenger > GetAvailableSeats())
- {
- Messager.ShowMessageWithColor("Ошибка: мало мест для пассажиров", ConsoleColor.Red, false);
- result = false;
- }
- if (Passenger == 0)
- {
- Messager.ShowMessageWithColor("Нету пассажиров", ConsoleColor.Red, false);
- result = false;
- }
- return result;
- }
- }
- class Carriage
- {
- public string Name { get; private set; }
- public int AmountPassengers { get; private set; }
- public Carriage(int amountPassengers, string name)
- {
- AmountPassengers = amountPassengers;
- Name = name;
- }
- public string GetInfo()
- {
- return $"{Name}. \nВмещает {AmountPassengers} пассажиров.";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement