コード例 #1
0
        /// <summary>
        /// Searches for and deletes contacts.
        /// </summary>
        /// <param name="contacts">The set of contacts to search through and delete.</param>
        public static void DeleteContact(ref HashSet <Contact> contacts)
        {
            if (contacts.Count > 0)
            {
                IEnumerable <Contact> searchResults = SearchForContacts(contacts);
                Contact contactToDelete             = new Contact(" ");

                if (searchResults.Count() == 1)
                {
                    contactToDelete = searchResults.First();
                }
                else if (searchResults.Count() > 1)
                {
                    Console.WriteLine("Multiple search results found. Looping over them all. Select the one you want to delete.");
                    foreach (Contact contact in searchResults)
                    {
                        Console.WriteLine(contact);
                        bool deleteThisContact = !MainFunctions.InputStartsWith("Delete this contact? (Y/n)", "n");

                        if (deleteThisContact)
                        {
                            contactToDelete = contact;
                            break;
                        }
                    }

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

                if (contactToDelete != new Contact(" "))
                {
                    Console.WriteLine("Deleting Contact...");
                    contacts.Remove(contactToDelete);
                    Console.WriteLine("Contact Deleted.");
                }
            }
            else
            {
                Console.WriteLine("No contacts");
            }
        }
コード例 #2
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.");
            }
        }
コード例 #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");
            }
        }