Skip to content

Instantly share code, notes, and snippets.

@StrikerXx798
Last active April 19, 2025 14:58
Show Gist options
  • Save StrikerXx798/a47ee5accb80438af2cc4042206c0e05 to your computer and use it in GitHub Desktop.
Save StrikerXx798/a47ee5accb80438af2cc4042206c0e05 to your computer and use it in GitHub Desktop.
ДЗ: Кадровый учет
using System;
using System.Text;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;
string[] employees = new string[0];
string[] positions = new string[0];
GetMenu(ref employees, ref positions);
}
static void GetMenu(ref string[] employees, ref string[] positions)
{
const int CommandAddProfile = 1;
const int CommandGetAllProfiles = 2;
const int CommandDeleteProfile = 3;
const int CommandSearchByLastname = 4;
const int CommandExitProgram = 5;
bool isRun = true;
while (isRun)
{
Console.WriteLine("\nВыберите действие:");
Console.WriteLine($"{CommandAddProfile} - Добавить досье сотрудника");
Console.WriteLine($"{CommandGetAllProfiles} - Вывести все досье");
Console.WriteLine($"{CommandDeleteProfile} - Удалить досье сотрудника");
Console.WriteLine($"{CommandSearchByLastname} - Найти досье сотрудника по фамилии");
Console.WriteLine($"{CommandExitProgram} - Выйти из программы");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case CommandAddProfile:
AddProfile(ref employees, ref positions);
break;
case CommandGetAllProfiles:
PrintProfiles(employees, positions);
break;
case CommandDeleteProfile:
DeleteProfile(ref employees, ref positions);
break;
case CommandSearchByLastname:
SearchByLastname(employees, positions);
break;
case CommandExitProgram:
Console.WriteLine("Вы успешно вышли из программы!");
isRun = false;
break;
default:
Console.WriteLine("Неверное действие. Попробуйте ввести другую команду!");
break;
}
}
}
static void AddProfile(ref string[] employees, ref string[] positions)
{
Console.Write("Введите ФИО сотрудника: ");
string newEmployee = Console.ReadLine();
Console.Write("Введите должность сотрудника: ");
string newPosition = Console.ReadLine();
employees = AddToArray(employees, newEmployee);
positions = AddToArray(positions, newPosition);
Console.WriteLine("Досье успешно добавлено!");
}
static void DeleteProfile(ref string[] employees, ref string[] positions)
{
if (employees.Length == 0)
{
Console.WriteLine("Список досье пуст. Удаление невозможно.");
return;
}
Console.Write("Введите номер досье для удаления: ");
int indexToDelete = Convert.ToInt32(Console.ReadLine()) - 1;
if (indexToDelete < 0 || indexToDelete >= employees.Length)
{
Console.WriteLine("Некорректный номер досье.");
return;
}
employees = RemoveFromArray(employees, indexToDelete);
positions = RemoveFromArray(positions, indexToDelete);
Console.WriteLine("Досье успешно удалено!");
}
static void PrintProfiles(string[] employees, string[] positions)
{
if (employees.Length == 0)
{
Console.WriteLine("Список досье пуст.");
return;
}
Console.WriteLine("Список досье:");
for (int i = 0; i < employees.Length; i++)
{
Console.WriteLine($"{i + 1}. {employees[i]} - {positions[i]}");
}
}
static void SearchByLastname(string[] employees, string[] positions)
{
if (employees.Length == 0)
{
Console.WriteLine("Список досье пуст.");
return;
}
Console.Write("Введите фамилию для поиска: ");
string lastname = Console.ReadLine();
bool isFound = false;
for (int i = 0; i < employees.Length; i++)
{
string[] nameParts = employees[i].Split();
if (nameParts.Length > 0 && nameParts[0].Equals(lastname, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"{i + 1}. {employees[i]} - {positions[i]}");
isFound = true;
}
}
if (isFound == false)
{
Console.WriteLine("Досье с указанной фамилией не найдено.");
}
}
static string[] AddToArray(string[] array, string newItem)
{
string[] tempArray = new string[array.Length + 1];
for (int i = 0; i < array.Length; i++)
{
tempArray[i] = array[i];
}
tempArray[array.Length] = newItem;
return tempArray;
}
static string[] RemoveFromArray(string[] array, int indexToRemove)
{
string[] tempArray = new string[array.Length - 1];
for (int i = 0; i < indexToRemove; i++)
{
tempArray[i] = array[i];
}
for (int i = indexToRemove + 1; i < array.Length; i++)
{
tempArray[i - 1] = array[i];
}
return tempArray;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment