Advertisement
Vdzhambazova

Phonebook

Nov 10th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. //Write a program that receives some info from the console about people and their phone numbers.
  8. //You are free to choose the manner in which the data is entered; each entry should have just one name and one number
  9. //(both of them strings).
  10. //After filling this simple phonebook, upon receiving the command "search", your program should be able to perform
  11. //a search of a contact by name and print her details in format "{name} -> {number}". In case the contact isn't found, print
  12. //"Contact {name} does not exist."
  13.  
  14. class Phonebook
  15. {
  16.  
  17.     static void Main()
  18.     {
  19.         Dictionary<string, string> phonebook = new Dictionary<string, string>();
  20.         string contactName = "";
  21.         string contactNumber = "";
  22.         while (true)
  23.         {
  24.             string[] contact = Console.ReadLine().Split(new char[] { '-' }).ToArray();
  25.             if (contact[0] != "search" && contact.Length >= 2)
  26.             {
  27.                 contactName = contact[0];
  28.                 contactNumber = contact[1];
  29.  
  30.                 phonebook.Add(contactName, contactNumber);
  31.             }
  32.  
  33.             else
  34.             {
  35.                 while (contact[0] == "search")
  36.                 {
  37.                     string searchName = Console.ReadLine();
  38.  
  39.                     if (phonebook.ContainsKey(searchName))
  40.                     {
  41.                         Console.WriteLine("{0} -> {1}", searchName, phonebook[searchName]);
  42.                         contact[0] = "search";
  43.                     }
  44.                     else
  45.                     {
  46.                         Console.WriteLine("Contact {0} does not exist.", searchName);
  47.                         contact[0] = "search";
  48.                     }
  49.  
  50.                 }
  51.             }
  52.  
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement