Example #1
0
        //to add contacts
        public void AddContact()
        {
            Console.WriteLine("\nEnter The First Name of Contact");
            firstName = Console.ReadLine();


            Console.WriteLine("\nEnter The Last Name of Contact");
            lastName = Console.ReadLine();


            Console.WriteLine("\nEnter The Address of Contact");
            address = Console.ReadLine();


            Console.WriteLine("\nEnter The City Name of Contact");
            city = Console.ReadLine().ToLower();


            Console.WriteLine("\nEnter The State Name of Contact");
            state = Console.ReadLine().ToLower();


            Console.WriteLine("\nEnter the Zip of Locality of Contact");
            zip = Console.ReadLine();


            Console.WriteLine("\nEnter The Phone Number of Contact");
            phoneNumber = Console.ReadLine();


            Console.WriteLine("\nEnter The Email of Contact");
            email = Console.ReadLine();

            // Creating an instance of contact with given details
            Person addNewContact = new Person(firstName, lastName, address, city, state, zip, phoneNumber, email, nameOfAddressBook);

            // Checking for duplicates with the equals method
            // Loop continues till the given contact not equal to any available contact
            while (addNewContact.Equals(contactList))
            {
                Console.WriteLine("contact already exists");


                // Giving option to user to re enter or to exit
                Console.WriteLine("Type Y to enter new name or any other key to exit");

                // If user wants to re-enter then taking input from user
                // Else return
                if ((Console.ReadLine().ToLower() == "y"))
                {
                    Console.WriteLine("Enter new first name");
                    firstName = Console.ReadLine();
                    Console.WriteLine("Enter new last name");
                    lastName      = Console.ReadLine();
                    addNewContact = new Person(firstName, lastName, address, city, state, zip, phoneNumber, email, nameOfAddressBook);
                }
                else
                {
                    return;
                }
            }


            contactList.Add(addNewContact);


            AddressDetails.AddToCityDictionary(city, addNewContact);


            AddressDetails.AddToStateDictionary(state, addNewContact);
            Console.WriteLine("\nContact added successfully");
        }
Example #2
0
        /// <summary>
        /// Updates the contact.
        /// </summary>
        public void UpdateContact()
        {
            // If the List have no contacts
            if (contactList.Count() == 0)
            {
                Console.WriteLine("No saved contacts");
                return;
            }

            // Input the name to be updated
            Console.WriteLine("\nEnter the name of candidate to be updated");
            string name = Console.ReadLine();


            // Search the name
            Person contact = SearchByName(name);

            // If contact doesnt exist
            if (contact == null)
            {
                Console.WriteLine("No record found");
                return;
            }

            // To print details of searched contact
            contact.toString();

            int updateAttributeNum = 0;

            // Getting the attribute to be updated
            Console.WriteLine("\nEnter the row number attribute to be updated or 0 to exit");
            try
            {
                updateAttributeNum = Convert.ToInt32(Console.ReadLine());
                if (updateAttributeNum == 0)
                {
                    return;
                }
            }
            catch
            {
                Console.WriteLine("Invalid entry");
                return;
            }

            // Getting the new value of attribute
            Console.WriteLine("\nEnter the new value to be entered");
            string newValue = Console.ReadLine();

            // Updating selected attribute with selected value
            switch (updateAttributeNum)
            {
            case UPDATE_FIRST_NAME:


                firstName = contact.firstName;

                contact.firstName = newValue;

                // If duplicate contact exists with that name then revert the operation
                if (contact.Equals(contactList))
                {
                    contact.firstName = firstName;
                    Console.WriteLine("Contact already exists with that name");
                    return;
                }
                break;

            case UPDATE_LAST_NAME:

                // Store the LastName of given contact in variable
                lastName = contact.lastName;

                // Update the contact with given name
                contact.lastName = newValue;

                // If duplicate contact exists with that name then revert the operation
                if (contact.Equals(contactList))
                {
                    contact.lastName = lastName;
                    Console.WriteLine("Contact already exists with that name");
                    return;
                }
                break;

            case UPDATE_ADDRESS:
                contact.address = newValue;
                break;

            case UPDATE_CITY:

                // Remove the contact from city dictionary
                AddressDetails.cityToContactMap[contact.city].Remove(contact);

                // Update the contact city
                contact.city = newValue;

                // Add to city dictionary
                AddressDetails.AddToCityDictionary(contact.city, contact);
                break;

            case UPDATE_STATE:

                // Remove the contact from state dictionary
                AddressDetails.stateToContactMap[contact.state].Remove(contact);

                // Update the contact state
                contact.state = newValue;

                // Add to state dictionary
                AddressDetails.AddToStateDictionary(contact.state, contact);
                break;

            case UPDATE_ZIP:
                contact.zip = newValue;
                break;

            case UPDATE_PHONE_NUMBER:
                contact.phoneNumber = newValue;
                break;

            case UPDATE_EMAIL:
                contact.email = newValue;
                break;

            default:
                Console.WriteLine("Invalid Entry");
                return;
            }

            Console.WriteLine("\nUpdate Successful");
        }