Skip to content

Instantly share code, notes, and snippets.

@StrikerXx798
Last active April 20, 2025 13:51
Show Gist options
  • Save StrikerXx798/f1ca57236c2d9533d7ba5f262c8dc902 to your computer and use it in GitHub Desktop.
Save StrikerXx798/f1ca57236c2d9533d7ba5f262c8dc902 to your computer and use it in GitHub Desktop.
ДЗ: Динамический массив продвинутый
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
internal class Program
{
const int CommandAddEmployee = 1;
const int CommandRemoveEmployee = 2;
const int CommandShowAllInformation = 3;
const int CommandExitProgram = 4;
static void Main(string[] args)
{
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;
Dictionary<string, List<(string FullName, string Passport)>> staff = new Dictionary<string, List<(string, string)>>();
bool isRun = true;
while (isRun)
{
Console.WriteLine("\nВыберите действие:");
Console.WriteLine($"{CommandAddEmployee} - Добавить сотрудника");
Console.WriteLine($"{CommandRemoveEmployee} - Удалить сотрудника");
Console.WriteLine($"{CommandShowAllInformation} - Показать всю информацию");
Console.WriteLine($"{CommandExitProgram} - Выйти из программы");
if (!int.TryParse(Console.ReadLine(), out int choice))
{
Console.WriteLine("Некорректный ввод. Попробуйте снова.");
continue;
}
switch (choice)
{
case CommandAddEmployee:
AddEmployee(staff);
break;
case CommandRemoveEmployee:
RemoveEmployee(staff);
break;
case CommandShowAllInformation:
ShowAllInformation(staff);
break;
case CommandExitProgram:
Console.WriteLine("Выход из программы.");
isRun = false;
break;
default:
Console.WriteLine("Неверный выбор. Попробуйте снова.");
break;
}
}
}
static void AddEmployee(Dictionary<string, List<(string FullName, string Passport)>> staff)
{
Console.Write("Введите должность: ");
string position = Console.ReadLine();
Console.Write("Введите полное имя сотрудника: ");
string fullName = Console.ReadLine();
Console.Write("Введите паспортные данные сотрудника: ");
string passport = Console.ReadLine();
if (!staff.ContainsKey(position))
{
staff[position] = new List<(string, string)>();
}
if (staff[position].Exists(e => e.Passport == passport))
{
Console.WriteLine($"Сотрудник с паспортными данными '{passport}' уже существует на должности '{position}'.");
}
else
{
staff[position].Add((fullName, passport));
Console.WriteLine($"Сотрудник '{fullName}' успешно добавлен на должность '{position}'.");
}
}
static void RemoveEmployee(Dictionary<string, List<(string FullName, string Passport)>> staff)
{
Console.Write("Введите должность: ");
string position = Console.ReadLine();
if (!staff.ContainsKey(position))
{
Console.WriteLine($"Должность '{position}' отсутствует.");
return;
}
Console.WriteLine($"Список сотрудников на должности '{position}':");
for (int i = 0; i < staff[position].Count; i++)
{
Console.WriteLine($"{i + 1}. {staff[position][i].FullName} (Паспорт: {staff[position][i].Passport})");
}
Console.Write("Введите номер сотрудника для удаления: ");
if (!int.TryParse(Console.ReadLine(), out int index) || index < 1 || index > staff[position].Count)
{
Console.WriteLine("Некорректный номер. Попробуйте снова.");
return;
}
var removedEmployee = staff[position][index - 1];
staff[position].RemoveAt(index - 1);
Console.WriteLine($"Сотрудник '{removedEmployee.FullName}' успешно удалён с должности '{position}'.");
if (staff[position].Count == 0)
{
staff.Remove(position);
Console.WriteLine($"Должность '{position}' удалена, так как больше не имеет сотрудников.");
}
}
static void ShowAllInformation(Dictionary<string, List<(string FullName, string Passport)>> staff)
{
if (staff.Count == 0)
{
Console.WriteLine("Список должностей и сотрудников пуст.");
return;
}
Console.WriteLine("Полная информация о должностях и сотрудниках:");
foreach (var position in staff)
{
Console.WriteLine($"Должность: {position.Key}");
for (int i = 0; i < position.Value.Count; i++)
{
var employee = position.Value[i];
Console.WriteLine($" {i + 1}. {employee.FullName} (Паспорт: {employee.Passport})");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment