Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Phonebook
- {
- using System;
- using System.Collections.Generic;
- using System.Text;
- public class Startup
- {
- private static Dictionary<string, string> phonebook;
- private static StringBuilder result;
- public static void Main()
- {
- phonebook = new Dictionary<string, string>();
- result = new StringBuilder();
- ReadInputsUntillEndWordAndExecuteAction("search", SplitStringAndSave);
- ReadInputsUntillEndWordAndExecuteAction("stop", SearchNameAndSaveResultToResult);
- Console.Write(result);
- }
- private static void ReadInputsUntillEndWordAndExecuteAction(string endWord, Action<string> toExecute)
- {
- string inputLine = Console.ReadLine();
- while (inputLine != endWord)
- {
- toExecute(inputLine);
- inputLine = Console.ReadLine();
- }
- }
- private static void SearchNameAndSaveResultToResult(string toSearch)
- {
- if (phonebook.ContainsKey(toSearch))
- {
- result.AppendLine($"{toSearch} -> {phonebook[toSearch]}");
- }
- else
- {
- result.AppendLine($"Contact {toSearch} does not exist.");
- }
- }
- private static void SplitStringAndSave(string toSplit)
- {
- string[] contactInfo = toSplit.Split('-');
- if (contactInfo.Length != 2)
- {
- return;
- }
- phonebook[contactInfo[0]] = contactInfo[1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement