Advertisement
MaoChessy

Task 32 - fix 3

Nov 11th, 2020
147
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.     class Depo
  37.     {
  38.         private List<Carriage> _availableCarriages;
  39.         private Direction _direction;
  40.         public Depo(List<Carriage> availableCarriages)
  41.         {
  42.             _availableCarriages = availableCarriages;
  43.         }
  44.         public void Work()
  45.         {
  46.             bool isOpen = true;
  47.             int step = 0;
  48.             while (isOpen)
  49.             {
  50.                 Console.Clear();
  51.                 if(step == 0)
  52.                 {
  53.                     Messager.ShowMessageWithColor("Ещё нет созданного направления. \n\n\n", ConsoleColor.White, false);
  54.                     Console.WriteLine("Enter - Создать направление \nEsc - выход");
  55.                     switch (Console.ReadKey().Key)
  56.                     {
  57.                         case ConsoleKey.Enter:
  58.                             Messager.ShowMessageWithColor("Вы получили новое направление. Оформите его!", ConsoleColor.Green, true);
  59.                             _direction = new Direction();
  60.                             step = 1;
  61.                             break;
  62.                         case ConsoleKey.Escape:
  63.                             isOpen = false;
  64.                             break;
  65.                     }
  66.                 }
  67.                 else if(step == 1)
  68.                 {
  69.                     Messager.ShowMessageWithColor("Направление есть. \n\n\n", ConsoleColor.White, false);
  70.                     Console.WriteLine("Enter - Создать поезд");
  71.                     switch (Console.ReadKey().Key)
  72.                     {
  73.                         case ConsoleKey.Enter:
  74.                             _direction.CreateTrain();
  75.                             Messager.ShowMessageWithColor("Вы  создали поезд", ConsoleColor.Green, true);
  76.                             step = 2;
  77.                             break;
  78.                     }
  79.                 }
  80.                 else if (step == 2)
  81.                 {
  82.                     ShowInfo();
  83.                     Messager.ShowMessageWithColor("\n\n\nМеню продажи билетов", ConsoleColor.White, false);
  84.                     Console.WriteLine("Enter - Продать билеты");
  85.                     switch (Console.ReadKey().Key)
  86.                     {
  87.                         case ConsoleKey.Enter:
  88.                             _direction.SellTickets();
  89.                             step = 3;
  90.                             break;
  91.                     }
  92.                 }
  93.                 else if (step == 3)
  94.                 {
  95.                     ShowInfo();
  96.                     Messager.ShowMessageWithColor("\n\nМеню создание вагонов поезда", ConsoleColor.White, false);
  97.                     Console.WriteLine("Enter - создать вагоны поезда");
  98.                     switch (Console.ReadKey().Key)
  99.                     {
  100.                         case ConsoleKey.Enter:
  101.                             _direction.Train.FormTrain(_availableCarriages,_direction.Passenger);
  102.                             Messager.ShowMessageWithColor("Вы сформировали состав", ConsoleColor.Green, true);
  103.                             step = 4;
  104.                             break;
  105.                     }
  106.                 }
  107.                 else if (step == 4)
  108.                 {
  109.                     ShowInfo();
  110.                     Messager.ShowMessageWithColor("\n\n\nМеню запуска поезда", ConsoleColor.White, false);
  111.                     Console.WriteLine("Enter - Запустить поезд");
  112.                     switch (Console.ReadKey().Key)
  113.                     {
  114.                         case ConsoleKey.Enter:
  115.                             _direction.Train.Start();
  116.                             step = 0;
  117.                             _direction = null;
  118.                             break;
  119.                        
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.         private void ShowInfo()
  125.         {
  126.             Console.WriteLine($"Направление {_direction.DirectionFrom} - {_direction.DirectionTo}");
  127.             Messager.ShowMessageWithColor($"Вагоны в составе", ConsoleColor.Yellow, false);
  128.             Console.WriteLine(_direction.Train.GetInfoCarriage());
  129.             Messager.ShowMessageWithColor($"Пассажиров/Доступных мест {_direction.Passenger}/{_direction.Train.GetAvailableSeats()}", ConsoleColor.White, false);
  130.         }
  131.     }
  132.     class Direction
  133.     {
  134.         public int Passenger { get; private set; }
  135.         public string DirectionFrom { get; private set; } = "";
  136.         public string DirectionTo { get; private set; } = "";
  137.         public Train Train { get; private set; }
  138.         public Direction()
  139.         {
  140.             while (DirectionFrom == "" || DirectionTo == "")
  141.             {
  142.                 Console.Clear();
  143.                 Messager.ShowMessageWithColor("Введите направление откуда: ", ConsoleColor.White, false);
  144.                 DirectionFrom = Console.ReadLine();
  145.                 Messager.ShowMessageWithColor("Введите направление куда: ", ConsoleColor.White, false);
  146.                 DirectionTo = Console.ReadLine();
  147.                 if (DirectionFrom == "" || DirectionTo == "")
  148.                     Messager.ShowMessageWithColor("Было введено пустое направление. Введите ещё раз.", ConsoleColor.Red, true);
  149.             }
  150.         }
  151.         public void CreateTrain()
  152.         {
  153.             Train = new Train();
  154.         }
  155.         public void SellTickets()
  156.         {
  157.             Passenger = RandomStatic.GetNext(50, 300);
  158.             Messager.ShowMessageWithColor("Билет проданы", ConsoleColor.Green, true);
  159.         }
  160.     }
  161.     class Train
  162.     {
  163.        
  164.         private List<Carriage> _listCarriage = new List<Carriage>();
  165.         public void Start()
  166.         {
  167.             Messager.ShowMessageWithColor("Поезд запущен!", ConsoleColor.Green, true);
  168.         }
  169.         public int GetAvailableSeats()
  170.         {
  171.             int result = 0;
  172.             foreach (Carriage car in _listCarriage)
  173.             {
  174.                 result += car.AmountPassengers;
  175.             }
  176.             return result;
  177.         }
  178.         public string GetInfoCarriage()
  179.         {
  180.             string result = "";
  181.             for (int i = 0; i < _listCarriage.Count; i++)
  182.             {
  183.                 result += $"{i}:{_listCarriage[i].Name}\n";
  184.             }
  185.             return result;
  186.         }
  187.         public void FormTrain(List<Carriage> typeCarriages, int amountPassenger)
  188.         {
  189.             typeCarriages.Sort(delegate (Carriage x, Carriage y)
  190.             {
  191.                 if (x.AmountPassengers > y.AmountPassengers) return -1;
  192.                 else if (x.AmountPassengers < y.AmountPassengers) return 1;
  193.                 else return 0;
  194.             });
  195.             int index = 0;
  196.             while (GetAvailableSeats() < amountPassenger)
  197.             {
  198.                 if (typeCarriages[index].AmountPassengers <= amountPassenger - GetAvailableSeats())
  199.                     _listCarriage.Add(typeCarriages[index]);
  200.                 else
  201.                     index++;
  202.                 if (index >= typeCarriages.Count)
  203.                     break;
  204.             }
  205.             if(amountPassenger-GetAvailableSeats()>0)
  206.                 _listCarriage.Add(typeCarriages[typeCarriages.Count - 1]);
  207.         }
  208.     }
  209.     class Carriage
  210.     {
  211.         public string Name { get; private set; }
  212.         public int AmountPassengers { get; private set; }
  213.         public Carriage(int amountPassengers, string name)
  214.         {
  215.             AmountPassengers = amountPassengers;
  216.             Name = name;
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement