Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 17, 2023 07:36
Show Gist options
  • Save Strelok78/769d2d837f6b8b94da5c20152b656ec7 to your computer and use it in GitHub Desktop.
Save Strelok78/769d2d837f6b8b94da5c20152b656ec7 to your computer and use it in GitHub Desktop.
Personnel accounting using Dictionary
using System.ComponentModel.Design;
namespace iJuniorRefactoring
{
internal class Program
{
public static void Main()
{
const string CommandAdd = "1";
const string CommandShowEmployees = "2";
const string CommandRemove = "3";
const string CommandExit = "4";
bool isOpen = true;
string enteredText;
string menuText = $"Press\n" +
$"{CommandAdd} - to add an employee\n" +
$"{CommandShowEmployees} - to get a list of employees\n" +
$"{CommandRemove} - to remove an employee from base\n" +
$"{CommandExit} - to leave an application";
Dictionary<string, string> employees = new Dictionary<string, string>();
while (isOpen)
{
Console.WriteLine(menuText);
Console.SetCursorPosition(0, 7);
enteredText = Console.ReadLine();
switch (enteredText)
{
case CommandAdd:
AddEmployee(employees);
break;
case CommandShowEmployees:
ShowEmployeesList(employees);
break;
case CommandRemove:
DeleteEmployee(employees);
break;
case CommandExit:
isOpen = ExitApplication();
break;
default:
Console.WriteLine("Incorrect command");
break;
}
Console.WriteLine("\n\nPress any key to continue...");
Console.ReadKey();
Console.Clear();
}
}
static void AddEmployee(Dictionary<string, string> dictionary)
{
string name;
string position;
Console.WriteLine("Enter name:");
name = Console.ReadLine();
Console.WriteLine("Enter position:");
position = Console.ReadLine();
if (dictionary.ContainsKey(name) == false)
dictionary.Add(name, position);
else
Console.WriteLine("That person is already in the base!");
}
static void ShowEmployeesList(Dictionary<string, string> dictionary)
{
if (dictionary.Any())
{
foreach (string key in dictionary.Keys)
Console.Write($"{key} - {dictionary[key]}; ");
}
else
{
Console.WriteLine("List is empty!");
}
}
static void DeleteEmployee(Dictionary<string, string> dictionary)
{
if (dictionary.Count != 0)
{
Console.Write("Enter employees name that needs to be removed: ");
dictionary.Remove(Console.ReadLine());
}
else
{
Console.WriteLine("Base is empty!");
}
}
static bool ExitApplication()
{
Console.WriteLine("Good bye!");
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment