Advertisement
simonradev

PhonebookUpdate

May 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. namespace Phonebook
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Text;
  6.  
  7.     public class Startup
  8.     {
  9.         private static Dictionary<string, string> phonebook;
  10.         private static StringBuilder result;
  11.  
  12.         public static void Main()
  13.         {
  14.             phonebook = new Dictionary<string, string>();
  15.             result = new StringBuilder();
  16.  
  17.             ReadInputsUntillEndWordAndExecuteAction("search", SplitStringAndSave);
  18.             ReadInputsUntillEndWordAndExecuteAction("stop", SearchNameAndSaveResultToResult);
  19.  
  20.             Console.Write(result);
  21.         }
  22.  
  23.         private static void ReadInputsUntillEndWordAndExecuteAction(string endWord, Action<string> toExecute)
  24.         {
  25.             string inputLine = Console.ReadLine();
  26.             while (inputLine != endWord)
  27.             {
  28.                 toExecute(inputLine);
  29.  
  30.                 inputLine = Console.ReadLine();
  31.             }
  32.         }
  33.  
  34.         private static void SearchNameAndSaveResultToResult(string toSearch)
  35.         {
  36.             if (phonebook.ContainsKey(toSearch))
  37.             {
  38.                 result.AppendLine($"{toSearch} -> {phonebook[toSearch]}");
  39.             }
  40.             else
  41.             {
  42.                 result.AppendLine($"Contact {toSearch} does not exist.");
  43.             }
  44.         }
  45.  
  46.         private static void SplitStringAndSave(string toSplit)
  47.         {
  48.             string[] contactInfo = toSplit.Split('-');
  49.  
  50.             if (contactInfo.Length != 2)
  51.             {
  52.                 return;
  53.             }
  54.  
  55.             phonebook[contactInfo[0]] = contactInfo[1];
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement