ranee

досье листы

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