Advertisement
TwinFrame

Clight_36_DossiersAdvanced

Jul 1st, 2023 (edited)
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         const ConsoleKey CommandAddDossier = ConsoleKey.F1;
  8.         const ConsoleKey CommandShowDossiers = ConsoleKey.F2;
  9.         const ConsoleKey CommandDeleteDossier = ConsoleKey.F3;
  10.         const ConsoleKey CommandExit = ConsoleKey.F4;
  11.  
  12.         List<string> dossiers = new List<string>();
  13.  
  14.         ConsoleKey userInput;
  15.         bool isContinue = true;
  16.  
  17.         while (isContinue)
  18.         {
  19.             Console.Clear();
  20.             Console.WriteLine($"{CommandAddDossier} - Добавить досье.");
  21.             Console.WriteLine($"{CommandShowDossiers} - Вывести все досье.");
  22.             Console.WriteLine($"{CommandDeleteDossier} - Удалить досье.");
  23.             Console.WriteLine($"{CommandExit} - Выход.");
  24.             userInput = Console.ReadKey().Key;
  25.  
  26.             switch (userInput)
  27.             {
  28.                 case CommandAddDossier:
  29.                     AddDossier(dossiers);
  30.                     break;
  31.  
  32.                 case CommandShowDossiers:
  33.                     ShowDossiers(dossiers);
  34.                     Console.ReadKey();
  35.                     break;
  36.  
  37.                 case CommandDeleteDossier:
  38.                     TryDeleteDossier(dossiers);
  39.                     break;
  40.  
  41.                 case CommandExit:
  42.                     isContinue = false;
  43.                     break;
  44.  
  45.                 default:
  46.                     Console.WriteLine("\nНе корректен ввод.");
  47.                     Console.ReadKey();
  48.                     break;
  49.             }
  50.         }
  51.     }
  52.  
  53.     static void AddDossier(List<string> dossiers)
  54.     {
  55.         string dossier = string.Empty;
  56.         char separator = '-';
  57.  
  58.         Console.Clear();
  59.  
  60.         dossier += Read("Введите фамилию", false);
  61.         dossier += Read("Введите имя");
  62.         dossier += Read("Введите отчество");
  63.         dossier += $" {separator} {Read("Введите должность", false)}";
  64.  
  65.         dossiers.Add(dossier);
  66.     }
  67.  
  68.     static string Read(string message, bool haveSeparator = true)
  69.     {
  70.         char separator = ' ';
  71.         string userInput = string.Empty;
  72.  
  73.         Console.Write($"{message}: ");
  74.  
  75.         if (haveSeparator)
  76.             userInput += separator;
  77.  
  78.         userInput += Console.ReadLine();
  79.  
  80.         return userInput;
  81.     }
  82.  
  83.     static void ShowDossiers(List<string> dossiers)
  84.     {
  85.         Console.Clear();
  86.  
  87.         if (IsEmptyDossier(dossiers))
  88.         {
  89.             Console.Write("Досье пустое.");
  90.         }
  91.         else
  92.         {
  93.             for (int i = 0; i < dossiers.Count; i++)
  94.             {
  95.                 Console.WriteLine($"{i + 1} - {dossiers[i]}");
  96.             }
  97.         }
  98.     }
  99.  
  100.     static void TryDeleteDossier(List<string> dossiers)
  101.     {
  102.         Console.Clear();
  103.  
  104.         if (IsEmptyDossier(dossiers))
  105.         {
  106.             Console.Write("Досье пустое.");
  107.             Console.ReadKey();
  108.         }
  109.         else
  110.         {
  111.             ShowDossiers(dossiers);
  112.  
  113.             Console.Write("Введите номер удаляемого пункта досье: ");
  114.             string userInput = Console.ReadLine();
  115.  
  116.             if (Int32.TryParse(userInput, out int number))
  117.             {
  118.                 if (number > 0 && number <= dossiers.Count)
  119.                 {
  120.                     dossiers.RemoveAt(number - 1);
  121.  
  122.                     Console.WriteLine("Досье удалено.");
  123.                 }
  124.                 else
  125.                 {
  126.                     Console.WriteLine("Такого пунтка нет.");
  127.                 }
  128.             }
  129.             else
  130.             {
  131.                 Console.WriteLine("Введено не число.");
  132.             }
  133.  
  134.             Console.ReadKey();
  135.         }
  136.     }
  137.  
  138.     static bool IsEmptyDossier(List<string> dossiers)
  139.     {
  140.         return dossiers.Count <= 0 || dossiers == null;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement