peshopbs2

Untitled

Mar 12th, 2020
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace PhonebookCommands
  8. {
  9. class Program
  10. {
  11. private static Dictionary<string, string> phonebook;
  12.  
  13. static void Main(string[] args)
  14. {
  15. phonebook = new Dictionary<string, string>();
  16.  
  17. Input();
  18. }
  19.  
  20. private static void Input()
  21. {
  22. string command = Console.ReadLine();
  23. while(command!="End")
  24. {
  25. ProcessCommand(command);
  26. command = Console.ReadLine();
  27. }
  28. }
  29.  
  30. private static void ProcessCommand(string command)
  31. {
  32. string[] commandData = command
  33. .Split(' ')
  34. .ToArray();
  35.  
  36. switch (commandData[0])
  37. {
  38. case "Add":
  39. Add(commandData);
  40. break;
  41. case "SearchByName":
  42. SearchByName(commandData);
  43. break;
  44. case "SearchByPhone":
  45. SearchByPhone(commandData);
  46. break;
  47. case "RemoveByName":
  48. RemoveByName(commandData);
  49. break;
  50. case "RemoveByPhone":
  51. RemoveByPhone(commandData);
  52. break;
  53. case "PrintAll":
  54. PrintAll(commandData);
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60.  
  61. private static void PrintAll(string[] commandData)
  62. {
  63. foreach (var contact in phonebook)
  64. {
  65. Console.WriteLine(contact);
  66. }
  67. }
  68.  
  69. private static void RemoveByPhone(string[] commandData)
  70. {
  71. string phone = commandData[1];
  72. string name = "";
  73. foreach (var contact in phonebook)
  74. {
  75. string contactPhone = contact.Value;
  76. if (contactPhone == phone)
  77. {
  78. name = contact.Key; //the .Key is the contact name!
  79. }
  80. }
  81.  
  82. phonebook.Remove(name);
  83. }
  84.  
  85. private static void RemoveByName(string[] commandData)
  86. {
  87. string name = commandData[1];
  88. phonebook.Remove(name);
  89. }
  90.  
  91. private static void SearchByPhone(string[] commandData)
  92. {
  93. throw new NotImplementedException();
  94. }
  95.  
  96. private static void SearchByName(string[] commandData)
  97. {
  98. string name = commandData[1];
  99.  
  100. if (phonebook.ContainsKey(name))
  101. {
  102. //имаме такъв човек
  103. Console.WriteLine("{0} -> {1}",
  104. name,
  105. phonebook[name]);
  106. }
  107. else
  108. {
  109. //нямаме такъв човек
  110. Console.WriteLine($"Contact {name} does not exist.");
  111. }
  112. }
  113.  
  114. private static void Add(string[] commandData)
  115. {
  116. //Add Gosho 08888888888888
  117. string name = commandData[1];
  118. string phone = commandData[2];
  119. //1. проверяваме дали имаме вече такъв човек
  120. if (phonebook.ContainsKey(name))
  121. {
  122. //ако имаме - сменяме номера
  123. phonebook[name] = phone;
  124. }
  125. else
  126. {
  127. //ако нямаме - добавяме човека с номера
  128. phonebook.Add(name, phone);
  129. }
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment