Advertisement
Rodunskiy

Untitled

May 10th, 2025
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.87 KB | None | 0 0
  1. namespace CSLight
  2. {
  3.     public class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             const string AddDossierCommand = "1";
  8.             const string ShowAllDossierCommand = "2";
  9.             const string SearchLastnameCommand = "3";
  10.             const string DeleteDossierCommand = "4";
  11.             const string ExitProgrammCommand = "5";
  12.  
  13.             Dictionary<string, List<string>> positions = new Dictionary<string, List<string>>();
  14.             bool isRunning = true;
  15.  
  16.             while (isRunning)
  17.             {
  18.                 Console.WriteLine(
  19.                 $"{AddDossierCommand}) Добавить досье\n" +
  20.                 $"{ShowAllDossierCommand}) Вывести все досье\n" +
  21.                 $"{SearchLastnameCommand}) Поиск по фамилии\n" +
  22.                 $"{DeleteDossierCommand}) Удалить досье\n" +
  23.                 $"{ExitProgrammCommand}) Выход из программы\n");
  24.  
  25.                 switch (Console.ReadLine())
  26.                 {
  27.                     case AddDossierCommand:
  28.                         AddDossier(ref positions);
  29.                         break;
  30.  
  31.                     case ShowAllDossierCommand:
  32.                         ShowAllDossier(positions);
  33.                         break;
  34.  
  35.                     case SearchLastnameCommand:
  36.                         SearchLastname(positions);
  37.                         break;
  38.  
  39.                     case DeleteDossierCommand:
  40.                         DeleteDossier(ref positions);
  41.                         break;
  42.  
  43.                     case ExitProgrammCommand:
  44.                         isRunning = false;
  45.                         break;
  46.                 }
  47.             }
  48.         }
  49.  
  50.         static void AddDossier(ref Dictionary<string, List<string>> positions)
  51.         {
  52.             Console.WriteLine("Введите ФИО сотрудника:");
  53.             string fullName = Console.ReadLine();
  54.  
  55.             Console.WriteLine("Введите должность сотрудника:");
  56.             string position = Console.ReadLine();
  57.  
  58.             if (positions.ContainsKey(position))
  59.             {
  60.                 positions[position].Add(fullName);
  61.             }
  62.             else
  63.             {
  64.                 positions[position] = new List<string> { fullName };
  65.             }
  66.  
  67.             Console.WriteLine("Досье добавлено!");
  68.         }
  69.  
  70.         static void ShowAllDossier(Dictionary<string, List<string>> positions)
  71.         {
  72.             if (positions.Count == 0)
  73.             {
  74.                 Console.WriteLine("Досье отсутствуют.");
  75.                 return;
  76.             }
  77.  
  78.             int index = 1;
  79.             foreach (var position in positions)
  80.             {
  81.                 Console.WriteLine($"Должность: {position.Key}");
  82.  
  83.                 foreach (var employee in position.Value)
  84.                 {
  85.                     Console.WriteLine($"{index++}) {employee}");
  86.                 }
  87.                 Console.WriteLine();
  88.             }
  89.         }
  90.  
  91.         static void SearchLastname(Dictionary<string, List<string>> positions)
  92.         {
  93.             Console.WriteLine("Введите нужную фамилию для поиска:");
  94.             string lastName = Console.ReadLine();
  95.             bool found = false;
  96.  
  97.             foreach (var position in positions)
  98.             {
  99.                 foreach (var employee in position.Value)
  100.                 {
  101.                     string[] names = employee.Split(' ');
  102.                     if (names[0].Equals(lastName, StringComparison.OrdinalIgnoreCase))
  103.                     {
  104.                         Console.WriteLine($"{employee} - {position.Key}");
  105.                         found = true;
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             if (!found)
  111.             {
  112.                 Console.WriteLine($"Сотрудники с фамилией '{lastName}' не найдены.");
  113.             }
  114.         }
  115.  
  116.         static void DeleteDossier(ref Dictionary<string, List<string>> positions)
  117.         {
  118.             if (positions.Count == 0)
  119.             {
  120.                 Console.WriteLine("Нет досье для удаления.");
  121.                 return;
  122.             }
  123.  
  124.             Console.WriteLine("Список всех сотрудников:");
  125.             List<Tuple<string, string>> allEmployees = new List<Tuple<string, string>>();
  126.             int index = 1;
  127.  
  128.             foreach (var position in positions)
  129.             {
  130.                 foreach (var employee in position.Value)
  131.                 {
  132.                     Console.WriteLine($"{index}) {employee} - {position.Key}");
  133.                     allEmployees.Add(new Tuple<string, string>(employee, position.Key));
  134.                     index++;
  135.                 }
  136.             }
  137.  
  138.             Console.WriteLine("Введите номер досье для удаления:");
  139.             int number = ReadInt();
  140.  
  141.             if (number < 1 || number > allEmployees.Count)
  142.             {
  143.                 Console.WriteLine("Неверный номер досье.");
  144.                 return;
  145.             }
  146.  
  147.             var employeeToDelete = allEmployees[number - 1];
  148.             string empName = employeeToDelete.Item1;
  149.             string positionName = employeeToDelete.Item2;
  150.  
  151.             positions[positionName].Remove(empName);
  152.  
  153.             if (positions[positionName].Count == 0)
  154.             {
  155.                 positions.Remove(positionName);
  156.             }
  157.  
  158.             Console.WriteLine("Досье удалено!");
  159.         }
  160.  
  161.         static int ReadInt()
  162.         {
  163.             int number = 0;
  164.             while (int.TryParse(Console.ReadLine(), out number))
  165.             {
  166.                 Console.WriteLine("Это не число. Пожалуйста, введите число:");
  167.             }
  168.             return number;
  169.         }
  170.     }
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement