peshopbs2

Untitled

Feb 27th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 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 Phonebook
  8. {
  9. class PhoneBook
  10. {
  11. public List<Person> Contacts { get; private set; }
  12.  
  13. public PhoneBook(List<Person> contacts)
  14. {
  15. Contacts = contacts;
  16. }
  17.  
  18. //1. Add new contact
  19. public void AddContact(Person contact)
  20. {
  21. Contacts.Add(contact);
  22. }
  23.  
  24. //2. Update contact phone by name
  25. public void UpdateContact(string name, string phoneNumber)
  26. {
  27. Person contact = GetContactByName(name);
  28. contact.UpdatePhoneNumber(phoneNumber);
  29. }
  30. //3. Search contact by name
  31. public Person GetContactByName(string name)
  32. {
  33. return Contacts
  34. .Where(contact => contact.Name == name)
  35. .FirstOrDefault();
  36. }
  37. //4. Search contact by phone
  38. public Person GetContactByPhone(string phoneNumber)
  39. {
  40. return Contacts
  41. .Where(contact => contact.PhoneNumber == phoneNumber)
  42. .FirstOrDefault();
  43. }
  44.  
  45. //5. Print all contacts
  46. public void PrintAllContacts()
  47. {
  48. Console.WriteLine("----- PHONEBOOK CONTACTS -----");
  49. foreach (Person contact in Contacts)
  50. {
  51. Console.WriteLine(contact);
  52. }
  53. Console.WriteLine(new string('-', 30));
  54. }
  55. //6. Delete contact by name
  56. public void DeleteContactByName(string name)
  57. {
  58. Person contact = GetContactByName(name);
  59.  
  60. Contacts.Remove(contact);
  61. }
  62. //7. Delete contact by phone
  63. public void DeleteContactByPhone(string phoneNumber)
  64. {
  65. Person contact = GetContactByPhone(phoneNumber);
  66.  
  67. Contacts.Remove(contact);
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment