Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that receives some info from the console about people and their phone numbers.
- * 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).
- * 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. */
- namespace _07.Phonebook
- {
- using System;
- using System.Collections.Generic;
- class Phonebook
- {
- static void Main(string[] args)
- {
- var phonebook = new Dictionary<string, HashSet<string>>();
- string textLine;
- FillPhonebookWithContacts(phonebook);
- List<string> contacts = new List<string>();
- textLine = Console.ReadLine().Trim();
- while (textLine != string.Empty)
- {
- contacts.Add(textLine);
- textLine = Console.ReadLine().Trim();
- }
- GetContactFromPhonebook(phonebook, contacts);
- }
- private static void FillPhonebookWithContacts(Dictionary<string, HashSet<string>> phonebook)
- {
- string textLine = Console.ReadLine().Trim();
- while (textLine != "search")
- {
- string[] entries = textLine.Split('-');
- if (entries.Length != 2)
- {
- textLine = Console.ReadLine().Trim();
- continue;
- }
- string key = entries[0].Trim();
- string value = entries[1].Trim();
- if (!phonebook.ContainsKey(key))
- {
- phonebook[key] = new HashSet<string>();
- }
- phonebook[key].Add(value);
- textLine = Console.ReadLine().Trim();
- }
- }
- private static void GetContactFromPhonebook(Dictionary<string, HashSet<string>> phonebook, List<string> contacts)
- {
- foreach (string contact in contacts)
- {
- if (phonebook.ContainsKey(contact))
- {
- Console.WriteLine("{0} -> {1}", contact, string.Join(", ", phonebook[contact]));
- }
- else
- {
- Console.WriteLine("Contact " + contact + " does not exist.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement