Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. namespace struct_dictionary
  2. {
  3.     class Dctnr {
  4.         int num = 0;
  5.         Dictionary<int, string> wishes = new Dictionary<int, string>();
  6.         public void addingEl(string str) {
  7.             num += 1;
  8.             wishes.Add(num, str);
  9.         }
  10.         public void removingEl(int key) {
  11.             if (key > 0)
  12.             {
  13.                 wishes.Remove(key);
  14.             }
  15.             else {
  16.                 Console.WriteLine("You've written an inappropriate number");
  17.             }
  18.         }
  19.         public void showingEl() {
  20.             foreach (int i in wishes.Keys) {
  21.                 Console.WriteLine(i + " " + wishes[i]);
  22.             }
  23.         }
  24.         public void countingEl() {
  25.             int count = wishes.Count();
  26.             Console.WriteLine(count);
  27.         }
  28.     }
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             Console.ForegroundColor = ConsoleColor.Yellow;
  34.             Console.WriteLine("The dictionary of wishes");
  35.             Console.ResetColor();
  36.  
  37.             Console.WriteLine("Please enter an appropriate number where:" +
  38.                    "\n 1 - adding one new element to the dictionary;" +
  39.                    "\n 2 - removing one element from the dictionary;" +
  40.                    "\n 3 - showing all the elements of the dictionary;" +
  41.                    "\n 4 - counting all the elements of the dictionary.");
  42.  
  43.             Dctnr dctnr = new Dctnr();
  44.  
  45.             while (true)
  46.             {
  47.                 int choice = Convert.ToInt32(Console.ReadLine());
  48.                 switch (choice)
  49.                 {
  50.                     case 1:
  51.                         Console.WriteLine("Please, input the new wish");
  52.                         dctnr.addingEl(Console.ReadLine());
  53.                         break;
  54.  
  55.                     case 2:
  56.                         Console.WriteLine("Please, input the number of the wish");
  57.                         dctnr.removingEl(Convert.ToInt32(Console.ReadLine()));
  58.                         break;
  59.  
  60.                     case 3:
  61.                         dctnr.showingEl();
  62.                         break;
  63.  
  64.                     case 4:
  65.                         dctnr.countingEl();
  66.                         break;
  67.  
  68.                     default:
  69.                         Console.WriteLine("unknown command; try again");
  70.                         break;
  71.                 }
  72.             }
  73.             Console.ReadKey();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement