Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Phonebook
- {
- class PhoneBook
- {
- public List<Person> Contacts { get; private set; }
- public PhoneBook(List<Person> contacts)
- {
- Contacts = contacts;
- }
- //1. Add new contact
- public void AddContact(Person contact)
- {
- Contacts.Add(contact);
- }
- //2. Update contact phone by name
- public void UpdateContact(string name, string phoneNumber)
- {
- Person contact = GetContactByName(name);
- contact.UpdatePhoneNumber(phoneNumber);
- }
- //3. Search contact by name
- public Person GetContactByName(string name)
- {
- return Contacts
- .Where(contact => contact.Name == name)
- .FirstOrDefault();
- }
- //4. Search contact by phone
- public Person GetContactByPhone(string phoneNumber)
- {
- return Contacts
- .Where(contact => contact.PhoneNumber == phoneNumber)
- .FirstOrDefault();
- }
- //5. Print all contacts
- public void PrintAllContacts()
- {
- Console.WriteLine("----- PHONEBOOK CONTACTS -----");
- foreach (Person contact in Contacts)
- {
- Console.WriteLine(contact);
- }
- Console.WriteLine(new string('-', 30));
- }
- //6. Delete contact by name
- public void DeleteContactByName(string name)
- {
- Person contact = GetContactByName(name);
- Contacts.Remove(contact);
- }
- //7. Delete contact by phone
- public void DeleteContactByPhone(string phoneNumber)
- {
- Person contact = GetContactByPhone(phoneNumber);
- Contacts.Remove(contact);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment