Advertisement
alidzhikov

Phonebook

May 8th, 2015
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. class Phonebook
  4. {
  5.     static void Main()
  6.     {
  7.         Dictionary<string, List<string>> phonebook = new Dictionary<string, List<string>>();
  8.  
  9.         string contactInformation = Console.ReadLine();
  10.         string[] contactTokens;
  11.         string contactName = String.Empty;
  12.         string contactNumber = String.Empty;
  13.  
  14.         while (contactInformation != "search")
  15.         {
  16.             contactTokens = contactInformation.Split(new char[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
  17.             contactName = contactTokens[0];
  18.             contactNumber = contactTokens[1];
  19.             if (!phonebook.ContainsKey(contactName))
  20.             {
  21.                 phonebook[contactName] = new List<string>();
  22.                
  23.             }
  24.             phonebook[contactName].Add(contactNumber);
  25.  
  26.             contactInformation = Console.ReadLine();
  27.         }
  28.  
  29.         string contactNameToSearch = Console.ReadLine();
  30.         while (!String.IsNullOrEmpty(contactNameToSearch))
  31.         {
  32.             if (phonebook.ContainsKey(contactNameToSearch))
  33.             {
  34.                 Console.Write("{0} -> {1}", contactNameToSearch, String.Join(", ", phonebook[contactNameToSearch]));
  35.             }
  36.             else
  37.             {
  38.                 Console.WriteLine("Contact {0} does not exist.", contactNameToSearch);
  39.             }
  40.  
  41.             contactNameToSearch = Console.ReadLine();
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement