Exemple #1
0
        private static void DeleteContact()
        {
            Console.WriteLine();
            Console.WriteLine("Please enter the first name and last name of the contact to delete.");
            Console.WriteLine("First Name");
            string contactFirstName = NonEmptyValue();

            Console.WriteLine("Last Name");
            string contactLastName = NonEmptyValue();

            // Get user contact from phone book using the combination his/her first name and last name
            Contact contact = PhoneBook.FindContactByFirstNameAndLastName(contactFirstName, contactLastName);

            // if the contact is in phone book, delete it otherwise, tell the user the contact does not exist
            if (contact == null)
            {
                Console.WriteLine($"Contact with First Name:'{contact.FirstName}' and Last Name: '{contact.LastName}' does not exist.");
            }
            else
            {
                PhoneBook.ContactList.Remove(contact);
            }

            Console.WriteLine();
            Console.WriteLine($"Contact with First Name:'{contactFirstName}' and Last Name: '{contactLastName}' deleted");
            Console.WriteLine($"1. Delete another contact phone number\n0. Back");
            int userResponse = UserOptionResponse(new int[] { 0, 1 });

            switch (userResponse)
            {
            case 1:
                DeleteContact();
                break;

            case 0:
                Menu();
                break;

            default:
                break;
            }
        }
Exemple #2
0
        private static void UpdateContact()
        {
            Console.WriteLine();
            Console.WriteLine("Please enter the first name and last name of the contact to update.");
            Console.WriteLine("First Name");
            string contactFirstName = NonEmptyValue();

            Console.WriteLine("Last Name");
            string contactLastName = NonEmptyValue();

            // Get user contact from phone book using the combination his/her first name and last name
            Contact contact = PhoneBook.FindContactByFirstNameAndLastName(contactFirstName, contactLastName);

            // if the contact is in phone book, update it otherwise, tell the user the contact does not exist
            if (contact == null)
            {
                Console.WriteLine($"Contact with First Name:'{contact.FirstName}' and Last Name: '{contact.LastName}' does not exist.");
            }
            else
            {
                Console.WriteLine("Please enter the contacts first name.");
                string firstName = NonEmptyValue();
                Console.WriteLine("Please enter the contacts last name.");
                string lastName = NonEmptyValue();

                Console.WriteLine("Please enter the contacts phone number.");
                string phoneNumber = Console.ReadLine();

                // check if the new first name and last name the user entered already belongs to another contact in phone book
                bool contactExist = PhoneBook.FindContactByFirstNameAndLastName(firstName, lastName) != null;

                // if the new first name and last name entered by the user belongs to another contact in phone book,
                // the user is forced to enter a different first name and last name
                while (contactExist)
                {
                    Console.WriteLine($"Contact with First Name:'{firstName}' and Last Name: '{lastName}' already exist. Enter a different name");
                    Console.WriteLine("First Name");
                    firstName = NonEmptyValue();
                    Console.WriteLine("Last Name");
                    lastName     = NonEmptyValue();
                    contactExist = PhoneBook.FindContactByFirstNameAndLastName(firstName, lastName) != null;
                }

                contact.FirstName = firstName;
                contact.LastName  = lastName;
                if (!string.IsNullOrEmpty(phoneNumber) && !string.IsNullOrWhiteSpace(phoneNumber))
                {
                    contact.PhoneNumber = phoneNumber;
                }

                Console.WriteLine();
                Console.WriteLine($"Contact with First Name:'{contactFirstName}' and Last Name: '{contactLastName}' updated");
                DisplayContact(contact);
            }

            Console.WriteLine();
            Console.WriteLine($"1. Update another contact phone number\n0. Back");
            int userResponse = UserOptionResponse(new int[] { 0, 1 });

            switch (userResponse)
            {
            case 1:
                UpdateContact();
                break;

            case 0:
                Menu();
                break;

            default:
                break;
            }
        }