GrandtherAzaMarks

DictionaryApp

Apr 30th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Everything
  5. {
  6.     class Program
  7.     {
  8.         //method for getting integers and avoid exceptions
  9.         private static void input(ref int a, ref bool c)
  10.         {
  11.             while (c)//if c = true then it was not the intinput
  12.             {
  13.                 try
  14.                 {
  15.                     c = false;//if every thing c = false and it goes out from loop
  16.                     a = Convert.ToInt32(Console.ReadLine());
  17.                 }
  18.                 catch (Exception)
  19.                 {
  20.                     c = true;//if there was an exeption (str iput) c = true and the loop continues
  21.                     Console.WriteLine("Your input was incorrect");//tels to user that hi was misstaken
  22.                     Console.WriteLine("Try once more");
  23.                 }
  24.             }
  25.             c = true;//at th end c = true
  26.         }
  27.  
  28.         //method just wtites a command list
  29.         private static void commandsReminder()
  30.         {
  31.             Console.WriteLine("1 - Add a key and value");
  32.             Console.WriteLine("2 - Delete a key value pair");
  33.             Console.WriteLine("3 - Find a key or value");
  34.             Console.WriteLine("4 - Print all pairs");
  35.             Console.WriteLine("5 - Terminate the proces");
  36.         }
  37.  
  38.         //asks user to give a key and a value for pair (1)
  39.         private static void addAKeyValuePair(ref Dictionary<int, string> dictionary, ref bool checker)
  40.         {
  41.             Console.WriteLine("Add a key value pair: ");
  42.             Console.WriteLine(new string('-', 50));
  43.             int key = 0;
  44.             string value = "";
  45.             checker = true;
  46.             Console.WriteLine("Give a key (int)");
  47.             input(ref key, ref checker);//the key shoul be int
  48.  
  49.             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
  50.             {
  51.                 Console.WriteLine("Give a value (string)");
  52.                 value = Console.ReadLine();
  53.                 dictionary.Add(key, value);
  54.                 Console.WriteLine("{2}\nKey = {0}, value = {1}\nWas added\n{2}", key, dictionary[key], new string('-', 50));
  55.                 Console.Write("Press any key");
  56.                 Console.ReadKey();
  57.                 Console.WriteLine();
  58.             }
  59.             else//if key existed we tell to user about it and finish the proces
  60.             {
  61.                 Console.WriteLine("This key is already exists");
  62.                 Console.Write("Press any key");
  63.                 Console.ReadKey();
  64.                 Console.WriteLine();
  65.             }
  66.  
  67.         }
  68.  
  69.         //gets a key and delets the pair with its key
  70.         private static void removeAKeyValuePair(ref Dictionary<int, string> dictionary, ref bool checker)
  71.         {
  72.             if (dictionary.Count == 0)
  73.                 Console.WriteLine("There is no key value pairs");
  74.             else
  75.             {
  76.                 Console.WriteLine("Remove a pair");
  77.                 Console.WriteLine(new string('-', 50));
  78.                 int key = 0;
  79.                 Console.WriteLine("Give a key of pair you want to delete");
  80.                 input(ref key, ref checker);
  81.                 if (dictionary.ContainsKey(key))
  82.                 {
  83.                     Console.WriteLine("{2}\nKey = {0}, value = {1}\nWas deleted\n{2}", key, dictionary[key], new string('-', 50));
  84.                     Console.Write("Press any key");
  85.                     dictionary.Remove(key);
  86.                     Console.ReadKey();
  87.                     Console.WriteLine();
  88.                 }
  89.                 else
  90.                 {
  91.                     Console.WriteLine("There is no such key");
  92.                     Console.Write("Press any key");
  93.                     Console.ReadKey();
  94.                     Console.WriteLine();
  95.                 }
  96.             }
  97.         }
  98.  
  99.         //returns the value if the key exists
  100.         private static void findKeyValuePair(ref Dictionary<int, string> dictionary, ref bool checker)
  101.         {
  102.             if (dictionary.Count == 0)
  103.                 Console.WriteLine("There is no key value pairs");
  104.             else
  105.             {
  106.                 Console.WriteLine("Find a value by key");
  107.                 Console.WriteLine(new string('-', 50));
  108.                 int key = 0;
  109.                 string value;
  110.                 Console.WriteLine("Input the key you are looking for");
  111.                 input(ref key, ref checker);
  112.                 if (dictionary.TryGetValue(key, out value))
  113.                 {
  114.                     Console.WriteLine("{1}\nValue = {0}\nWas found\n{1}", value, new string('-', 50));
  115.                     Console.Write("Press any key");
  116.                     Console.ReadKey();
  117.                     Console.WriteLine();
  118.                 }
  119.                 else
  120.                 {
  121.                     Console.WriteLine("The key is not found");
  122.                     Console.Write("Press any key");
  123.                     Console.ReadKey();
  124.                     Console.WriteLine();
  125.                 }
  126.             }
  127.         }
  128.  
  129.         //prinst all pairs which exists
  130.         private static void printDictionary(Dictionary<int, string> d)
  131.         {
  132.             if (d.Count == 0)
  133.                 Console.WriteLine("There is no key value pairs");
  134.             else
  135.             {
  136.                 Console.WriteLine("Print all pairs");
  137.                 Console.WriteLine(new string('-', 50));
  138.                 foreach (KeyValuePair<int, string> p in d)
  139.                 {
  140.                     Console.WriteLine("Key = {0}, value = {1}", p.Key, p.Value);
  141.                 }
  142.                 Console.WriteLine(new string('-', 50));
  143.                 Console.Write("Press any key");
  144.                 Console.ReadKey();
  145.                 Console.WriteLine();
  146.             }
  147.         }
  148.  
  149.         static void Main()
  150.         {
  151.             Dictionary<int, string> dictionary = new Dictionary<int, string>();
  152.             int command = 0;
  153.             bool mainChecker = true;
  154.             Console.WriteLine("Hello, this is a Key Value Pair.");
  155.             Console.WriteLine("To make your life easier each command will be just a number-key");
  156.             Console.WriteLine("So, now you have an empty Key Value Pair, what do you want to do?");
  157.             commandsReminder();
  158.             Console.WriteLine(new string('=', 50));
  159.             do
  160.             {
  161.                 input(ref command, ref mainChecker);
  162.                 switch (command)
  163.                 {
  164.                     case 1:
  165.                         addAKeyValuePair(ref dictionary, ref mainChecker);
  166.                         break;
  167.                     case 2:
  168.                         removeAKeyValuePair(ref dictionary, ref mainChecker);
  169.                         break;
  170.                     case 3:
  171.                         findKeyValuePair(ref dictionary, ref mainChecker);
  172.                         break;
  173.                     case 4:
  174.                         printDictionary(dictionary);
  175.                         break;
  176.                     case 5:
  177.                         break;
  178.                     default:
  179.                         Console.WriteLine("You have no such command");
  180.                         break;
  181.                 }
  182.                 Console.WriteLine(new string('=', 50));
  183.  
  184.                 //check if user what to exit, he'll not get a reminder
  185.                 if (command != 5)
  186.                 {
  187.                     commandsReminder();
  188.                     Console.WriteLine("What's next?");
  189.                 }
  190.             } while (command != 5); //goes out of loop
  191.             Console.WriteLine("Good bey");
  192.             Console.Write("Press any key");
  193.             Console.ReadKey();
  194.         }
  195.     }
  196. }
Add Comment
Please, Sign In to add comment