Guest User

Untitled

a guest
Nov 14th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp14
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. MobilePhone mobilePhone = new MobilePhone();
  11.  
  12. mobilePhone.Menu();
  13.  
  14. }
  15. }
  16. class MobilePhone
  17. {
  18. List<Contatcs> ListOfContants = new List<Contatcs>();
  19.  
  20.  
  21. public static void PrintMenu()
  22. {
  23. Console.ForegroundColor = ConsoleColor.Green;
  24. Console.WriteLine();
  25. Console.WriteLine("Choose Option:");
  26. Console.WriteLine("0 - Quit");
  27. Console.WriteLine("1 - Print List Of Contatcs");
  28. Console.WriteLine("2 - Add new Contact");
  29. Console.WriteLine("3 - Update Existing Contact");
  30. Console.WriteLine("4 - Remove Exisitng Object");
  31. Console.WriteLine("5 - Find Contact");
  32. Console.WriteLine();
  33. Console.ResetColor();
  34. }
  35. public void Menu()
  36. {
  37.  
  38. PrintMenu();
  39. bool MenuOption = true;
  40. while (MenuOption)
  41. {
  42. int number = int.Parse(Console.ReadLine());
  43. switch (number)
  44. {
  45. case 0:
  46. MenuOption = false;
  47. break;
  48. case 1:
  49. PrintListOfContacts();
  50. break;
  51. case 2:
  52. addNewContact();
  53. break;
  54. case 3:
  55. UpdateExistingContact();
  56. break;
  57. case 4:
  58. RemoveExistingContact();
  59. break;
  60. case 5:
  61. FindContact();
  62. break;
  63. }
  64. PrintMenu();
  65.  
  66. }
  67. }
  68. private void PrintListOfContacts()
  69. {
  70. foreach (var item in ListOfContants)
  71. {
  72. Console.WriteLine(item.ToString());
  73.  
  74. }
  75. }
  76. private void addNewContact()
  77. {
  78. Console.WriteLine("Enter name:");
  79. string name = Console.ReadLine();
  80. Console.WriteLine("Enter telephone number");
  81. string telephone = Console.ReadLine();
  82.  
  83. if (FindContact(name) == false)
  84. {
  85. ListOfContants.Add(new Contatcs(name, telephone));
  86. }
  87. else
  88. {
  89. Console.WriteLine("Contact already Exit.");
  90. }
  91. }
  92. bool FindContact()
  93. {
  94. Console.WriteLine("Enter Contact Name:");
  95. string name = Console.ReadLine();
  96. Console.WriteLine(FindContact(name));
  97.  
  98. return false;
  99. }
  100. bool FindContact(string name)
  101. {
  102. for (int i = 0; i < ListOfContants.Count; i++)
  103. {
  104. if (ListOfContants[i].Name == name)
  105. {
  106. Console.WriteLine("Conctact: " + name + " Found on position : " + i);
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. bool UpdateExistingContact()
  113. {
  114. Console.WriteLine("Enter name of Contact to Update:");
  115. string name = Console.ReadLine();
  116. if (FindContact(name) == true)
  117. {
  118. for (int i = 0; i < ListOfContants.Count; i++)
  119. {
  120. if (ListOfContants[i].Name == name)
  121. {
  122. Console.WriteLine("Enter new Name:");
  123. string NewName = Console.ReadLine();
  124. ListOfContants[i].Name = NewName;
  125. }
  126. }
  127. return true;
  128. }
  129.  
  130.  
  131. return false;
  132.  
  133. }
  134. bool RemoveExistingContact()
  135. {
  136. Console.WriteLine("Enter Contact do remove:");
  137. string name = Console.ReadLine();
  138. if (FindContact(name) == true)
  139. {
  140. foreach (Contatcs contact in ListOfContants)
  141. {
  142. if (contact.Name.Equals(name))
  143. {
  144. ListOfContants.Remove(contact);
  145. return true;
  146. }
  147. }
  148. }
  149.  
  150. return false;
  151. }
  152.  
  153. }
  154. class Contatcs
  155. {
  156. public Contatcs(string Name, string TelephoneNumber)
  157. {
  158. this.Name = Name;
  159. this.TelephoneNumber = TelephoneNumber;
  160. }
  161. public string Name { get; set; }
  162. public string TelephoneNumber { get; set; }
  163.  
  164. public override string ToString()
  165. {
  166. return "Name:" + Name + "Telephone number:" + TelephoneNumber;
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment