Advertisement
YavorJS

Phonebook and Phonebook Upgrade

Aug 30th, 2016
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 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 _12.Phonebook_Upgrade
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var phonebook = new SortedDictionary<string, string>();
  14. string command = "";
  15. while (true)
  16. {
  17. string[] entry = Console.ReadLine().Split(' ').ToArray();
  18. command = entry[0];
  19. if (command == "END")
  20. {
  21. break;
  22. }
  23. string name = "";
  24. string phone = "";
  25.  
  26. if (entry.Length >= 2)
  27. {
  28. name = entry[1];
  29. }
  30.  
  31. if (entry.Length == 3)
  32. {
  33. phone = entry[2];
  34. }
  35.  
  36. if (command == "A")
  37. {
  38. if (phonebook.ContainsKey(name) && !phonebook.ContainsValue(phone))
  39. {
  40. phonebook.Remove(name);
  41. phonebook.Add(name, phone);
  42. }
  43. else if (!phonebook.ContainsKey(name) && !phonebook.ContainsValue(phone))
  44. {
  45. phonebook.Add(name, phone);
  46. }
  47. }
  48. else if (command == "S")
  49. {
  50. if (!phonebook.ContainsKey(name))
  51. {
  52. Console.WriteLine($"Contact {name} does not exist.");
  53. }
  54. else if (phonebook.ContainsKey(name))
  55. {
  56. foreach (var item in phonebook)
  57. {
  58. if (item.Key == name)
  59. {
  60. Console.WriteLine($"{item.Key} -> {item.Value}");
  61. }
  62. }
  63. }
  64. }
  65. else if (command == "ListAll")
  66. {
  67. foreach (var item in phonebook)
  68. {
  69. Console.WriteLine($"{item.Key} -> {item.Value}");
  70. }
  71.  
  72. }
  73.  
  74. }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement