kwest87

Untitled

Jan 20th, 2024
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.00 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp28
  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 CommandDeleteDossier = "3";
  12.             const string CommandSearchName = "4";
  13.             const string CommandExitProgram = "5";
  14.  
  15.             string[] names = new string[0];
  16.             string[] positions = new string[0];
  17.             string userInput;
  18.             bool isWork = true;
  19.  
  20.             while (isWork)
  21.             {
  22.                 Console.WriteLine("        МЕНЮ");
  23.                 Console.WriteLine(
  24.                     " 1) Добавить досье.\n" +
  25.                     " 2) Показать досье.\n" +
  26.                     " 3) Удалить досье.\n" +
  27.                     " 4) Поиск по фамилии.\n" +
  28.                     " 5) Выход.");
  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 CommandDeleteDossier:
  41.                         DeleteDossier(ref names, ref positions);
  42.                         break;
  43.                     case CommandSearchName:
  44.                         SearchName(names, positions);
  45.                         break;
  46.                     case CommandExitProgram:
  47.                         isWork = false;
  48.                         break;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         static void AddElement(ref string[] array, string text)
  54.         {
  55.             Console.Write(text);
  56.             string userInput = Console.ReadLine();
  57.             string[] temporaryArray = new string[array.Length + 1];
  58.  
  59.             for (int i = 0; i < array.Length; i++)
  60.             {
  61.                 temporaryArray[i] = array[i];
  62.             }
  63.  
  64.             temporaryArray[temporaryArray.Length - 1] = userInput;
  65.             array = temporaryArray;
  66.         }
  67.  
  68.         static void AddDossier(ref string[] names, ref string[] positions)
  69.         {
  70.             AddElement(ref names, "Укажите ваше ФИО :");
  71.             AddElement(ref positions, "Укажите вашу должность :");
  72.         }
  73.  
  74.         static void ShowDossier(string[] names, string[] positions)
  75.         {
  76.             for (int i = 0; i < names.Length; i++)
  77.             {
  78.                 ShowElement(i, names[i], positions[i]);
  79.             }
  80.         }
  81.  
  82.         static void DeleteElement(ref string[] array, int index)
  83.         {
  84.             string[] tempArray = new string[array.Length - 1];
  85.  
  86.             for (int i = 0; i < index - 1; i++)
  87.             {
  88.                 tempArray[i] = array[i];
  89.             }
  90.             for (int i = index - 1; i < tempArray.Length; i++)
  91.             {
  92.                 tempArray[i] = array[i + 1];
  93.             }
  94.  
  95.             array = tempArray;
  96.         }
  97.  
  98.         static void DeleteDossier(ref string[] names, ref string[] positions)
  99.         {
  100.             Console.Write("Укажите номер досье которое нужно удалить : ");
  101.             string userInput = Console.ReadLine();
  102.             bool isNumber = int.TryParse(userInput, out int numberValue);
  103.  
  104.             if (isNumber && numberValue <= names.Length)
  105.             {
  106.                 DeleteElement(ref names, numberValue);
  107.                 DeleteElement(ref positions, numberValue);
  108.                 Console.WriteLine($"Досье {userInput} удалено.");
  109.             }
  110.             else
  111.             {
  112.                 Console.WriteLine($"Досье {userInput} нету.");
  113.             }
  114.         }
  115.  
  116.         static void SearchName(string[] names, string[] positions)
  117.         {
  118.             Console.Write("Введите фамилию : ");
  119.             string userInput = Console.ReadLine();
  120.             string[] nameElements;
  121.             bool isFound = false;
  122.  
  123.             for (int i = 0; i < names.Length; i++)
  124.             {
  125.                 nameElements = names[i].Split(' ');
  126.  
  127.                 for (int j = 0; j < nameElements.Length; j++)
  128.                 {
  129.                     if (nameElements[j].ToLower() == userInput.ToLower())
  130.                     {
  131.                         Console.Write("Найден : ");
  132.                         ShowElement(i, names[i], positions[i]);
  133.                         isFound = true;
  134.                     }
  135.                 }
  136.             }
  137.  
  138.             if (isFound == false)
  139.             {
  140.                 Console.WriteLine("Ничего не найдено.");
  141.             }
  142.         }
  143.  
  144.         static void ShowElement(int index ,string name , string position)
  145.         {
  146.             Console.WriteLine($"{index + 1}) {name} - {position}.");
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment