Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 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 ConsoleApp9
  8. {
  9. class MobliePhone
  10. {
  11. List<Contact> ContactsList = new List<Contact> { };
  12.  
  13. public void menu()
  14. {
  15. Console.WriteLine("Choose Option:");
  16. Console.WriteLine("0 - Quit");
  17. Console.WriteLine("1 - Print List Of Contatcs");
  18. Console.WriteLine("2 - Add new Contact");
  19. Console.WriteLine("3 - Update Existing Contact");
  20. Console.WriteLine("4 - Remove Exisitng Object");
  21. Console.WriteLine("5 - Find Contact");
  22.  
  23.  
  24. bool MenuOption = true;
  25. while (MenuOption)
  26. {
  27. int number = Convert.ToInt32(Console.ReadLine());
  28. switch (number)
  29. {
  30. case 0:
  31. MenuOption = false;
  32. break;
  33. case 1:
  34. PrintListOfContacts();
  35. break;
  36. case 2:
  37. addNewContact();
  38. break;
  39. case 3:
  40. UpdateExistingContact();
  41. break;
  42. case 4:
  43. RemoveExistingContact();
  44. break;
  45. case 5:
  46. FindContact();
  47. break;
  48.  
  49. }
  50. }
  51. }
  52.  
  53. bool FindContact()
  54. {
  55. Console.WriteLine("Enter Name:");
  56. string name = Console.ReadLine();
  57. Console.WriteLine(FindContact(name));
  58.  
  59. return false;
  60. }
  61. bool FindContact(string name)
  62. {
  63. for (int i = 0; i < ContactsList.Count; i++)
  64. {
  65. if (ContactsList[i].name == name)
  66. {
  67. Console.WriteLine("Contact:" + name + "On position:" + i);
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73.  
  74. private void RemoveExistingContact()
  75. {
  76. string imie;
  77. string number;
  78. Console.WriteLine("Enter Name:");
  79. imie = Console.ReadLine();
  80.  
  81. int ifDel = 0;
  82.  
  83. foreach (var item in ContactsList)
  84. {
  85. if (item.name.Equals(imie))
  86. {
  87. ContactsList.Remove(item);
  88. ifDel++;
  89. break;
  90. }
  91.  
  92. }
  93. if (ifDel == 0)
  94. {
  95. Console.WriteLine("Contact dosent exsist");
  96. }
  97.  
  98. }
  99.  
  100. private void UpdateExistingContact()
  101. {
  102. string imie;
  103. string number;
  104. Console.WriteLine("Enter Name:");
  105. imie = Console.ReadLine();
  106. int ifEdit = 0;
  107. foreach (var item in ContactsList)
  108. {
  109. if (item.name == imie)
  110. {
  111. Console.WriteLine("Enter new name");
  112. string newName = Console.ReadLine();
  113. item.name = newName;
  114. Console.WriteLine("Enter new phone number");
  115. string newPN = Console.ReadLine();
  116. item.tel = newPN;
  117. ifEdit++;
  118.  
  119. }
  120. }
  121. if (ifEdit == 0)
  122. {
  123. Console.WriteLine("Contact dosent exist");
  124. }
  125. }
  126.  
  127. private void addNewContact()
  128. {
  129.  
  130. string imie;
  131. string number;
  132. Console.WriteLine("Enter Name:");
  133. imie = Console.ReadLine();
  134. Console.WriteLine("Enter phone number");
  135. number = Console.ReadLine();
  136. if (FindContact(imie) == true)
  137. {
  138. Console.WriteLine("Contact already exist");
  139. }
  140. else
  141. {
  142. ContactsList.Add(new Contact(imie, number));
  143. }
  144.  
  145. }
  146.  
  147.  
  148. private void PrintListOfContacts()
  149. {
  150. foreach (var item in ContactsList)
  151. {
  152. Console.WriteLine("Tel and Name" + item.tel +" "+ item.name);
  153. }
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement