Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. namespace ClassesII_Demo.Models
  7. {
  8. public static class Phonebook
  9. {
  10. private static readonly List<Contact> contacts = new List<Contact>();
  11.  
  12. public static void AddContact(Contact contact)
  13. {
  14. foreach (var phoneContact in contacts)
  15. {
  16. if (phoneContact.Equals(contact))
  17. {
  18. throw new ArgumentException("Contact already exists!");
  19. }
  20. }
  21.  
  22. contacts.Add(contact);
  23. }
  24. public static string ListContactsForSave(string orderBy)
  25. {
  26. // remove unused variable
  27.  
  28. var builder = new StringBuilder();
  29. builder.AppendLine("[");
  30. for (int i=0;i<contacts.Count;i++)
  31. {
  32. var contact = contacts[i];
  33. builder.AppendLine(contact.GetContactInfoForSaving());
  34. if (i != contacts.Count - 1)
  35. {
  36. builder.Append(",");
  37. }
  38. }
  39. builder.AppendLine("]");
  40. return builder.ToString();
  41.  
  42.  
  43. }
  44. public static string ListContacts(string orderBy)
  45. {
  46.  
  47. if (orderBy.Equals("0"))
  48. {
  49. var builder = new StringBuilder();
  50. foreach (var contact in contacts)
  51. {
  52. builder.AppendLine(contact.GetContactInfo());
  53. }
  54.  
  55. return builder.ToString();
  56. }
  57. else
  58. if (orderBy.Equals("name"))
  59. {
  60. List<Contact> sortedByName = contacts.OrderBy(x => x.Name).ToList();
  61. var builder = new StringBuilder();
  62.  
  63. foreach (var contact in sortedByName)
  64. {
  65. builder.AppendLine(contact.GetContactInfo());
  66. }
  67.  
  68. return builder.ToString();
  69. }
  70.  
  71. else if (orderBy.Equals("time"))
  72. {
  73. List<Contact> sortedByTime = contacts.OrderBy(x => x.CreatedOn).ToList();
  74. var builder = new StringBuilder();
  75.  
  76. foreach (var contact in sortedByTime)
  77. {
  78. builder.AppendLine(contact.GetContactInfo());
  79. }
  80.  
  81. return builder.ToString();
  82. }
  83.  
  84. return "Phonebook is empty.Please enter a contact first!";
  85. }
  86.  
  87. internal static string RemoveContact(string v)
  88. {
  89. if (v == null)
  90. {
  91. throw new ArgumentException("Name is null");
  92. }
  93. // var contact
  94. foreach (var PhoneContacts in contacts)
  95. {
  96. var currentContact = (Contact)PhoneContacts;
  97. if (currentContact.Name == v)
  98. {
  99. contacts.Remove(currentContact);
  100. return "Contact removed from phonebook";
  101. }
  102. }
  103.  
  104. return "Contact to remove not found";
  105. }
  106. // todo string name
  107. internal static string UpdateContact(string v, string newNumber)
  108. {
  109. if (v == null)
  110. {
  111. throw new NotImplementedException("Contact to update is null");
  112. }
  113. // todo var contact
  114. foreach (var PhoneContact in contacts)
  115. {
  116. if (PhoneContact.Name == v)
  117. {
  118. PhoneContact.PhoneNumber = newNumber;
  119. PhoneContact.CreatedOn = DateTime.Now;
  120. return $"Phonenumber of {v} has been update to {newNumber}!";
  121. }
  122. }
  123.  
  124. return $"The contact {v} has not been found!";
  125. }
  126. //todo string name
  127. internal static string SearchContacts(string v)
  128. {
  129. if (v == null)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. StringBuilder totalInfo = new StringBuilder();
  134. //todo var contact
  135. foreach (var PhoneContact in contacts)
  136. {
  137. //for (int i = 0; i < v.Length; i++)
  138. //{
  139.  
  140. //}
  141. v = v.ToLower();
  142. string currentname = PhoneContact.Name.ToLower();
  143. if (currentname.Contains(v))
  144. {
  145. totalInfo.AppendLine(PhoneContact.GetContactInfo());
  146. }
  147. }
  148. if (totalInfo.Length == 0)
  149. {
  150. return $"Name containing {v} was not found";
  151. }
  152. else
  153. {
  154. return totalInfo.ToString();
  155. }
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement