Advertisement
simonradev

Phonebook

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