Exemplo n.º 1
0
        /// <summary>
        /// Creates and adds a new contact to the specified set of contacts.
        /// </summary>
        /// <param name="contactSet">The set of contacts to add the new contact to.</param>
        public static void AddNewContact(ref HashSet <Contact> contactSet)
        {
            Console.Clear();
            Console.WriteLine("Creating new Contact.\n");

            Contact newContact = Contact.Create();

            Console.WriteLine(newContact);
            bool contactOK = !MainFunctions.InputStartsWith("Is this contact OK? (Y/n) ", "n");

            if (contactOK)
            {
                contactSet.Add(newContact);
                Console.WriteLine("Added new contact to address book.");
            }
            else
            {
                Console.WriteLine("Discarded new contact.");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Searches for contacts and returns the results.
        /// </summary>
        /// <param name="contacts">The set of contacts to search through.</param>
        /// <returns>The search results.</returns>
        public static IEnumerable <Contact> SearchForContacts(HashSet <Contact> contacts)
        {
            string searchBy  = MainFunctions.ReadLine("Search By: (Name/address/phone number/fax number/email address) ");
            string searchFor = MainFunctions.ReadLine("Search For: ");

            HashSet <Contact> results = new HashSet <Contact>();

            char firstLetter = 'n';

            try
            {
                firstLetter = searchBy.ToLower().ToCharArray()[0];
            }
            catch (IndexOutOfRangeException) { }

            switch (firstLetter)
            {
            case 'a':
                Console.WriteLine("Searching by Address.");
                foreach (Contact contact in contacts)
                {
                    var addressQuery =
                        from address in contact.Addresses
                        where address.ToString().Contains(searchFor)
                        select contact;

                    foreach (Contact queryContact in addressQuery)
                    {
                        results.Add(queryContact);
                    }
                }
                break;

            case 'p':
                Console.WriteLine("Searching by Phone Number.");
                foreach (Contact contact in contacts)
                {
                    var phoneQuery =
                        from phoneNumber in contact.PhoneNumbers
                        where phoneNumber.ToString().Contains(searchFor)
                        select contact;

                    foreach (Contact queryContact in phoneQuery)
                    {
                        results.Add(queryContact);
                    }
                }
                break;

            case 'f':
                Console.WriteLine("Searching by Fax Number.");
                foreach (Contact contact in contacts)
                {
                    var faxQuery =
                        from faxNumber in contact.FaxNumbers
                        where faxNumber.ToString().Contains(searchFor)
                        select contact;

                    foreach (Contact queryContact in faxQuery)
                    {
                        results.Add(queryContact);
                    }
                }
                break;

            case 'e':
                Console.WriteLine("Searching by Email Address.");
                foreach (Contact contact in contacts)
                {
                    var emailQuery =
                        from emailAddress in contact.EmailAddresses
                        where emailAddress.ToString().Contains(searchFor)
                        select contact;

                    foreach (Contact queryContact in emailQuery)
                    {
                        results.Add(queryContact);
                    }
                }
                break;

            default:
                Console.WriteLine("Searching by Name.");
                var nameQuery =
                    from contact in contacts
                    where contact.Name.Contains(searchFor)
                    select contact;

                foreach (Contact queryContact in nameQuery)
                {
                    results.Add(queryContact);
                }
                break;
            }

            return(results);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Searches for and edits contacts.
        /// </summary>
        /// <param name="contacts">The set of contacts to search through and edit.</param>
        public static void EditContact(ref HashSet <Contact> contacts)
        {
            if (contacts.Count > 0)
            {
                IEnumerable <Contact> searchResults = SearchForContacts(contacts);
                Contact contactToEdit = new Contact(" ");

                if (searchResults.Count() == 1)
                {
                    Console.WriteLine("\nOnly one result found, so that will be used.");
                    contactToEdit = searchResults.First();
                }
                else if (searchResults.Count() > 1)
                {
                    Console.WriteLine("Multiple search results found. Looping over them all. Select the one you want to edit.");
                    foreach (Contact contact in searchResults)
                    {
                        Console.WriteLine(contact);
                        bool editThisContact = !MainFunctions.InputStartsWith("Edit this contact? (Y/n)", "n");

                        if (editThisContact)
                        {
                            contactToEdit = contact;
                            break;
                        }
                    }

                    if (contactToEdit == new Contact(" "))
                    {
                        Console.WriteLine("No contact was selected to be edited, so nothing will be edited");
                    }
                }
                else
                {
                    Console.WriteLine("Search results are empty, nothing to edit");
                }

                if (contactToEdit != new Contact(" "))
                {
                    Console.WriteLine("Editing this contact:\n");
                    Console.WriteLine(contactToEdit);

                    Console.Write("\n\nPress any key to edit...");
                    Console.ReadKey();

                    Console.WriteLine("\nDeleting old contact...");
                    contacts.Remove(contactToEdit);

                    Console.WriteLine("\nDeleted old contact, editing selected contact...");
                    contactToEdit.Edit();

                    Console.WriteLine("\nEdited contact. Here is the resulting contact:");
                    Console.WriteLine(contactToEdit);

                    Console.WriteLine("\nAdding new contact to address book...");
                    contacts.Add(contactToEdit);
                    Console.WriteLine("\nAdded new contact to address book.");
                }
            }
            else
            {
                Console.WriteLine("No contacts");
            }
        }