ranee

досье

Feb 10th, 2022 (edited)
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             bool exit = false;
  11.             int menuBar = 0;
  12.             List<string> menu = new List<string> { "1. Добавить досье.", "2. Вывести досье.", "3. Удалить досье.", "4. Выход." };
  13.             Dictionary<string, string> dossiers = new Dictionary<string, string>
  14.             {
  15.                 { "Иванов Иван Иванович",  "Директор" },
  16.                 { "Шевцов Глеб Егорыч",  "Коллектор" }
  17.             };
  18.             Console.CursorVisible = false;
  19.  
  20.             while (exit == false)
  21.             {
  22.                 bool input = false;
  23.                 DrawMenu(ref menuBar, menu);
  24.                 CheckingKeystrokes(ref menuBar, menu, ref input);
  25.                 Console.Clear();
  26.                 if(input)
  27.                 {
  28.                     switch (menu[menuBar])
  29.                     {
  30.                         case "1. Добавить досье.":
  31.                             AddDossiers(dossiers);
  32.                             break;
  33.                         case "2. Вывести досье.":
  34.                             DrawAllDossier(dossiers);
  35.                             Console.ReadKey();
  36.                             Console.Clear();
  37.                             break;
  38.                         case "3. Удалить досье.":
  39.                             DrawAllDossier(dossiers);
  40.                             RemoveDossiers(dossiers);
  41.                             break;
  42.                         case "4. Выход.":
  43.                             ChangeToTrue(ref exit);
  44.                             break;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.         }
  50.  
  51.         static void RemoveDossiers(Dictionary<string, string> dossiers)
  52.         {
  53.             DrawLabels("Ведите ФИО сотрудника:", 0, 40);
  54.             string checkWorker = Console.ReadLine();
  55.             if (dossiers.ContainsKey(checkWorker))
  56.             {
  57.                 dossiers.Remove(checkWorker);
  58.                 Console.Clear();
  59.             }
  60.             else
  61.             {
  62.                 DrawLabels($"Нет сотрудника с фио {checkWorker}", 1, 40);
  63.             }
  64.         }
  65.         static void AddDossiers(Dictionary<string, string> dossiers)
  66.         {
  67.             string newWorker, positonWorker;
  68.             DrawLabels("Ведите ФИО нового сотрудника:", 0, 0);
  69.             newWorker = Console.ReadLine();
  70.             DrawLabels("Ведите должность сотрудника:", 1, 0);
  71.             positonWorker = Console.ReadLine();
  72.             TryAdd(newWorker, positonWorker, dossiers);
  73.             Console.Clear();
  74.         }
  75.         static void TryAdd(string key, string value, Dictionary<string, string> dossiers)
  76.         {
  77.             if (dossiers.ContainsKey(key))
  78.             {
  79.                 Console.WriteLine("Работник уже внесен.");
  80.                 Console.ReadKey();
  81.                 Console.Clear();
  82.             }
  83.             else
  84.             {
  85.                 dossiers.Add(key, value);
  86.             }
  87.         }
  88.  
  89.         static void DrawAllDossier(Dictionary<string, string> dossiers)
  90.         {
  91.             if (dossiers.Count == 0)
  92.             {
  93.                 DrawLabels("Нет досье в списке.");
  94.             }
  95.             else
  96.             {
  97.                 int serialNumber = 1;
  98.                 foreach (var dossier in dossiers)
  99.                 {
  100.                     DrawLabels($"{serialNumber} {dossier.Key} - {dossier.Value}", serialNumber - 1, 0);
  101.                     serialNumber++;
  102.                 }
  103.             }
  104.         }
  105.  
  106.         static void DrawLabels(string text, int x = 0, int y = 30)
  107.         {
  108.             Console.SetCursorPosition(y, x);
  109.             Console.Write(text);
  110.         }
  111.  
  112.         static void DrawMenu(ref int menuBar, List<string> menu)
  113.         {
  114.             Console.SetCursorPosition(0, 0);
  115.             for (int i = 0; i < menu.Count; i++)
  116.             {
  117.                 if (i == menuBar)
  118.                 {
  119.                     Console.BackgroundColor = ConsoleColor.Gray;
  120.                     Console.ForegroundColor = ConsoleColor.Black;
  121.                     Console.WriteLine(menu[i]);
  122.                 }
  123.                 else
  124.                 {
  125.                     Console.WriteLine(menu[i]);
  126.                 }
  127.                 Console.ResetColor();
  128.             }
  129.         }
  130.  
  131.         static void ChangeToTrue(ref bool temp)
  132.         {
  133.             temp = true;
  134.         }
  135.  
  136.         static void UpLine(ref int menuBar, List<string> menu)
  137.         {
  138.             if (menuBar <= 0)
  139.             {
  140.                 menuBar = menu.Count - 1;
  141.                 DrawMenu(ref menuBar, menu);
  142.             }
  143.             else
  144.             {
  145.                 menuBar--;
  146.                 DrawMenu(ref menuBar, menu);
  147.             }
  148.         }
  149.  
  150.         static void DownLine(ref int menuBar, List<string> menu)
  151.         {
  152.             if (menuBar == menu.Count - 1)
  153.             {
  154.                 menuBar = 0;
  155.                 DrawMenu(ref menuBar, menu);
  156.             }
  157.             else
  158.             {
  159.                 menuBar++;
  160.                 DrawMenu(ref menuBar, menu);
  161.             }
  162.         }
  163.         static void CheckingKeystrokes(ref int menuBar, List<string> menu, ref bool input)
  164.         {
  165.             ConsoleKeyInfo key = Console.ReadKey();
  166.             switch (key.Key)
  167.             {
  168.                 case ConsoleKey.UpArrow:
  169.                     UpLine(ref menuBar, menu);
  170.                     break;
  171.                 case ConsoleKey.DownArrow:
  172.                     DownLine(ref menuBar, menu);
  173.                     break;
  174.                 case ConsoleKey.Enter:
  175.                     ChangeToTrue(ref input);
  176.                     break;
  177.             }
  178.         }
  179.     }
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment