illiden

TrainConfigurator

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