コード例 #1
0
        //get from file
        public static AddressDetails GetFromFile()
        {
            FileStream stream;
            string     path = @"C:\Users\K.R.DHASHNIGA\source\repos\AddressBookFileIO\AddressBookFileIO\Contact.txt";

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

                    // Deserialize the data from file
                    // If stream is null then it throws Serialization exception
                    AddressDetails addressBookDetails = (AddressDetails)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 AddressDetails());
            }
            catch (SerializationException)
            {
                Console.WriteLine("No previous records");
                return(new AddressDetails());
            }
        }
コード例 #2
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");
        }
コード例 #3
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
            AddressDetails addressBookDetails = AddressDetails.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();

            string exportFilePath = @"C:\Users\K.R.DHASHNIGA\source\repos\ThirdParty\ThirdParty\Details.csv";

            if (File.Exists(exportFilePath))
            {
                using (var writer = new StreamWriter(exportFilePath))
                    using (CsvWriter csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture))
                    {
                        // Storing all the details of the contacts into a list

                        csvWriter.WriteRecords(exportFilePath);
                    }
            }

            string exportPath = @"C:\Users\K.R.DHASHNIGA\source\repos\ThirdParty\ThirdParty\Details.json";

            if (File.Exists(exportPath))
            {
                Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer();
                using (var writer = new StreamWriter(exportFilePath))
                    using (JsonWriter jsonWriter = new JsonTextWriter(writer))
                    {
                        // Storing all the details of the contacts into a list

                        ser.Serialize(writer, List);
                    }
            }
        }
コード例 #4
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");
        }