Advertisement
YavorGrancharov

Phonebook2(dict)

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