Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Everything
- {
- class Program
- {
- //method for getting integers and avoid exceptions
- private static void input(ref int a, ref bool c)
- {
- while (c)//if c = true then it was not the intinput
- {
- try
- {
- c = false;//if every thing c = false and it goes out from loop
- a = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception)
- {
- c = true;//if there was an exeption (str iput) c = true and the loop continues
- Console.WriteLine("Your input was incorrect");//tels to user that hi was misstaken
- Console.WriteLine("Try once more");
- }
- }
- c = true;//at th end c = true
- }
- //method just wtites a command list
- private static void commandsReminder()
- {
- Console.WriteLine("1 - Add a key and value");
- Console.WriteLine("2 - Delete a key value pair");
- Console.WriteLine("3 - Find a key or value");
- Console.WriteLine("4 - Print all pairs");
- Console.WriteLine("5 - Terminate the proces");
- }
- //asks user to give a key and a value for pair (1)
- private static void addAKeyValuePair(ref Dictionary<int, string> dictionary, ref bool checker)
- {
- Console.WriteLine("Add a key value pair: ");
- Console.WriteLine(new string('-', 50));
- int key = 0;
- string value = "";
- checker = true;
- Console.WriteLine("Give a key (int)");
- input(ref key, ref checker);//the key shoul be int
- if (!dictionary.ContainsKey(key))//if there was no such key every thing is OK and we add a pair and tell to user that pair was added
- {
- Console.WriteLine("Give a value (string)");
- value = Console.ReadLine();
- dictionary.Add(key, value);
- Console.WriteLine("{2}\nKey = {0}, value = {1}\nWas added\n{2}", key, dictionary[key], new string('-', 50));
- Console.Write("Press any key");
- Console.ReadKey();
- Console.WriteLine();
- }
- else//if key existed we tell to user about it and finish the proces
- {
- Console.WriteLine("This key is already exists");
- Console.Write("Press any key");
- Console.ReadKey();
- Console.WriteLine();
- }
- }
- //gets a key and delets the pair with its key
- private static void removeAKeyValuePair(ref Dictionary<int, string> dictionary, ref bool checker)
- {
- if (dictionary.Count == 0)
- Console.WriteLine("There is no key value pairs");
- else
- {
- Console.WriteLine("Remove a pair");
- Console.WriteLine(new string('-', 50));
- int key = 0;
- Console.WriteLine("Give a key of pair you want to delete");
- input(ref key, ref checker);
- if (dictionary.ContainsKey(key))
- {
- Console.WriteLine("{2}\nKey = {0}, value = {1}\nWas deleted\n{2}", key, dictionary[key], new string('-', 50));
- Console.Write("Press any key");
- dictionary.Remove(key);
- Console.ReadKey();
- Console.WriteLine();
- }
- else
- {
- Console.WriteLine("There is no such key");
- Console.Write("Press any key");
- Console.ReadKey();
- Console.WriteLine();
- }
- }
- }
- //returns the value if the key exists
- private static void findKeyValuePair(ref Dictionary<int, string> dictionary, ref bool checker)
- {
- if (dictionary.Count == 0)
- Console.WriteLine("There is no key value pairs");
- else
- {
- Console.WriteLine("Find a value by key");
- Console.WriteLine(new string('-', 50));
- int key = 0;
- string value;
- Console.WriteLine("Input the key you are looking for");
- input(ref key, ref checker);
- if (dictionary.TryGetValue(key, out value))
- {
- Console.WriteLine("{1}\nValue = {0}\nWas found\n{1}", value, new string('-', 50));
- Console.Write("Press any key");
- Console.ReadKey();
- Console.WriteLine();
- }
- else
- {
- Console.WriteLine("The key is not found");
- Console.Write("Press any key");
- Console.ReadKey();
- Console.WriteLine();
- }
- }
- }
- //prinst all pairs which exists
- private static void printDictionary(Dictionary<int, string> d)
- {
- if (d.Count == 0)
- Console.WriteLine("There is no key value pairs");
- else
- {
- Console.WriteLine("Print all pairs");
- Console.WriteLine(new string('-', 50));
- foreach (KeyValuePair<int, string> p in d)
- {
- Console.WriteLine("Key = {0}, value = {1}", p.Key, p.Value);
- }
- Console.WriteLine(new string('-', 50));
- Console.Write("Press any key");
- Console.ReadKey();
- Console.WriteLine();
- }
- }
- static void Main()
- {
- Dictionary<int, string> dictionary = new Dictionary<int, string>();
- int command = 0;
- bool mainChecker = true;
- Console.WriteLine("Hello, this is a Key Value Pair.");
- Console.WriteLine("To make your life easier each command will be just a number-key");
- Console.WriteLine("So, now you have an empty Key Value Pair, what do you want to do?");
- commandsReminder();
- Console.WriteLine(new string('=', 50));
- do
- {
- input(ref command, ref mainChecker);
- switch (command)
- {
- case 1:
- addAKeyValuePair(ref dictionary, ref mainChecker);
- break;
- case 2:
- removeAKeyValuePair(ref dictionary, ref mainChecker);
- break;
- case 3:
- findKeyValuePair(ref dictionary, ref mainChecker);
- break;
- case 4:
- printDictionary(dictionary);
- break;
- case 5:
- break;
- default:
- Console.WriteLine("You have no such command");
- break;
- }
- Console.WriteLine(new string('=', 50));
- //check if user what to exit, he'll not get a reminder
- if (command != 5)
- {
- commandsReminder();
- Console.WriteLine("What's next?");
- }
- } while (command != 5); //goes out of loop
- Console.WriteLine("Good bey");
- Console.Write("Press any key");
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment