Advertisement
holllowknight

ДЗ: Кадры

Mar 18th, 2020 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.48 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Kadr
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const string CommandAdd = "add";
  10.             const string CommandPrint = "print";
  11.             const string CommandDelete = "delete";
  12.             const string CommandFind = "find";
  13.             const string CommandExit = "exit";
  14.  
  15.             string[] fullNames = new string[0];
  16.             string[] positions = new string[0];
  17.             string userInput;
  18.             bool isRunning = true;
  19.  
  20.             while (isRunning)
  21.             {
  22.                 Console.Clear();
  23.                 Console.WriteLine($"{CommandAdd} - добавить досье\n{CommandPrint} - распечатать досье\n{CommandDelete} - удалить досье");
  24.                 Console.WriteLine($"{CommandFind} - найти номер досье по фамилии\n{CommandExit} - выйти из программы");
  25.                 Console.WriteLine();
  26.                 userInput = Console.ReadLine();
  27.  
  28.                 switch (userInput)
  29.                 {
  30.                     case CommandAdd:
  31.                         AddFile(ref fullNames, ref positions);
  32.                         break;
  33.  
  34.                     case CommandPrint:
  35.                         PrintFiles(fullNames, positions);
  36.                         break;
  37.  
  38.                     case CommandDelete:
  39.                         DeleteFile(ref fullNames, ref positions);
  40.                         break;
  41.  
  42.                     case CommandFind:
  43.                         FindFiles(fullNames, positions);
  44.                         break;
  45.  
  46.                     case CommandExit:
  47.                         isRunning = false;
  48.                         break;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         static void AddFile(ref string[] fullNames, ref string[] positions)
  54.         {
  55.             string fullName;
  56.             string position;
  57.  
  58.             Console.WriteLine("Введите ФИО");
  59.             fullName = Console.ReadLine();
  60.             Console.WriteLine("Введите должность");
  61.             position = Console.ReadLine();
  62.             fullNames = AddInArray(fullNames, fullName);
  63.             positions = AddInArray(positions, position);
  64.             Console.WriteLine("Количество досье в архиве: " + fullNames.Length);
  65.             Console.ReadKey();
  66.         }
  67.  
  68.         static void PrintFiles(string[] fullNames, string[] positions)
  69.         {
  70.             if (fullNames.Length == 0)
  71.             {
  72.                 Console.WriteLine("В досье отсутствуют записи");
  73.             }
  74.             else
  75.             {
  76.                 for (int i = 0; i < fullNames.Length; i++)
  77.                     Console.WriteLine($"{i + 1} - {fullNames[i]} - {positions[i]}");
  78.             }
  79.  
  80.             Console.ReadKey();
  81.         }
  82.  
  83.         static void DeleteFile(ref string[] fullNames, ref string[] positions)
  84.         {
  85.             Console.Write("Введите номер удаляемой записи ");
  86.             int fileIndex = Convert.ToInt32(Console.ReadLine()) - 1;
  87.  
  88.             if (fileIndex >= fullNames.Length)
  89.             {
  90.                 Console.WriteLine("Досье с данным номером отсутствует");
  91.             }
  92.             else
  93.             {
  94.                 fullNames = DeleteFromArray(fullNames, fileIndex);
  95.                 positions = DeleteFromArray(positions, fileIndex);
  96.             }
  97.  
  98.             Console.WriteLine("Количество досье в архиве: " + fullNames.Length);
  99.             Console.ReadKey();
  100.         }
  101.  
  102.         static void FindFiles(string[] fullNames, string[] positions)
  103.         {
  104.             Console.Write("Введите искомую фамилию ");
  105.             string surnameRequest = Console.ReadLine();
  106.             string foundEntries = "";
  107.             int foundEntryCount = 0;
  108.  
  109.             for (int i = 0; i < fullNames.Length; i++)
  110.             {
  111.                 string surname = fullNames[i].Split(' ')[0];
  112.  
  113.                 if (surnameRequest == surname)
  114.                 {
  115.                     foundEntryCount++;
  116.                     foundEntries += string.Format($"{i} ");
  117.                 }
  118.             }
  119.  
  120.             if (foundEntryCount == 0)
  121.             {
  122.                 Console.WriteLine("Досье отсутствует");
  123.             }
  124.             else
  125.             {
  126.                 Console.WriteLine("Фамилия встречается в досье: " + foundEntries);
  127.             }
  128.  
  129.             Console.ReadKey();
  130.         }
  131.  
  132.         static string[] AddInArray(string[] array, string value)
  133.         {
  134.             string[] temporaryArray = new string[array.Length + 1];
  135.  
  136.             for (int i = 0; i < array.Length; i++)
  137.             {
  138.                 temporaryArray[i] = array[i];
  139.             }
  140.  
  141.             temporaryArray[array.Length] = value;
  142.             return temporaryArray;
  143.         }
  144.  
  145.         static string[] DeleteFromArray(string[] array, int index)
  146.         {
  147.             if(array.Length == 0)
  148.             {
  149.                 return array;
  150.             }
  151.  
  152.             string[] temporaryArray = new string[array.Length - 1];
  153.  
  154.             for (int i = 0; i < index; i++)
  155.             {
  156.                 temporaryArray[i] = array[i];
  157.             }
  158.  
  159.             for (int i = index; i < temporaryArray.Length; i++)
  160.             {
  161.                 temporaryArray[i] = array[i + 1];
  162.             }
  163.  
  164.             return temporaryArray;
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement