Skip to content

Instantly share code, notes, and snippets.

@trayforyou
Created April 28, 2024 08:32
Show Gist options
  • Save trayforyou/288dbaeebfe1bbb46be542ee3e1090ae to your computer and use it in GitHub Desktop.
Save trayforyou/288dbaeebfe1bbb46be542ee3e1090ae to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Collections3
{
internal class Program
{
static void Main(string[] args)
{
string commandAddEmployee = "add";
string commandDeleteEmployee = "delete";
string commandWriteAllEmployee = "info";
bool isOpen = true;
Dictionary<string, List<string>> employees = new Dictionary<string, List<string>>();
while (isOpen)
{
Console.Clear();
Console.WriteLine("Введите команду:");
Console.WriteLine(commandAddEmployee + " - Добавить нового сотрудника.");
Console.WriteLine(commandDeleteEmployee + " - Уволить сотрудника.");
Console.WriteLine(commandWriteAllEmployee + " - Показать всех сотрудников.");
string userInput = Console.ReadLine();
if (userInput == commandAddEmployee)
{
AddNewEmployee(employees);
}
else if (userInput == commandDeleteEmployee)
{
DeleteEmployee(employees);
}
else if (userInput == commandWriteAllEmployee)
{
WriteAllEmployees(employees);
}
Console.ReadKey();
}
}
private static void AddNewEmployee(Dictionary<string, List<string>> employees)
{
Console.Clear();
Console.Write("Введите должность сотрудника: ");
string post = Console.ReadLine();
Console.Write("Введите Фамилию Имя и Отчество сотрудника через пробел: ");
string nameOfEmployee = Console.ReadLine();
if (employees.ContainsKey(post))
{
employees[post].Add(nameOfEmployee);
}
else
{
employees.Add(post, new List<string>());
employees[post].Add(nameOfEmployee);
}
}
private static void WriteAllEmployees(Dictionary<string, List<string>> employees)
{
Console.Clear();
foreach (var post in employees)
{
Console.WriteLine($"\n{post.Key}\n");
foreach (var employee in post.Value)
{
Console.WriteLine(employee);
}
}
}
private static void DeleteEmployee(Dictionary<string, List<string>> employees)
{
Console.Clear();
Console.Write("Введите ФИО сотрудника которого хотите уволить: ");
string userInput = Console.ReadLine();
foreach (var post in employees)
{
int counterIndex = post.Value.Count - 1;
for (int i = counterIndex; i >= 0; i--)
{
if (post.Value[i] == userInput)
{
post.Value.RemoveAt(i);
break;
}
}
if (post.Value.Count == 0)
{
employees.Remove(post.Key);
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment