/* UC13:- Ability to Read the Address Book with Persons Contact into a File using File IO
         *       - Using C# File IO
         */
        public static AddressBookDetails ReadFromFile()
        {
            FileStream stream;
            string     path = @"D:\Practice\C#\AddressBookSystem\AddressBookSystem\IO File\PersonContacts.txt";

            try
            {                                                        // If path is not found then it throws file not found exception
                using (stream = new FileStream(path, FileMode.Open)) // Open the specified path
                {
                    BinaryFormatter formatter = new BinaryFormatter();

                    // Deserialize the data from file
                    // If stream is null then it throws Serialization exception
                    AddressBookDetails addressBookDetails = (AddressBookDetails)formatter.Deserialize(stream);

                    // Copy the details of instance variables to static
                    cityToContactMap  = addressBookDetails.cityToCOntactMapInstance;
                    stateToContactMap = addressBookDetails.stateToContactMapInstance;
                    return(addressBookDetails);
                };
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("file not found");
                return(new AddressBookDetails());
            }
            catch (SerializationException)
            {
                Console.WriteLine("No previous records");
                return(new AddressBookDetails());
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome To Address Book Program");

            AddressBookDetails addressBookDetails = new AddressBookDetails();

            L1 : Console.WriteLine("\nType to select address book\nAdd - To add or access address book\nview - To view all names of address books\nDelete - To delete Address book\nE - To exit");

            switch (Console.ReadLine().ToLower())
            {
            //To add or access new Address book
            case TO_ADD_OR_ACCESS:
                addressBookDetails.AddOrAccessAddressBook();
                break;

            //To view all address book names
            case TO_VIEW_ALL_ADDRESSBOOKS:
                addressBookDetails.ViewAllAddressBooks();
                break;

            //To delete an address book
            case TO_DELETE_ADDRESS_BOOK:
                addressBookDetails.DeleteAddressBook();
                break;

            default:
                Console.WriteLine("User exited application");
                return;
            }
            goto L1;
        }
        /*UC7:- bility to ensure there is no Duplicate Entry of the same Person in a particular Address Book
         *              - Duplicate Check is done on Person Name while adding person to Address Book.
         *              - Use Collection Methods to Search Person by Name for Duplicate Entry.
         *              - Override equals method to check for Duplicate.
         */
        public void AddContact()                    //Create AddContact method to add Record
        {
            Console.Write("Enter First Name:- ");   //Take input
            firstName = Console.ReadLine();         //Store firstname
            Console.Write("Enter Last Name:- ");    //Take input
            lastName = Console.ReadLine();
            Console.Write("Enter The Address ");    //Take input
            address = Console.ReadLine();
            Console.Write("Enter City:- ");         //Take input
            city = Console.ReadLine();
            Console.Write("Enter State:- ");        //Take input
            state = Console.ReadLine();
            Console.Write("Enter Zip Code:- ");     //Take input
            zip = Console.ReadLine();
            Console.Write("Enter Phone Number:- "); //Take input
            phoneNumber = Console.ReadLine();
            Console.Write("Enter Email:- ");        //Take input
            email = Console.ReadLine();
            // Creating an instance of contact with given details
            ContactDetails addNewContact = new ContactDetails(firstName, lastName, address, city, state, zip, phoneNumber, email, nameOfAddressBook);

            while (addNewContact.Equals(contactList))
            {
                Console.WriteLine("contact already exists");
                // Giving option to user to re enter or to exit
                Console.Write("1.Enter New Name\n0.Exit\nEnter Choice:- ");
                int opt = Convert.ToInt32(Console.ReadLine());
                if (opt == 1)
                {
                    Console.WriteLine("Enter new First name");
                    firstName = Console.ReadLine();
                    Console.WriteLine("Enter new Last name");
                    lastName      = Console.ReadLine();
                    addNewContact = new ContactDetails(firstName, lastName, address, city, state, zip, phoneNumber, email, nameOfAddressBook);
                }
                else
                {
                    return;
                }
            }

            contactList.Add(addNewContact);                                // Adding the contact to list
            AddressBookDetails.AddToCityDictionary(city, addNewContact);   // Adding contact to city dictionary
            AddressBookDetails.AddToStateDictionary(state, addNewContact); // Adding contact to state dictionary
            Console.WriteLine("\nContact Added successfully");
        }
        public void EditContact() //Create EditContact method to edite record
        {
            try
            {
                if (contactList.Count() == 0)               //List have no contacts
                {
                    Console.WriteLine(" Record Not Found"); //print
                    return;
                }
                Console.Write("Enter First Name :- ");              //Take First Name
                string         name    = Console.ReadLine();
                ContactDetails contact = SearchByName(name);        // Search the name

                if (contact == null)                                // If contact doesnt exist
                {
                    Console.WriteLine($"{name } Record Not Found"); //Print
                    return;
                }

                contact.Display();  //  print record of searched contact

                int updateAttributeNum = 0;
                Console.Write("Enter Row number Record to be Edit:- "); //print and take input
                try
                {
                    updateAttributeNum = Convert.ToInt32(Console.ReadLine());
                    if (updateAttributeNum == 0)
                    {
                        return;
                    }
                }
                catch
                {
                    Console.WriteLine("Invalid entry");
                    return;
                }
                Console.Write("Enter the New Record:- "); // Take input
                string newValue = Console.ReadLine();
                switch (updateAttributeNum)               // Updating selected attribute with selected value
                {
                case UPDATE_FIRST_NAME:
                    firstName         = contact.FirstName; // Store the firstname of contact in variable
                    contact.FirstName = newValue;          // Update the contact with given name
                    if (contact.Equals(contactList))       // If duplicate contact exists with that name then revert the operation
                    {
                        contact.FirstName = firstName;
                        Console.Write($"Contact already exists that{ contact.FirstName} Name");
                        return;
                    }
                    break;

                case UPDATE_LAST_NAME:
                    lastName         = contact.LastName; // Store the LastName of given contact in variable
                    contact.LastName = newValue;         // Update the contact with given name
                    if (contact.Equals(contactList))     // If duplicate contact exists with that name then revert the operation
                    {
                        contact.LastName = lastName;
                        Console.WriteLine($"Contact already exists with that {contact.LastName} name");
                        return;
                    }
                    break;

                case UPDATE_ADDRESS:
                    contact.Address = newValue;
                    break;

                case UPDATE_CITY:
                    AddressBookDetails.cityToContactMap[contact.City].Remove(contact); // Remove the contact from city dictionary
                    contact.City = newValue;                                           // Update the contact city
                    AddressBookDetails.AddToCityDictionary(contact.City, contact);     // Add to city dictionary
                    break;

                case UPDATE_STATE:
                    AddressBookDetails.stateToContactMap[contact.State].Remove(contact); // Remove the contact from state dictionary
                    contact.State = newValue;                                            // Update the contact state
                    AddressBookDetails.AddToStateDictionary(contact.State, contact);     // Add to state dictionary
                    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;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("Record Successfully Edit and Updated ");
        }
        static void Main(string[] args)
        { /* UC6:- Refactor to add multiple Address Book to the System.
           *     Each Address Book has a unique Name
           *     - Use Console to add new Address Book
           *     - Maintain Dictionary of Address Book Name to Address Book
           */
            Console.WriteLine("**** Welcome To Address Book System ****");
            AddressBookDetails addressBookDetails = AddressBookDetails.ReadFromFile();
            bool flag = true;

            while (flag)
            {
                Console.Write("\n1.Add Address Book System" +
                              "\n2.Show Address Book System Name" +
                              "\n3.Delete Address Book" +
                              "\n4.Search Person in City or State " +
                              "\n5.Show all Record in City or State" +
                              "\n6.Count of Record City or State" +
                              "\n7.Exit\nEnter Your Choice: - ");

                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    addressBookDetails.AddAddressBook();     // add new Address book
                    break;

                case 2:
                    addressBookDetails.ViewAllAddressBooks();     //view all address book names
                    break;

                case 3:
                    addressBookDetails.DeleteAddressBook();     //delete an address book
                    break;

                case 4:
                    try
                    {                                                      /* UC8:- Ability to search Person in a City or State across the multiple AddressBook
                                                                            *       - Search Result can show multiple person in the city or state
                                                                            */
                        Console.Write("1.City\n2.State\nEnter Choice:-");  //print
                        int choice2 = Convert.ToInt32(Console.ReadLine()); //take input and convert int32
                        if (choice2 == 1)                                  //check condition
                        {
                            addressBookDetails.SearchInCity();             //search city
                        }
                        else if (choice2 == 2)
                        {
                            addressBookDetails.SearchInState();//search State
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case 5:
                    try
                    {                                                      /* UC8:- Ability to search Person in a City or State across the multiple AddressBook
                                                                            *        - Search Result can show multiple person in the city or state
                                                                            */
                        Console.Write("1.City\n2.State\nEnter Choice:-");  //print
                        int choice3 = Convert.ToInt32(Console.ReadLine()); //take input and convert int32
                        if (choice3 == 1)
                        {
                            addressBookDetails.ViewAllByCity();  //  view all contact in a city
                        }
                        else if (choice3 == 2)
                        {
                            addressBookDetails.ViewAllByState();  // view all contact in a State
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case 6:
                    try
                    {                                                      /* UC10:- Ability to get number of contact persons i.e. count by City or State.
                                                                            *       - Search Result will show count by city and by state.
                                                                            */
                        Console.Write("1.City\n2.State\nEnter Choice:-");  //print
                        int choice4 = Convert.ToInt32(Console.ReadLine()); //take input and convert int32
                        if (choice4 == 1)
                        {
                            addressBookDetails.CountAllByCity(); //get count of contacts in City
                        }
                        else if (choice4 == 2)
                        {
                            addressBookDetails.CountAllByState(); //get count of contacts in state
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case 7:

                    flag = false;
                    break;

                default:
                    Console.WriteLine("Please Enter Valid Option");
                    break;
                }
            }

            // UC 13 Writing to the previously stored records.
            addressBookDetails.WriteToFile();
        }
示例#6
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The arguments.</param>
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome To Address Book Program");

            // UC 13 Getting the previously stored records
            AddressBookDetails addressBookDetails = AddressBookDetails.GetFromFile();
            bool flag = true;

            while (flag)
            {
                Console.WriteLine("\nType to select address book" +
                                  "\nA      - To add or access address book" +
                                  "\nview   - To view all names of address books" +
                                  "\nDelete - To delete Address book" +
                                  "\nCity   - To search contact in a city" +
                                  "\nState  - To search contact in a state" +
                                  "\nVCity  - To view all contacts in a city" +
                                  "\nVState - To view all contacts in a state" +
                                  "\nCCity  - To get count of contacts city wise" +
                                  "\nCState - To get count of contacts state wise" +
                                  "\nE      - To exit\n\n");
                switch (Console.ReadLine().ToLower())
                {
                // To add or access new Address book
                case TO_ADD_OR_ACCESS:
                    addressBookDetails.AddOrAccessAddressBook();
                    break;

                // To view all address book names
                case TO_VIEW_ALL_ADDRESSBOOKS:
                    addressBookDetails.ViewAllAddressBooks();
                    break;

                // To delete an address book
                case TO_DELETE_ADDRESS_BOOK:
                    addressBookDetails.DeleteAddressBook();
                    break;

                // To search for a person in a city
                case SEARCH_PERSON_IN_CITY:
                    addressBookDetails.SearchInCity();
                    break;

                // To search for a person in a state
                case SEARCH_PERSON_IN_STATE:
                    addressBookDetails.SearchInState();
                    break;

                // To view all contacts in a city
                case VIEW_ALL_IN_CITY:
                    addressBookDetails.ViewAllByCity();
                    break;

                // To view all contacts in a city
                case VIEW_ALL_IN_STATE:
                    addressBookDetails.ViewAllByState();
                    break;

                // To get count of contacts in a city
                case COUNT_ALL_IN_CITY:
                    addressBookDetails.CountAllByCity();
                    break;

                // To get count of contacts in a state
                case COUNT_ALL_IN_STATE:
                    addressBookDetails.CountAllByState();
                    break;

                case EXIT:
                    Console.WriteLine("User exited application");
                    flag = false;
                    break;

                default:
                    Console.WriteLine("Invalid entry");
                    break;
                }
            }

            // UC 13 Writing to the previously stored records.
            addressBookDetails.WriteToFile();
        }
        /// <summary>
        /// Adds the contact.
        /// </summary>
        public void AddContact()
        {
            // Getting FirstName
            Console.WriteLine("\nEnter The First Name of Contact");
            firstName = Console.ReadLine();

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

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

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

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

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

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

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

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

            // Checking for duplicates with the equals method
            // Loop continues till the given contact doesnt equal to any available contact
            while (addNewContact.Equals(contactList))
            {
                Console.WriteLine("contact already exists");
                logger.Error("User tried to create a duplicate of contact");

                // 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 ContactDetails(firstName, lastName, address, city, state, zip, phoneNumber, email, nameOfAddressBook);
                }
                else
                {
                    return;
                }
            }

            // Adding the contact to list
            contactList.Add(addNewContact);

            // Adding contact to city dictionary
            AddressBookDetails.AddToCityDictionary(city, addNewContact);

            // Adding contact to state dictionary
            AddressBookDetails.AddToStateDictionary(state, addNewContact);
            Console.WriteLine("\nContact added successfully");
        }
        /// <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();

            logger.Info("User tried to update contact" + name);

            // Search the name
            ContactDetails 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:

                // Store the firstname of given contact in variable
                firstName = contact.FirstName;

                // Update the contact with given name
                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
                AddressBookDetails.cityToContactMap[contact.City].Remove(contact);

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

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

            case UPDATE_STATE:

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

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

                // Add to state dictionary
                AddressBookDetails.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;
            }
            logger.Info("User updated contact");
            Console.WriteLine("\nUpdate Successful");
        }