Advertisement
NelIfandieva

Phonebook_Dictionaries_Ex02

Feb 26th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Feb26_2018
  6. {
  7.     class MainClass
  8.     {
  9.         /* */
  10.  
  11.         public static void Main()
  12.         {
  13.             //int k = int.Parse(Console.ReadLine());
  14.             //var input = Console.ReadLine()
  15.             //                   .Split()
  16.             //                   .Select(double.Parse)
  17.             //                   .ToList();
  18.  
  19.             //var firstK = input.Take(k);
  20.             //var asd = input.ToArray().Reverse();
  21.             //Console.WriteLine(asd);
  22.             char[] sep = {' '};
  23.             string input;
  24.             SortedDictionary<string, string> phoneBook = new SortedDictionary<string, string>();
  25.             while((input = Console.ReadLine()) != "END")
  26.             {
  27.                 string[] commands = input.Split(sep, StringSplitOptions.RemoveEmptyEntries);
  28.  
  29.                 if(commands[0] == "A")
  30.                 {
  31.                     if(phoneBook.ContainsKey(commands[1]))
  32.                     {
  33.                         phoneBook[commands[1]] = commands[2];
  34.                     }
  35.                     else
  36.                     {
  37.                         phoneBook.Add(commands[1], commands[2]);
  38.                     }
  39.                 }
  40.                 else if(commands[0] == "S")
  41.                 {
  42.                     if(phoneBook.ContainsKey(commands[1]))
  43.                     {
  44.                         var name = commands[1];
  45.                         var number = phoneBook[commands[1]];
  46.                         Console.WriteLine("{0} -> {1}", name, number);
  47.                     }
  48.                     else
  49.                     {
  50.                         Console.WriteLine("Contact {0} does not exist.", commands[1]);
  51.                     }
  52.                 }
  53.                 else if(commands[0] == "ListAll")
  54.                 {
  55.                     foreach(var contact in phoneBook)
  56.                     {
  57.                         var name = contact.Key;
  58.                         var number = contact.Value;
  59.                         Console.WriteLine("{0} -> {1}", name, number);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement