Advertisement
MaoChessy

Task 32 fix

Nov 11th, 2020 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CLight
  5. {
  6.     class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             List<Carriage> carriage = new List<Carriage>() {new Carriage(10, "Малый вагон"), new Carriage(20, "Средний вагон"), new Carriage(30, "Большой вагон") };
  11.             Depo depo = new Depo(carriage);
  12.             depo.Work();
  13.         }
  14.     }
  15.     public static class RandomStatic
  16.     {
  17.         static private Random _rand = new Random();
  18.         static public int GetNext(int min, int max)
  19.         {
  20.             return _rand.Next(min, max);
  21.  
  22.         }
  23.     }
  24.     public static class Messager
  25.     {
  26.         static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
  27.         {
  28.             ConsoleColor defaultColor = Console.ForegroundColor;
  29.             Console.ForegroundColor = color;
  30.             Console.WriteLine(message);
  31.             Console.ForegroundColor = defaultColor;
  32.             if (delay)
  33.                 Console.ReadKey();
  34.         }
  35.     }
  36.  
  37.     class Depo
  38.     {
  39.         private List<Carriage> _availableCarriages;
  40.         private Direction _direction;
  41.         public Depo(List<Carriage> availableCarriages)
  42.         {
  43.             _availableCarriages = availableCarriages;
  44.         }
  45.         public void Work()
  46.         {
  47.             bool isOpen = true;
  48.             while (isOpen)
  49.             {
  50.                 Console.Clear();
  51.                 if (_direction != null)
  52.                 {
  53.                     ShowInfo();
  54.                     Console.SetCursorPosition(0, 13);
  55.                     Console.WriteLine("1 - Продать билеты\n2 - Установить вагон \n3 - Удалить вагон\nEnter - Старт поезда\nESC - выход");
  56.                     ConsoleKeyInfo key = Console.ReadKey(true);
  57.                     switch (key.Key)
  58.                     {
  59.                         case ConsoleKey.D1:
  60.                             _direction.SellTicket();
  61.                             break;
  62.                         case ConsoleKey.D2:
  63.                             AddCarriage();
  64.                             break;
  65.                         case ConsoleKey.D3:
  66.                             RemoveCarriage();
  67.                             break;
  68.                         case ConsoleKey.Enter:
  69.                             if (_direction.Train.Start())
  70.                                 _direction = null;
  71.                             break;
  72.                         case ConsoleKey.Escape:
  73.                             isOpen = false;
  74.                             break;
  75.                     }
  76.                 }
  77.                 else
  78.                 {
  79.                     Messager.ShowMessageWithColor("Ещё нет созданного направления. \n\n\n", ConsoleColor.White, false);
  80.                     Console.WriteLine("Enter - Создать направление");
  81.                     switch (Console.ReadKey().Key)
  82.                     {
  83.                         case ConsoleKey.Enter:
  84.                             Messager.ShowMessageWithColor("Вы получили новое направление. Оформите его!", ConsoleColor.Green, true);
  85.                             _direction = new Direction();
  86.                             break;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         private void ShowInfo()
  92.         {
  93.             int xPositionOfMenuEditor = 0;
  94.             int xPositionOfTrain = 60;
  95.  
  96.             Console.SetCursorPosition(xPositionOfMenuEditor, 0);
  97.             Console.Write("\nДоступно вагонов:\n");
  98.             for (int i = 0; i < _availableCarriages.Count; i++)
  99.             {
  100.                 Messager.ShowMessageWithColor(i + ":" + _availableCarriages[i].GetInfo(), ConsoleColor.Blue, false);
  101.             }
  102.  
  103.             Console.SetCursorPosition(xPositionOfTrain, 0);
  104.             Console.WriteLine($"Направление {_direction.DirectionFrom} - {_direction.DirectionTo}");
  105.             Console.CursorLeft = xPositionOfTrain;
  106.             Console.WriteLine($"Вагонов в составе {_direction.Train.ListCarriage.Count}:");
  107.             for (int i = 0; i < _direction.Train.ListCarriage.Count; i++)
  108.             {
  109.                 Console.CursorLeft = xPositionOfTrain;
  110.                 Messager.ShowMessageWithColor(i + ":" + _direction.Train.ListCarriage[i].Name, ConsoleColor.Yellow, false);
  111.             }
  112.             Console.CursorLeft = xPositionOfTrain;
  113.             Messager.ShowMessageWithColor($"Пассажиров/Доступных мест {_direction.Train.Passenger}/{_direction.Train.GetAvailableSeats()}", ConsoleColor.White, false);
  114.         }
  115.         private void AddCarriage()
  116.         {
  117.             int index, amount;
  118.             index = ReadInt("Введите индекс вагона для добавления: ");
  119.             amount = ReadInt("Ввелите колличество добавляемых объектов: ");
  120.             for (int i = 0; i < amount; i++)
  121.             {
  122.                 _direction.Train.ListCarriage.Add(_availableCarriages[index]);
  123.             }
  124.             Messager.ShowMessageWithColor("Успешное добавление", ConsoleColor.Green, true);
  125.  
  126.         }
  127.         private void RemoveCarriage()
  128.         {
  129.             int index = ReadInt("Введите индекс вагона для удаления: ");
  130.             _direction.Train.ListCarriage.RemoveAt(index);
  131.             Messager.ShowMessageWithColor("Успешное удаление", ConsoleColor.Green, true);
  132.         }
  133.         private int ReadInt(string massege)
  134.         {
  135.             Console.Write(massege);
  136.             return Convert.ToInt32(Console.ReadLine());
  137.         }
  138.     }
  139.     class Direction
  140.     {
  141.         public string DirectionFrom { get; private set; } = "";
  142.         public string DirectionTo { get; private set; } = "";
  143.         public Train Train { get; private set; }
  144.         public Direction()
  145.         {
  146.             while (DirectionFrom == "" || DirectionTo == "")
  147.             {
  148.                 Console.Clear();
  149.                 Messager.ShowMessageWithColor("Введите направление откуда: ", ConsoleColor.White, false);
  150.                 DirectionFrom = Console.ReadLine();
  151.                 Messager.ShowMessageWithColor("Введите направление куда: ", ConsoleColor.White, false);
  152.                 DirectionTo = Console.ReadLine();
  153.                 if (DirectionFrom == "" || DirectionTo == "")
  154.                     Messager.ShowMessageWithColor("Было введено пустое направление. Введите ещё раз.", ConsoleColor.Red, true);
  155.             }
  156.             Train = new Train();
  157.         }
  158.         public void SellTicket()
  159.         {
  160.             Train.SetPassenger(RandomStatic.GetNext(50, 300));
  161.             Messager.ShowMessageWithColor("Билет проданы", ConsoleColor.Green, true);
  162.         }
  163.     }
  164.     class Train
  165.     {
  166.         public List<Carriage> ListCarriage { get; private set; } = new List<Carriage>();
  167.         public int Passenger { get; private set; }
  168.         public bool Start()
  169.         {
  170.             if (CheckPossibilityStart())
  171.             {
  172.                 Messager.ShowMessageWithColor("Поезд запущен!", ConsoleColor.Green, true);
  173.                 return true;
  174.             }
  175.             Console.ReadKey();
  176.             return false;
  177.         }
  178.         public int GetAvailableSeats()
  179.         {
  180.             int result = 0;
  181.             foreach (Carriage car in ListCarriage)
  182.             {
  183.                 result += car.AmountPassengers;
  184.             }
  185.             return result;
  186.         }
  187.         public void SetPassenger(int passenger)
  188.         {
  189.             Passenger = passenger;
  190.         }
  191.         private bool CheckPossibilityStart()
  192.         {
  193.             bool result = true;
  194.             if (Passenger > GetAvailableSeats())
  195.             {
  196.                 Messager.ShowMessageWithColor("Ошибка: мало мест для пассажиров", ConsoleColor.Red, false);
  197.                 result = false;
  198.             }
  199.             if (Passenger == 0)
  200.             {
  201.                 Messager.ShowMessageWithColor("Нету пассажиров", ConsoleColor.Red, false);
  202.                 result = false;
  203.             }
  204.             return result;
  205.         }
  206.     }
  207.     class Carriage
  208.     {
  209.         public string Name { get; private set; }
  210.         public int AmountPassengers { get; private set; }
  211.         public Carriage(int amountPassengers, string name)
  212.         {
  213.             AmountPassengers = amountPassengers;
  214.             Name = name;
  215.         }
  216.         public string GetInfo()
  217.         {
  218.             return $"{Name}. \nВмещает {AmountPassengers} пассажиров.";
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement