Advertisement
dimipan80

Phonebook

May 10th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. /* Write a program that receives some info from the console about people and their phone numbers.
  2.  * You are free to choose the manner in which the data is entered; each entry should have just one name and one number (both of them strings).
  3.  * After filling this simple phonebook, upon receiving the command "search", your program should be able to perform a search of a contact by name and print her details in format "{name} -> {number}". In case the contact isn't found, print "Contact {name} does not exist. */
  4.  
  5. namespace _07.Phonebook
  6. {
  7.     using System;
  8.     using System.Collections.Generic;
  9.  
  10.     class Phonebook
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var phonebook = new Dictionary<string, HashSet<string>>();
  15.             string textLine;
  16.             FillPhonebookWithContacts(phonebook);
  17.  
  18.             List<string> contacts = new List<string>();
  19.             textLine = Console.ReadLine().Trim();
  20.             while (textLine != string.Empty)
  21.             {
  22.                 contacts.Add(textLine);
  23.                 textLine = Console.ReadLine().Trim();
  24.             }
  25.  
  26.             GetContactFromPhonebook(phonebook, contacts);
  27.         }
  28.  
  29.         private static void FillPhonebookWithContacts(Dictionary<string, HashSet<string>> phonebook)
  30.         {
  31.             string textLine = Console.ReadLine().Trim();
  32.             while (textLine != "search")
  33.             {
  34.                 string[] entries = textLine.Split('-');
  35.                 if (entries.Length != 2)
  36.                 {
  37.                     textLine = Console.ReadLine().Trim();
  38.                     continue;
  39.                 }
  40.  
  41.                 string key = entries[0].Trim();
  42.                 string value = entries[1].Trim();
  43.                 if (!phonebook.ContainsKey(key))
  44.                 {
  45.                     phonebook[key] = new HashSet<string>();
  46.                 }
  47.  
  48.                 phonebook[key].Add(value);
  49.                 textLine = Console.ReadLine().Trim();
  50.             }
  51.         }
  52.  
  53.         private static void GetContactFromPhonebook(Dictionary<string, HashSet<string>> phonebook, List<string> contacts)
  54.         {
  55.             foreach (string contact in contacts)
  56.             {
  57.                 if (phonebook.ContainsKey(contact))
  58.                 {
  59.                     Console.WriteLine("{0} -> {1}", contact, string.Join(", ", phonebook[contact]));
  60.                 }
  61.                 else
  62.                 {
  63.                     Console.WriteLine("Contact " + contact + " does not exist.");
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement