ranee

досье

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