Advertisement
SPavelA

EmployesDictionary

Sep 24th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.64 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8.  
  9. namespace CollectionsTask4Employes
  10. {
  11.     internal class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             const string CommandAddEmploye = "1";
  16.             const string CommandPrintAllEmploye = "2";
  17.             const string CommandDeleteEmploye = "3";
  18.             const string CommandExit = "4";
  19.  
  20.             Dictionary<string, List<string>> positionsEmplyes = new Dictionary<string, List<string>> ();
  21.            
  22.             string inputCommand = "";
  23.             bool isWorking = true;
  24.  
  25.             while (isWorking)
  26.             {
  27.                 Console.Clear();
  28.                 Console.WriteLine($"Сейчас в базе {CalcEmployeCount(positionsEmplyes)} сотрудников");
  29.                 Console.WriteLine($"{CommandAddEmploye} - добавить нового сотрудника");
  30.                 Console.WriteLine($"{CommandPrintAllEmploye} - распечатать все досье");
  31.                 Console.WriteLine($"{CommandDeleteEmploye} - удалить сотрудника");
  32.                 Console.WriteLine($"{CommandExit} - выход");
  33.                 Console.Write("Введите команду: ");
  34.                 inputCommand = Console.ReadLine();
  35.  
  36.                 switch (inputCommand)
  37.                 {
  38.                     case CommandAddEmploye:
  39.                         AddEmploye(positionsEmplyes);
  40.                         break;
  41.  
  42.                     case CommandPrintAllEmploye:
  43.                         PrintAllEmployes(positionsEmplyes);
  44.                         break;
  45.  
  46.                     case CommandDeleteEmploye:
  47.                         DeleteEmploye(positionsEmplyes);
  48.                         break;
  49.  
  50.                     case CommandExit:
  51.                         isWorking = false;
  52.                         break;
  53.  
  54.                     default:
  55.                         Console.WriteLine("Неизвестная команда");
  56.                         break;
  57.                 }
  58.  
  59.                 Console.ReadKey();
  60.             }
  61.         }
  62.  
  63.         private static void DeleteEmploye(Dictionary<string, List<string>> positionsEmployes)
  64.         {
  65.             if (IsEmpty(positionsEmployes) == true)
  66.             {
  67.                 return;
  68.             }
  69.  
  70.             PrintAllEmployes(positionsEmployes);
  71.             Console.Write("Введите должность сотрудника, которого хотите удалить: ");
  72.            
  73.             string positionForDelete = Console.ReadLine();
  74.  
  75.             if (positionsEmployes.ContainsKey(positionForDelete) == false)
  76.             {
  77.                 Console.WriteLine("Нет такой должности");
  78.                 return;
  79.             }
  80.  
  81.             PrintEmployesByPosition(positionsEmployes, positionForDelete);
  82.             Console.Write("Введите номер сотрудника, которого хотите удалить: ");
  83.  
  84.             int employeNumberForDelete = ReadInt();
  85.  
  86.             if (employeNumberForDelete < 1 || employeNumberForDelete > positionsEmployes[positionForDelete].Count)
  87.             {
  88.                 Console.WriteLine("Сотрудника с таким номером не существует в базе");
  89.                 return;
  90.             }
  91.  
  92.             positionsEmployes[positionForDelete].RemoveAt(employeNumberForDelete - 1);
  93.  
  94.             if (positionsEmployes[positionForDelete].Count == 0)
  95.             {
  96.                 positionsEmployes.Remove(positionForDelete);
  97.             }
  98.  
  99.             Console.WriteLine("Сотрудник удален из базы");
  100.         }
  101.  
  102.         private static int ReadInt()
  103.         {
  104.             int number;
  105.  
  106.             while (int.TryParse(Console.ReadLine(), out number) == false)
  107.             {
  108.                 Console.Write("Это не число, попробуйте еще раз: ");
  109.             }
  110.  
  111.             return number;
  112.         }
  113.  
  114.         private static void AddEmploye(Dictionary<string, List<string>> positionsEmployes)
  115.         {
  116.             string name = "";
  117.             string position;
  118.             Console.WriteLine("Введите фамилию нового сотрудника: ");
  119.             name += Console.ReadLine() + " ";
  120.             Console.WriteLine("Введите имя нового сотрудника: ");
  121.             name += Console.ReadLine() + " ";
  122.             Console.WriteLine("Введите отчество нового сотрудника: ");
  123.             name += Console.ReadLine();
  124.             Console.WriteLine("Введите должность нового сотрудника: ");
  125.             position = Console.ReadLine();
  126.            
  127.             if(positionsEmployes.Keys.Contains(position) == false)
  128.             {
  129.                 positionsEmployes.Add(position, new List<string>());
  130.             }
  131.  
  132.             positionsEmployes[position].Add(name);
  133.         }
  134.  
  135.         private static void PrintAllEmployes(Dictionary<string, List<string>> positionsEmployes)
  136.         {
  137.             if (IsEmpty(positionsEmployes) == true)
  138.             {
  139.                 return;
  140.             }
  141.  
  142.             foreach(string position in positionsEmployes.Keys)
  143.             {
  144.                 PrintEmployesByPosition(positionsEmployes, position);
  145.             }
  146.         }
  147.  
  148.         private static void PrintEmployesByPosition(Dictionary<string, List<string>> positionsEmployes, string position)
  149.         {
  150.             int employeNumber = 1;
  151.  
  152.             Console.WriteLine($"На должности {position}:");
  153.  
  154.             foreach (string name in positionsEmployes[position])
  155.             {
  156.                 PrintEmploye(employeNumber++, name);
  157.             }
  158.         }
  159.  
  160.         private static void PrintEmploye(int employeNumber, string name)
  161.         {
  162.             Console.WriteLine($"{employeNumber} {name}");
  163.         }
  164.  
  165.         private static bool IsEmpty(Dictionary<string, List<string>> positionsEmployes)
  166.         {
  167.             if (positionsEmployes.Count == 0)
  168.             {
  169.                 Console.WriteLine("В базе нет сотрудников");
  170.                 return true;
  171.             }
  172.             return false;
  173.         }
  174.  
  175.         private static int CalcEmployeCount(Dictionary<string, List<string>> positionsEmployes)
  176.         {
  177.             int count = 0;
  178.  
  179.             foreach (string position in positionsEmployes.Keys)
  180.             {
  181.                 count += positionsEmployes[position].Count;
  182.             }
  183.  
  184.             return count;
  185.         }
  186.     }
  187. }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement