lovelyvook

Unit_30

Jun 30th, 2024 (edited)
738
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 1 0
  1. using System;
  2.  
  3. namespace Ijunior
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const string CommandAddDossier = "1";
  10.             const string CommandShowDossier = "2";
  11.             const string CommandRemoveDossier = "3";
  12.             const string CommandSearchDossier = "4";
  13.             const string CommandExit = "5";
  14.  
  15.             bool isWork = true;
  16.             string userInput;
  17.             string[] names = new string[0];
  18.             string[] positions = new string[0];
  19.  
  20.             while (isWork)
  21.             {
  22.                 Console.Write($"{CommandAddDossier} - добавить досье" +
  23.                 $"\n{CommandShowDossier} - посмотреть все досье" +
  24.                 $"\n{CommandRemoveDossier} - удалить досье" +
  25.                 $"\n{CommandSearchDossier} - найти досье" +
  26.                 $"\n{CommandExit} - выход" +
  27.                 $"\nВведите команду: ");
  28.  
  29.                 userInput = Console.ReadLine();
  30.                 Console.Clear();
  31.  
  32.                 switch (userInput)
  33.                 {
  34.                     case CommandAddDossier:
  35.                         AddDossier(ref names, ref positions);
  36.                         break;
  37.                     case CommandShowDossier:
  38.                         ShowDossier(names, positions);
  39.                         break;
  40.                     case CommandRemoveDossier:
  41.                         RemoveDossier(ref names, ref positions);
  42.                         break;
  43.                     case CommandSearchDossier:
  44.                         SearchDossier(names, positions);
  45.                         break;
  46.                     case CommandExit:
  47.                         isWork = false;
  48.                         break;
  49.                     default:
  50.                         Console.WriteLine("Некорректный запрос");
  51.                         break;
  52.                 }
  53.  
  54.                 Console.ReadKey();
  55.                 Console.Clear();
  56.             }
  57.         }
  58.  
  59.         static void AddDossier(ref string[] names, ref string[] positions)
  60.         {
  61.             Console.Write("Введите ФИО сотрудника: ");
  62.             string name = Console.ReadLine();
  63.             names = ExpandArray(names, name);
  64.  
  65.             Console.Write("Введите должность сотрудника: ");
  66.             string position = Console.ReadLine();
  67.             positions = ExpandArray(positions, position);
  68.         }
  69.  
  70.         static string[] ExpandArray(string[] array, string value)
  71.         {
  72.             string[] expandedArray = new string[array.Length + 1];
  73.  
  74.             for (int i = 0; i < array.Length; i++)
  75.             {
  76.                 expandedArray[i] = array[i];
  77.             }
  78.  
  79.             expandedArray[expandedArray.Length - 1] = value;
  80.             return expandedArray;
  81.         }
  82.  
  83.         static void ShowDossier(string[] names, string[] positions)
  84.         {
  85.             if (IsArrayFull(names))
  86.             {
  87.                 for (int i = 0; i < names.Length; i++)
  88.                 {
  89.                     Console.WriteLine(i + 1 + " - " + names[i] + " - " + positions[i]);
  90.                 }
  91.             }
  92.         }
  93.  
  94.         static bool IsArrayFull(string[] array)
  95.         {
  96.             if (array.Length > 0)
  97.             {
  98.                 return true;
  99.             }
  100.  
  101.             Console.WriteLine("Список пуст");
  102.             return false;
  103.         }
  104.  
  105.         static void RemoveDossier(ref string[] names, ref string[] positions)
  106.         {
  107.             ShowDossier(names, positions);
  108.  
  109.             if (IsArrayFull(names))
  110.             {
  111.                 Console.Write("Выберите номер для удаления: ");
  112.                 string userInput = Console.ReadLine();
  113.  
  114.                 if (int.TryParse(userInput, out int index))
  115.                 {
  116.                     if (index > 0 && index <= names.Length)
  117.                     {
  118.                         index--;
  119.                         names = ReduceArray(names, index);
  120.                         positions = ReduceArray(positions, index);
  121.                     }
  122.                     else
  123.                     {
  124.                         Console.WriteLine("Некорректный ввод");
  125.                     }
  126.                 }
  127.                 else
  128.                 {
  129.                     Console.WriteLine("Некорректный ввод");
  130.                 }
  131.             }
  132.         }
  133.  
  134.         static string[] ReduceArray(string[] array, int index)
  135.         {
  136.             string[] reducedArray = new string[array.Length - 1];
  137.  
  138.             for (int i = 0; i < index; i++)
  139.             {
  140.                 reducedArray[i] = array[i];
  141.             }
  142.  
  143.             index++;
  144.  
  145.             for (int i = index; i < array.Length; i++)
  146.             {
  147.                 reducedArray[i - 1] = array[i];
  148.             }
  149.  
  150.             return reducedArray;
  151.         }
  152.  
  153.         static void SearchDossier(string[] names, string[] positions)
  154.         {
  155.             if (IsArrayFull(names))
  156.             {
  157.                 char separator = ' ';
  158.                 bool haveLastName = true;
  159.  
  160.                 Console.Write("Введите фамилию: ");
  161.                 string lastName = Console.ReadLine();
  162.  
  163.                 for (int i = 0; i < names.Length; i++)
  164.                 {
  165.                     string[] separatedName = names[i].Split(separator);
  166.  
  167.                     if (separatedName[0].ToLower() == lastName.ToLower())
  168.                     {
  169.                         Console.WriteLine(i + 1 + " - " + names[i] + " - " + positions[i]);
  170.                         haveLastName = false;
  171.                     }
  172.                 }
  173.  
  174.                 if (haveLastName)
  175.                 {
  176.                     Console.WriteLine("Досье не найдено");
  177.                 }
  178.             }
  179.         }
  180.     }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment