Skip to content

Instantly share code, notes, and snippets.

@novicehelg
Created April 29, 2025 10:44
Show Gist options
  • Save novicehelg/9a96cb80440c93fe000dd3da3dc1642a to your computer and use it in GitHub Desktop.
Save novicehelg/9a96cb80440c93fe000dd3da3dc1642a to your computer and use it in GitHub Desktop.
дз по теме коллекции/кадровый учет продвинутый 4
internal class Program
{
private static void Main(string[] args)
{
const string CommandAddNewPerson = "1";
const string CommandDeleteStaff = "2";
const string CommandShowAllStaff = "3";
const string CommandExit = "4";
bool isWorking = true;
Dictionary<string, List<string>> allStaff = new Dictionary<string, List<string>>();
ShowMenu(CommandAddNewPerson,CommandDeleteStaff,CommandShowAllStaff,CommandExit);
while (isWorking)
{
Console.Clear();
ShowMenu(CommandAddNewPerson, CommandDeleteStaff, CommandShowAllStaff, CommandExit);
string userChoice = Console.ReadLine();
switch (userChoice)
{
case CommandAddNewPerson:
AddPerson(allStaff);
break;
case CommandDeleteStaff:
DeletePerson(allStaff);
break;
case CommandShowAllStaff:
ShowStaff(allStaff);
break;
case CommandExit:
isWorking = false;
break;
default:
Console.WriteLine("ВВедена неверная команда.Попробуйте снова.");
break;
}
Console.ReadLine();
}
}
static void AddPerson(Dictionary<string, List<string>> allStaff)
{
Console.WriteLine("Введите название должности");
string userInputPosition = Console.ReadLine();
Console.WriteLine("ВВедите имя сотрудника");
string userInputName = Console.ReadLine();
if (allStaff.ContainsKey(userInputPosition) == false)
{
List<string> staffNames = new List<string>();
allStaff.Add(userInputPosition, staffNames);
}
allStaff[userInputPosition].Add(userInputName);
}
static void DeleteNameFromList(List<string> namesOfStaff, int index)
{
if (namesOfStaff.Count < index || index<0)
{
Console.WriteLine("Oшибка.Такого сотрудника нет.");
}
else
{
namesOfStaff.RemoveAt(index-1);
}
}
static void DeletePerson(Dictionary<string, List<string>> allStaff)
{
Console.WriteLine("Введите название должности");
string userInputPosition = Console.ReadLine();
if (allStaff.ContainsKey(userInputPosition))
{
List<string> staffOnPosition = allStaff[userInputPosition];
Console.WriteLine("Введите номер сотрудника");
if (int.TryParse(Console.ReadLine(), out int staffToDeleteIndex))
{
DeleteNameFromList(staffOnPosition, staffToDeleteIndex);
if (staffOnPosition.Count == 0)
{
allStaff.Remove(userInputPosition);
}
}
else
{
Console.WriteLine("Ошибка ввода.");
}
}
else
{
Console.WriteLine("Ошибка.Такой должности нет.");
}
}
static void ShowStaff(Dictionary<string, List<string>> staff)
{
foreach (var key in staff.Keys)
{
Console.WriteLine(key + ":");
List<string> staffInKey = staff[key];
for (int i = 0; i < staffInKey.Count; i++)
{
Console.WriteLine("\t"+(i +1) + staffInKey[i]);
}
}
}
static void ShowMenu(string addNewPerson,string deletePerson,string showAllStaff,string exit)
{
Console.WriteLine("Выберите колманду:");
Console.WriteLine($"{addNewPerson} - Добавить сотрудника");
Console.WriteLine($"{deletePerson} - Удалить сотрудника");
Console.WriteLine($"{showAllStaff} - Показать полный список");
Console.WriteLine($"{exit} - Выход");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment