Advertisement
SaNik74

personal accounting

May 13th, 2024 (edited)
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.51 KB | None | 0 0
  1. namespace personal_accounting
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             const string AddNewDossierCommand = "add dossier";
  8.             const string OutputDossierCommand = "output all dossier";
  9.             const string FindLastNameCommand = "find";
  10.             const string DeleteDossierCommand = "delete";
  11.             const string ExitCommand = "exit";
  12.  
  13.             bool isWorking = true;
  14.             string[] fullNames = new string[0];
  15.             string[] jobTitles = new string[0];
  16.  
  17.             while (isWorking)
  18.             {
  19.                 Console.WriteLine($"База данных:" +
  20.             $"\nДля добавления досье введите - {AddNewDossierCommand}." +
  21.             $"\nДля вывода всей базы введите - {OutputDossierCommand}." +
  22.             $"\nДля удаления досье введите - {DeleteDossierCommand}." +
  23.             $"\nДля поиска по фамилии введите - {FindLastNameCommand}." +
  24.             $"\nДля выхода введите - {ExitCommand}.");
  25.  
  26.                 string? userInput = Console.ReadLine();
  27.  
  28.                 switch (userInput)
  29.                 {
  30.                     case AddNewDossierCommand:
  31.                         AddDossier(ref fullNames, ref jobTitles);
  32.                         break;
  33.  
  34.                     case OutputDossierCommand:
  35.                         OutputAllDossier(fullNames, jobTitles);
  36.                         break;
  37.  
  38.                     case DeleteDossierCommand:
  39.                         DeleteDossier(ref fullNames, ref jobTitles);
  40.                         break;
  41.  
  42.                     case FindLastNameCommand:
  43.                         FindLastName(fullNames, jobTitles);
  44.                         break;
  45.  
  46.                     case ExitCommand:
  47.                         isWorking = false;
  48.                         break;
  49.  
  50.                     default:
  51.                         DisplayUncorrectMessage();
  52.                         break;
  53.                 }
  54.  
  55.                 Console.Clear();
  56.             }
  57.         }
  58.  
  59.         static string[] AddData(string[] array, string data)
  60.         {
  61.             string[] tempArray = new string[array.Length + 1];
  62.  
  63.             for (int i = 0; i < array.Length; i++)
  64.             {
  65.                 tempArray[i] = array[i];
  66.             }
  67.  
  68.             Console.Write($"Введите {data}: ");
  69.             string? name = Console.ReadLine();
  70.             tempArray[array.Length] = name;
  71.             array = tempArray;
  72.             return array;
  73.         }
  74.  
  75.         static void AddDossier(ref string[] fullNames, ref string[] jobTitles)
  76.         {
  77.             string fullName = "ФИО";
  78.             string jobTitle = "должность";
  79.  
  80.             fullNames = AddData(fullNames, fullName);
  81.             jobTitles = AddData(jobTitles, jobTitle);
  82.         }
  83.  
  84.         static void OutputAllDossier(string[] fullNames, string[] jobTitles)
  85.         {
  86.             if (fullNames.Length == 0)
  87.             {
  88.                 Console.WriteLine("Массив пуст.");
  89.                 Console.ReadKey();
  90.             }
  91.             else
  92.             {
  93.                 for (int i = 0; i < fullNames.Length - 1; i++)
  94.                 {
  95.                     int serialNumber = i + 1;
  96.  
  97.                     Console.Write($"{serialNumber} {fullNames[i]} {jobTitles[i]} - ");
  98.                 }
  99.  
  100.                 Console.Write($"{fullNames.Length} {fullNames[fullNames.Length - 1]} {jobTitles[jobTitles.Length - 1]}.");
  101.  
  102.                 Console.ReadKey();
  103.             }
  104.         }
  105.  
  106.         static string[] DeleteData(string[] array, int index)
  107.         {
  108.             string[] tempArray = new string[array.Length - 1];
  109.  
  110.             for (int i = 0; i < index; i++)
  111.             {
  112.                 tempArray[i] = array[i];
  113.             }
  114.  
  115.             for (int i = index + 1; i < array.Length; i++)
  116.             {
  117.                 tempArray[i - 1] = array[i];
  118.             }
  119.  
  120.             array = tempArray;
  121.             return array;
  122.         }
  123.  
  124.         static int TryGetNumber()
  125.         {
  126.             bool isWorking = true;
  127.             int number = 0;
  128.  
  129.             bool isNumber = int.TryParse(Console.ReadLine(), out int result);
  130.  
  131.             if (isNumber)
  132.             {
  133.                 number = result - 1;
  134.                 isWorking = false;
  135.             }
  136.             else
  137.             {
  138.                 Console.WriteLine("Необходимо ввести число.");
  139.                 Console.ReadKey();
  140.             }
  141.  
  142.             return number;
  143.         }
  144.  
  145.         static void DeleteDossier(ref string[] fullNames, ref string[] jobTitles)
  146.         {
  147.             Console.Write("Введите номер досье, которое хотите удалить: ");
  148.  
  149.             int number = TryGetNumber();
  150.  
  151.             if (number > fullNames.Length || number < 0)
  152.             {
  153.                 Console.WriteLine("Число вышло за пределы массива.");
  154.                 Console.ReadKey();
  155.             }
  156.             else if (fullNames.Length == 0)
  157.             {
  158.                 Console.WriteLine("Массив пуст.");
  159.                 Console.ReadKey();
  160.             }
  161.             else
  162.             {
  163.                 fullNames = DeleteData(fullNames, number);
  164.                 jobTitles = DeleteData(jobTitles, number);
  165.             }
  166.         }
  167.  
  168.         static void FindLastName(string[] fullNames, string[] jobTitles)
  169.         {
  170.             if (fullNames.Length == 0)
  171.             {
  172.                 Console.WriteLine("Массив пуст.");
  173.                 Console.ReadKey();
  174.                 return;
  175.             }
  176.  
  177.             Console.Write("Введите фамилию: ");
  178.             string? lastName = Console.ReadLine();
  179.  
  180.             for (int i = 0; i < fullNames.Length; i++)
  181.             {
  182.                 int serialNumber = i + 1;
  183.  
  184.                 if (fullNames[i].Split()[0] == lastName)
  185.                 {
  186.                     Console.WriteLine($"{serialNumber} {fullNames[i]} {jobTitles[i]}");
  187.                     Console.ReadKey();
  188.                     return;
  189.                 }
  190.             }
  191.  
  192.             Console.WriteLine($"Досье с фамилией {lastName} не найдено.");
  193.             Console.ReadKey();
  194.         }
  195.  
  196.         static void DisplayUncorrectMessage()
  197.         {
  198.             Console.WriteLine("Введена некорректная команда. Попробуйте еще раз.");
  199.             Console.ReadKey();
  200.         }
  201.     }
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement