illiden

TrainConfigurator v2

Mar 27th, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.51 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TrainConfigurator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Configurator configurator = new Configurator();
  10.             configurator.Menu();
  11.         }
  12.     }
  13.  
  14.     class Configurator
  15.     {
  16.         private int _stage = 1;
  17.         private string _departure = " ";
  18.         private string _arrival = " ";
  19.         private int _passagers = 0;
  20.         private string _departTable = "";
  21.  
  22.         public void Menu(bool exit = false)
  23.         {
  24.             while (!exit)
  25.             {
  26.                 Console.Clear();
  27.                 Console.WriteLine("Добро пожаловать в конфигуратор пассажирских поездов!\n\n" + Table() +
  28.                                     "\n\nВыберите действие:");
  29.                 switch (_stage)
  30.                 {
  31.                     case 1:
  32.                         exit = DirectionStage();
  33.                         break;
  34.                     case 2:
  35.                         exit = TiketStage();
  36.                         break;
  37.                     case 3:
  38.                         exit = TrainStage();
  39.                         break;
  40.                     case 4:
  41.                         exit = DepartStage();
  42.                         break;
  43.                 }
  44.                 Console.ReadKey();
  45.             }
  46.         }
  47.  
  48.         public void Cancel()
  49.         {
  50.             _stage = 1;
  51.             _departure = " ";
  52.             _arrival = " ";
  53.             _passagers = 0;
  54.         }
  55.  
  56.         public void SetDepartTable(int places, int smallWagons, int mediumWagons, int largeWagons)
  57.         {
  58.             _departTable = $"По направлению \"{_departure} - {_arrival}\", " +
  59.                 $"рассчитанный на {places} мест " +
  60.                 $"и состоящий из \n{smallWagons} маленьких вагонов, {mediumWagons} средних вагонов и {largeWagons} больших вагонов " +
  61.                 $"отправился с {_passagers} пассажирами.\n" +
  62.                 $"В поезде осталось {places - _passagers} свободных мест";
  63.         }
  64.  
  65.         private void SetDeparture()
  66.         {
  67.             Console.Write("\nВведите место отправления: ");
  68.             _departure = Console.ReadLine();
  69.         }
  70.  
  71.         private void SetArrival()
  72.         {
  73.             Console.Write("\nВведите место назначения: ");
  74.             _arrival = Console.ReadLine();
  75.         }
  76.  
  77.         private string Table()
  78.         {
  79.             string direction = _departure + " - " + _arrival;
  80.             string table = $"Составляется поезд по направлению \"{direction}\"";
  81.             if (direction != "  -  ")
  82.             {
  83.                 if (_passagers > 0)
  84.                 {
  85.                     table += ", продано " + _passagers + " билетов";
  86.                     return table;
  87.                 }
  88.                 else
  89.                 {
  90.                     return table;
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 return "Направление поезда не задано";
  96.             }
  97.         }
  98.  
  99.         private void SellTikets()
  100.         {
  101.             Random random = new Random();
  102.             _passagers = random.Next(50, 2021);
  103.         }
  104.  
  105.         private bool IsSetDirection()
  106.         {
  107.             if (_departure == " " || _arrival == " ")
  108.             {
  109.                 return false;
  110.             }
  111.             else
  112.             {
  113.                 return true;
  114.             }
  115.         }
  116.  
  117.         private bool DirectionStage()
  118.         {
  119.             Console.WriteLine("1. Задать место отправления" +
  120.                             "\n2. Задать место назначения" +
  121.                             "\n3. Выход");
  122.             int input = Convert.ToInt32(Console.ReadLine());
  123.             switch (input)
  124.             {
  125.                 case 1:
  126.                     SetDeparture();
  127.                     break;
  128.                 case 2:
  129.                     SetArrival();
  130.                     break;
  131.                 case 3:
  132.                     return true;
  133.             }
  134.             if (IsSetDirection())
  135.             {
  136.                 _stage++;
  137.                 return false;
  138.             }
  139.             else
  140.             {
  141.                 return false;
  142.             }
  143.         }
  144.  
  145.         private bool TiketStage()
  146.         {
  147.             Console.WriteLine("1. Изменить место отправления" +
  148.                             "\n2. Изменить место назначения" +
  149.                             "\n3. Отмена поезда" +
  150.                             "\n4. Продать билеты" +
  151.                             "\n5. Выход");
  152.             int input = Convert.ToInt32(Console.ReadLine());
  153.             switch (input)
  154.             {
  155.                 case 1:
  156.                     SetDeparture();
  157.                     return false;
  158.                 case 2:
  159.                     SetArrival();
  160.                     return false;
  161.                 case 3:
  162.                     Cancel();
  163.                     return false;
  164.                 case 4:
  165.                     SellTikets();
  166.                     _stage++;
  167.                     return false;
  168.                 case 5:
  169.                     return true;
  170.                 default:
  171.                     return false;
  172.             }
  173.         }
  174.  
  175.         private bool TrainStage()
  176.         {
  177.             TrainConstructor trainConstructor = new TrainConstructor(_passagers, this);
  178.             trainConstructor.Menu(Table());
  179.             _stage++;
  180.             return false;
  181.         }
  182.  
  183.         private bool DepartStage()
  184.         {
  185.             Console.WriteLine("1. Отправить поезд" +
  186.                             "\n2. Отмена поезда" +
  187.                             "\n3. Выход");
  188.             int input = Convert.ToInt32(Console.ReadLine());
  189.             switch (input)
  190.             {
  191.                 case 1:
  192.                     Console.Clear();
  193.                     Console.WriteLine(_departTable);
  194.                     Cancel();
  195.                     Console.WriteLine("\nНажмите любую клавишу, чтобы составить еще один поезд или 1 - чтобы выйти");
  196.                     if (Console.ReadLine() == "1")
  197.                     {
  198.                         return true;
  199.                     }
  200.                     else
  201.                     {
  202.                         return false;
  203.                     }
  204.                 case 2:
  205.                     Cancel();
  206.                     return false;
  207.                 case 5:
  208.                     return true;
  209.                 default:
  210.                     return false;
  211.             }
  212.         }
  213.     }
  214.  
  215.     class TrainConstructor
  216.     {
  217.         private int _passagers;
  218.         private int _places = 0;
  219.         private int _smallWagonCount = 0;
  220.         private int _mediumWagonCount = 0;
  221.         private int _largeWagonCount = 0;
  222.         private Configurator _configurator;
  223.  
  224.         public TrainConstructor(int passagers, Configurator configurator)
  225.         {
  226.             _passagers = passagers;
  227.             _configurator = configurator;
  228.         }
  229.  
  230.         public void Menu(string table)
  231.         {
  232.             bool exit = false;
  233.             string fix;
  234.             while (!exit)
  235.             {
  236.                 Console.Clear();
  237.                 _places = _smallWagonCount * 50 + _mediumWagonCount * 100 + _largeWagonCount * 200;
  238.                 if (_places >= _passagers)
  239.                 {
  240.                     fix = "";
  241.                 }
  242.                 else
  243.                 {
  244.                     fix = "(невозможно, недостаточно мест)";
  245.                 }
  246.                 Console.WriteLine("Добро пожаловать в конфигуратор пассажирских поездов!\n\n" + table +
  247.                                     "\n\nСейчас в поезде " + _places + " мест" +
  248.                                     "\n\nСписок вагонов:" +
  249.                                     "\nМаленьких (на 50 пассажиров) - " + _smallWagonCount +
  250.                                     "\nСредних (на 100 пассажиров) - " + _mediumWagonCount +
  251.                                     "\nБольших (на 200 пассажиров) - " + _largeWagonCount +
  252.                                     "\n\nВыберите действие:" +
  253.                                     "\n1. Добавить вагоны" +
  254.                                     "\n2. Удалить вагоны" +
  255.                                     "\n3. Сбросить" +
  256.                                     "\n4. Отмена поезда" +
  257.                                     "\n5. Зафиксировать конфигурацию поезда" + fix +
  258.                                     "\n6. Выход");
  259.                 int input = Convert.ToInt32(Console.ReadLine());
  260.                 switch (input)
  261.                 {
  262.                     case 1:
  263.                         AddWagon();
  264.                         break;
  265.                     case 2:
  266.                         DeleteWagon();
  267.                         break;
  268.                     case 3:
  269.                         DeleteAllWagons();
  270.                         break;
  271.                     case 4:
  272.                         _configurator.Cancel();
  273.                         _configurator.Menu();
  274.                         break;
  275.                     case 5:
  276.                         if (_places >= _passagers)
  277.                         {
  278.                             exit = true;
  279.                             _configurator.SetDepartTable(_places, _smallWagonCount, _mediumWagonCount, _largeWagonCount);
  280.                             return;
  281.                         }
  282.                         else
  283.                         {
  284.                             Console.WriteLine("\nПассажирам недостаточно мест, добавьте больше вагонов");
  285.                         }
  286.                         break;
  287.                     case 6:
  288.                         exit = true;
  289.                         _configurator.Menu(true);
  290.                         break;
  291.                 }
  292.                 Console.ReadKey();
  293.             }
  294.         }
  295.  
  296.         private void AddWagon()
  297.         {
  298.             Console.Write("\nКакой вагон вы хотите добавить?");
  299.             ApplyWagon(1);
  300.         }
  301.  
  302.         private void DeleteWagon()
  303.         {
  304.             Console.Write("\nКакой вагон вы хотите удалить?");
  305.             ApplyWagon(-1);
  306.         }
  307.  
  308.         private void ApplyWagon(int count)
  309.         {
  310.             Console.WriteLine(" (1 - маленький, 2 - средний, 3 - большой)");
  311.             int input = Convert.ToInt32(Console.ReadLine());
  312.             switch (input)
  313.             {
  314.                 case 1:
  315.                     _smallWagonCount += count;
  316.                     break;
  317.                 case 2:
  318.                     _mediumWagonCount += count;
  319.                     break;
  320.                 case 3:
  321.                     _largeWagonCount += count;
  322.                     break;
  323.             }
  324.         }
  325.  
  326.         private void DeleteAllWagons()
  327.         {
  328.             _smallWagonCount = 0;
  329.             _mediumWagonCount = 0;
  330.             _largeWagonCount = 0;
  331.         }
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment