Advertisement
Guest User

4.1

a guest
May 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Title = "4.1 Кадровый учет";
  14.  
  15.             //string[,] arrFIO = new string[0, 3]; // Массив будет двумерным для отдельного хранения фамилии, имени и отчества
  16.             //                                  // это облегчит поиск по фамилии
  17.             //string[] arrPositions = new string[0];
  18.  
  19.             string[,] arrFIO = { {"Иванов", "Иван", "Иванович" },
  20.                                  {"Петров", "Петр", "Петрович" },
  21.                                  {"Маск", "Илон", null },
  22.                                  {"Герасим", null, null }}; // Массив будет двумерным для отдельного хранения фамилии, имени и отчества
  23.                                                  // это облегчит поиск по фамилии
  24.             string[] arrPositions = {"Менеджер по уборке территории", "Управляющий хозяйственным инвентарём", "Главный инженер", "Разнорабочий"};
  25.  
  26.  
  27.             int iCommandNumber = -1;
  28.             while (true)
  29.             {
  30.                 Console.Clear();
  31.                 Console.WriteLine("1. Добавить досье.");
  32.                 Console.WriteLine("2. Вывести все досье.");
  33.                 Console.WriteLine("3. Удалить досье.");
  34.                 Console.WriteLine("4. Поиск по фамилии.");
  35.                 Console.WriteLine("---------------------");
  36.                 Console.WriteLine("9. Выход.");
  37.                 Console.WriteLine();
  38.                 iCommandNumber = Convert.ToInt32(GetUserInput("Введите команду: ", 0, 7, true));
  39.  
  40.                 //Console.WriteLine("Введите команду: ");
  41.                 //Console.SetCursorPosition(17, 7);
  42.                 //while (!int.TryParse(Console.ReadLine(), out iCommandNumber))
  43.                 //{
  44.                 //    Console.SetCursorPosition(17, 7);
  45.                 //    Console.Write("    << Введены некорректные данные, повторите");
  46.                 //    Console.SetCursorPosition(17, 7);
  47.                 //}
  48.                 //Console.SetCursorPosition(18, 7);
  49.                 //Console.WriteLine("                                                          ");
  50.  
  51.                 if (iCommandNumber == 9)
  52.                     break;
  53.  
  54.                 switch (iCommandNumber)
  55.                 {
  56.                     case 1:
  57.                         Console.WriteLine();
  58.                         Console.WriteLine("Добавление досье");
  59.                         string sFIO = GetUserInput("Введите фамилию имя отчество\n(например, Иванов Иван Иванович): ");
  60.                         string sPositions = GetUserInput("Введите должность: ");
  61.                         AddRecord(ref arrFIO, ref arrPositions, sFIO, sPositions);
  62.                         break;
  63.                     case 2:
  64.                         Console.WriteLine();
  65.                         Console.WriteLine("Список всех досье");
  66.                         ShowAllRecords(ref arrFIO, ref arrPositions);
  67.                         PressAnyKey();
  68.                         break;
  69.                     case 3:
  70.                         Console.WriteLine();
  71.                         Console.WriteLine("Удаление досье");
  72.                         int i = Convert.ToInt32(GetUserInput("Введите номер досье для удаления: ", 0, 10, true));
  73.                         if (i <= 0 || i > arrFIO.GetLength(0))
  74.                         {
  75.                             Console.WriteLine($"Досье с номером {i} не найдено.");
  76.                             PressAnyKey();
  77.                         }
  78.                         else
  79.                         {
  80.                             if (GetUserInput($"Удалить досье № {i}? (Y-да): ").ToLower() == "y")
  81.                             {
  82.                                 DelRecord(ref arrFIO, ref arrPositions, i - 1);
  83.                                 Console.WriteLine($"Досье № {i} удалено.");
  84.                                 PressAnyKey();
  85.                             }
  86.                         }
  87.                         break;
  88.                     case 4:
  89.                         Console.WriteLine();
  90.                         Console.WriteLine("Поиск по фамилии");
  91.                         string sLastName = GetUserInput("Введите фамилию для поиска досье: ");
  92.                         int iRecorNamber = FindRecord(ref arrFIO, sLastName.ToLower());
  93.                         if(iRecorNamber == -1)
  94.                         {
  95.                             Console.WriteLine("Не найдено досье для фамилии " + sLastName);
  96.                         }
  97.                         else
  98.                         {
  99.                             Console.WriteLine($"Досье для фамилии {sLastName} найдено. Номер досье: {iRecorNamber + 1}");
  100.                         }
  101.                         PressAnyKey();
  102.                         break;
  103.                 }
  104.             }
  105.         }
  106.  
  107.         static void PressAnyKey()
  108.         {
  109.             Console.WriteLine();
  110.             Console.WriteLine("Нажмите любую клавишу...");
  111.             Console.ReadKey();
  112.         }
  113.  
  114.         static string GetUserInput(string message, int coordX = -1, int coordY = -1, bool isInteger = false)
  115.         {
  116.             if(coordX >= 0 || coordY >= 0)
  117.                 Console.SetCursorPosition(Math.Max(coordX, 0), Math.Max(coordY, 0));
  118.  
  119.             Console.Write(message);
  120.             string result = "";
  121.  
  122.             if (isInteger)
  123.             {
  124.                 int i;
  125.                 while (!int.TryParse(Console.ReadLine(), out i))
  126.                 {
  127.                     if (coordX >= 0 || coordY >= 0)
  128.                         Console.SetCursorPosition(Math.Max(coordX, 0) + message.Length, Math.Max(coordY, 0));
  129.                     Console.Write("    << Введены некорректные данные, повторите");
  130.                     if (coordX >= 0 || coordY >= 0)
  131.                         Console.SetCursorPosition(Math.Max(coordX, 0) + message.Length, Math.Max(coordY, 0));
  132.                 }
  133.                 result = i.ToString();
  134.             }
  135.             else
  136.             {
  137.                 result = Console.ReadLine();
  138.             }
  139.             return result;
  140.         }
  141.  
  142.         static void NewSize(ref string[,] arrFIO, ref string[] arrPositions, int size)
  143.         {
  144.             string[,] arrTmp = new string[size, arrFIO.GetLength(1)];
  145.             for (int i = 0; i < Math.Min(arrFIO.GetLength(0), size); i++)
  146.             {
  147.                 for (int j = 0; j < arrFIO.GetLength(1); j++)
  148.                 {
  149.                     arrTmp[i, j] = arrFIO[i, j];
  150.                 }
  151.             }
  152.             arrFIO = arrTmp;
  153.  
  154.             string[] arrTmp1 = new string[size];
  155.             for (int i = 0; i < Math.Min(arrPositions.Length, size); i++)
  156.             {
  157.                 arrTmp1[i] = arrPositions[i];
  158.             }
  159.             arrPositions = arrTmp1;
  160.         }
  161.  
  162.         static void AddRecord(ref string[,] arrFIO, ref string[] arrPositions, string sFIO, string sPositions)
  163.         {
  164.             string[] arrStr = sFIO.Split(' ');
  165.  
  166.             NewSize(ref arrFIO, ref arrPositions, arrFIO.GetLength(0) + 1);
  167.  
  168.             for(int i = 0; i < Math.Min(arrFIO.GetLength(1), arrStr.Length); i++)
  169.             {
  170.                 arrFIO[arrFIO.GetLength(0) - 1, i] = arrStr[i];
  171.             }
  172.  
  173.             arrPositions[arrPositions.Length - 1] = sPositions;
  174.         }
  175.  
  176.         static void ShowAllRecords(ref string[,] arrFIO, ref string[] arrPositions)
  177.         {
  178.             for(int i = 0; i < arrFIO.GetLength(0); i++)
  179.             {
  180.                 Console.Write(i + 1 + ". ");
  181.                 for (int j = 0; j < arrFIO.GetLength(1); j++)
  182.                 {
  183.                     Console.Write(arrFIO[i, j] + " ");
  184.                 }
  185.                 Console.WriteLine(" - " + arrPositions[i]);
  186.             }
  187.         }
  188.  
  189.         static void DelRecord(ref string[,] arrFIO, ref string[] arrPositions, int recNumber)
  190.         {
  191.             for (int i = recNumber; i < arrFIO.GetLength(0) - 1; i++)
  192.             {
  193.                 for(int j = 0; j < arrFIO.GetLength(1); j++)
  194.                 {
  195.                     arrFIO[i, j] = arrFIO[i + 1, j];
  196.                 }
  197.                 arrPositions[i] = arrPositions[i + 1];
  198.             }
  199.  
  200.             NewSize(ref arrFIO, ref arrPositions, arrFIO.GetLength(0) - 1);
  201.         }
  202.  
  203.         static int FindRecord(ref string[,] arrFIO, string findString)
  204.         {
  205.             int result = -1;
  206.             for(int i = 0; i < arrFIO.GetLength(0); i++)
  207.             {
  208.                 if((arrFIO[i, 0]).ToLower() == findString)
  209.                 {
  210.                     result = i;
  211.                     break;
  212.                 }
  213.             }
  214.             return result;
  215.         }
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement