Advertisement
YavorGrancharov

Phonebook_Upgrade(dict)

Oct 17th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Phonebook_Upgrade
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.             SortedDictionary<string, string> data =
  13.                 new SortedDictionary<string, string>();
  14.  
  15.  
  16.             while (input != "END")
  17.             {
  18.                 if (input == "ListAll")
  19.                 {
  20.                     foreach (var record in data)
  21.                     {
  22.                         Console.WriteLine("{0} -> {1}", record.Key, record.Value);
  23.                     }
  24.                 }
  25.  
  26.                 string[] tokens = input.Split(' ').ToArray();
  27.                 string ch = tokens[0];
  28.  
  29.                 if (tokens.Length == 3)
  30.                 {
  31.                     if (ch == "A")
  32.                     {
  33.                         string name = tokens[1];
  34.                         string number = tokens[2];
  35.                         if (!data.ContainsKey(name))
  36.                         {
  37.                             data[name] = number;
  38.                         }
  39.                         data[name] = number;
  40.                     }
  41.                 }
  42.                 else if (tokens.Length == 2)
  43.                 {
  44.                     if (ch == "S")
  45.                     {
  46.                         string name = tokens[1];
  47.                         foreach (var record in data)
  48.                         {
  49.                             if (record.Key == name)
  50.                             {
  51.                                 Console.WriteLine("{0} -> {1}", record.Key, record.Value);
  52.                             }
  53.                         }
  54.                         if (!data.ContainsKey(name))
  55.                         {
  56.                             Console.WriteLine("Contact {0} does not exist.", name);
  57.                         }
  58.                     }
  59.                 }                
  60.                 input = Console.ReadLine();
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement