Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace CollectionsTask4Employes
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- const string CommandAddEmploye = "1";
- const string CommandPrintAllEmploye = "2";
- const string CommandDeleteEmploye = "3";
- const string CommandExit = "4";
- Dictionary<string, List<string>> positionsEmplyes = new Dictionary<string, List<string>> ();
- string inputCommand = "";
- bool isWorking = true;
- while (isWorking)
- {
- Console.Clear();
- Console.WriteLine($"Сейчас в базе {CalcEmployeCount(positionsEmplyes)} сотрудников");
- Console.WriteLine($"{CommandAddEmploye} - добавить нового сотрудника");
- Console.WriteLine($"{CommandPrintAllEmploye} - распечатать все досье");
- Console.WriteLine($"{CommandDeleteEmploye} - удалить сотрудника");
- Console.WriteLine($"{CommandExit} - выход");
- Console.Write("Введите команду: ");
- inputCommand = Console.ReadLine();
- switch (inputCommand)
- {
- case CommandAddEmploye:
- AddEmploye(positionsEmplyes);
- break;
- case CommandPrintAllEmploye:
- PrintAllEmployes(positionsEmplyes);
- break;
- case CommandDeleteEmploye:
- DeleteEmploye(positionsEmplyes);
- break;
- case CommandExit:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Неизвестная команда");
- break;
- }
- Console.ReadKey();
- }
- }
- private static void DeleteEmploye(Dictionary<string, List<string>> positionsEmployes)
- {
- if (IsEmpty(positionsEmployes) == true)
- {
- return;
- }
- PrintAllEmployes(positionsEmployes);
- Console.Write("Введите должность сотрудника, которого хотите удалить: ");
- string positionForDelete = Console.ReadLine();
- if (positionsEmployes.ContainsKey(positionForDelete) == false)
- {
- Console.WriteLine("Нет такой должности");
- return;
- }
- PrintEmployesByPosition(positionsEmployes, positionForDelete);
- Console.Write("Введите номер сотрудника, которого хотите удалить: ");
- int employeNumberForDelete = ReadInt();
- if (employeNumberForDelete < 1 || employeNumberForDelete > positionsEmployes[positionForDelete].Count)
- {
- Console.WriteLine("Сотрудника с таким номером не существует в базе");
- return;
- }
- positionsEmployes[positionForDelete].RemoveAt(employeNumberForDelete - 1);
- if (positionsEmployes[positionForDelete].Count == 0)
- {
- positionsEmployes.Remove(positionForDelete);
- }
- Console.WriteLine("Сотрудник удален из базы");
- }
- private static int ReadInt()
- {
- int number;
- while (int.TryParse(Console.ReadLine(), out number) == false)
- {
- Console.Write("Это не число, попробуйте еще раз: ");
- }
- return number;
- }
- private static void AddEmploye(Dictionary<string, List<string>> positionsEmployes)
- {
- string name = "";
- string position;
- Console.WriteLine("Введите фамилию нового сотрудника: ");
- name += Console.ReadLine() + " ";
- Console.WriteLine("Введите имя нового сотрудника: ");
- name += Console.ReadLine() + " ";
- Console.WriteLine("Введите отчество нового сотрудника: ");
- name += Console.ReadLine();
- Console.WriteLine("Введите должность нового сотрудника: ");
- position = Console.ReadLine();
- if(positionsEmployes.Keys.Contains(position) == false)
- {
- positionsEmployes.Add(position, new List<string>());
- }
- positionsEmployes[position].Add(name);
- }
- private static void PrintAllEmployes(Dictionary<string, List<string>> positionsEmployes)
- {
- if (IsEmpty(positionsEmployes) == true)
- {
- return;
- }
- foreach(string position in positionsEmployes.Keys)
- {
- PrintEmployesByPosition(positionsEmployes, position);
- }
- }
- private static void PrintEmployesByPosition(Dictionary<string, List<string>> positionsEmployes, string position)
- {
- int employeNumber = 1;
- Console.WriteLine($"На должности {position}:");
- foreach (string name in positionsEmployes[position])
- {
- PrintEmploye(employeNumber++, name);
- }
- }
- private static void PrintEmploye(int employeNumber, string name)
- {
- Console.WriteLine($"{employeNumber} {name}");
- }
- private static bool IsEmpty(Dictionary<string, List<string>> positionsEmployes)
- {
- if (positionsEmployes.Count == 0)
- {
- Console.WriteLine("В базе нет сотрудников");
- return true;
- }
- return false;
- }
- private static int CalcEmployeCount(Dictionary<string, List<string>> positionsEmployes)
- {
- int count = 0;
- foreach (string position in positionsEmployes.Keys)
- {
- count += positionsEmployes[position].Count;
- }
- return count;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement