Ejemplo n.º 1
0
        /// <summary>
        /// XML Serialize instance of an AddressBooks Class
        /// </summary>
        /// <param name="addressBooks">instance to serialize</param>
        public static void SerializeAddressBooks(AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping(DictToListMapping.DictionaryToList(addressBooks));

            XmlSerializer xmlser     = new XmlSerializer(typeof(DictToListMapping));
            FileStream    fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);

            xmlser.Serialize(fileStream, dictToList);
            fileStream.Close();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// XML Deserialize and store in instance of AddressBooks Class
        /// </summary>
        /// <param name="addressBooks">ref to addressbooks instance</param>
        public static void DeserializeAddressBooks(ref AddressBooks addressBooks)
        {
            DictToListMapping dictToListMapping = new DictToListMapping();

            XmlSerializer xmlser     = new XmlSerializer(typeof(DictToListMapping));
            FileStream    fileStream = new FileStream(path, FileMode.Open);

            dictToListMapping = (DictToListMapping)xmlser.Deserialize(fileStream);
            fileStream.Close();

            addressBooks = DictToListMapping.ListToDictionary(dictToListMapping);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Method to map addressbooks dictionary to this class lists
        /// </summary>
        /// <param name="addressBooks">instance of AddressBooks Class to map</param>
        /// <returns>Instance of this class after mapping</returns>
        public static DictToListMapping DictionaryToList(AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping();

            foreach (var element in addressBooks._multiAddressBooks)
            {
                string addressBookName = element.Key;
                foreach (var contact in element.Value.AddressBook)
                {
                    dictToList.AddressBookName.Add(addressBookName);
                    dictToList.ContactName.Add(contact.Key);
                    dictToList.FirstName.Add(contact.Value.FirstName);
                    dictToList.LastName.Add(contact.Value.LastName);
                    dictToList.City.Add(contact.Value.City);
                    dictToList.State.Add(contact.Value.State);
                    dictToList.ZipCode.Add(contact.Value.Zip);
                    dictToList.PhoneNumber.Add(contact.Value.PhoneNumber);
                    dictToList.Email.Add(contact.Value.Email);
                    dictToList.DateAdded.Add(contact.Value.DateAdded);
                }
            }
            return(dictToList);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Method to map this class lists to a addressbooks dictionary
        /// </summary>
        /// <param name="dictToList">Instance of this class to map</param>
        /// <returns>Instance of AddressBooks Class after mapping</returns>
        public static AddressBooks ListToDictionary(DictToListMapping dictToList)
        {
            AddressBooks    addressBooks = new AddressBooks();
            AddressBookMain addressBookMain;
            ContactDetails  contactDetails;

            string addressBookName = "", contactName = "";

            for (int i = 0; i < dictToList.AddressBookName.Count; i++)
            {
                addressBookMain = new AddressBookMain();
                contactDetails  = new ContactDetails();

                addressBookName            = dictToList.AddressBookName[i];
                contactName                = dictToList.ContactName[i];
                contactDetails.FirstName   = dictToList.FirstName[i];
                contactDetails.LastName    = dictToList.LastName[i];
                contactDetails.City        = dictToList.City[i];
                contactDetails.State       = dictToList.State[i];
                contactDetails.Zip         = dictToList.ZipCode[i];
                contactDetails.PhoneNumber = dictToList.PhoneNumber[i];
                contactDetails.Email       = dictToList.Email[i];
                contactDetails.DateAdded   = dictToList.DateAdded[i];

                if (addressBooks._multiAddressBooks.ContainsKey(addressBookName))
                {
                    addressBooks._multiAddressBooks[addressBookName].AddressBook.Add(contactName, contactDetails);
                }
                else
                {
                    addressBookMain.AddressBook.Add(contactName, contactDetails);
                    addressBooks._multiAddressBooks.Add(addressBookName, addressBookMain);
                }
            }
            return(addressBooks);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            int    choice      = 0;
            bool   flag        = true;
            string addBookName = "";

            MultipleAddressBooks multipleAddressBooks = new MultipleAddressBooks();
            AddressBooks         addressBook          = null;

            Console.WriteLine("Welcome to the Address Book Program!");
            while (true)
            {
                Console.WriteLine("1.Add Address Book\n2.Open Address Book");
                choice = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter name of Address Book");
                addBookName = Console.ReadLine();
                switch (choice)
                {
                case 1:
                    multipleAddressBooks.AddAddressBook(addBookName);
                    addressBook = multipleAddressBooks.GetAddressBook(addBookName);
                    flag        = true;
                    break;

                case 2:
                    addressBook = multipleAddressBooks.GetAddressBook(addBookName);
                    flag        = true;
                    if (addressBook == null)
                    {
                        Console.WriteLine("No such Address Book");
                        flag = false;
                        continue;
                    }
                    break;

                default:
                    flag = false;
                    Console.WriteLine("Invalid Choice");
                    break;
                }
                bool exist = true;
                while (exist)
                {
                    Console.WriteLine("Select the option. \n1. Add new contact. \n2. Edit existing contact. \n3. Delete existing contact \n4. Search a contact" +
                                      " by city \n5. Search a contact by state \n6. Count By City \n7. Count By State \n8. Show contacts in alphabetical order \n9. Sort by City" +
                                      " \n10. Sort by state \n11. Sort by zipcode \n12. Exit");
                    int option = int.Parse(Console.ReadLine());
                    switch (option)
                    {
                    case 1:
                    {
                        addressBook.AddPerson();
                        if (addressBook.CheckDuplicate())
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Contact added!");
                            addressBook.WriteWithStreamWriter(addBookName);
                            Console.WriteLine("Contact details have been written to file successfully");
                            addressBook.ReadFromStreamReader();
                            AddressBooks.WriteCSVFile(addressBook.contactList);
                            AddressBooks.WriteToJsonFile(addressBook.contactList);
                            AddressBooks.ReadJsonFile();
                            break;
                        }
                    }

                    case 2:
                    {
                        Console.WriteLine("Enter the first name of that person: ");
                        addressBook.EditPersonDetails();
                        addressBook.WriteWithStreamWriter(addBookName);
                        Console.WriteLine("Contact details have been written to file successfully");
                        addressBook.ReadFromStreamReader();
                        AddressBooks.WriteCSVFile(addressBook.contactList);
                        AddressBooks.WriteToJsonFile(addressBook.contactList);
                        AddressBooks.ReadJsonFile();
                        break;
                    }

                    case 3:
                    {
                        Console.WriteLine("Enter the first name of that person: ");
                        addressBook.DeletePersonDetails();
                        break;
                    }

                    case 4:
                    {
                        Console.WriteLine("Enter the city: ");
                        string city = Console.ReadLine();
                        multipleAddressBooks.SearchPersonOverMultipleAddressBook(city);
                        break;
                    }

                    case 5:
                    {
                        Console.WriteLine("Enter the state: ");
                        string state = Console.ReadLine();
                        multipleAddressBooks.SearchPersonOverMultipleAddressBook(state);
                        break;
                    }

                    case 6:
                        multipleAddressBooks.SetContactByCityDictionary();
                        foreach (var contactByCity in multipleAddressBooks.ContactByCity)
                        {
                            Console.WriteLine("City :" + contactByCity.Key + "   Count :" + contactByCity.Value.Count);
                        }
                        break;

                    case 7:
                        multipleAddressBooks.SetContactByStateDictionary();
                        foreach (var contactByState in multipleAddressBooks.ContactByState)
                        {
                            Console.WriteLine("State :" + contactByState.Key + "   Count :" + contactByState.Value.Count);
                        }
                        break;

                    case 8:
                    {
                        addressBook.SortByNameInAlphabeticalOrder();
                        break;
                    }

                    case 9:
                    {
                        addressBook.SortByCity();
                        break;
                    }

                    case 10:
                    {
                        addressBook.SortByState();
                        break;
                    }

                    case 11:
                    {
                        addressBook.SortByZip();
                        break;
                    }

                    case 12:
                    {
                        exist = false;
                        break;
                    }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void AddAddressBook(string name)
        {
            AddressBooks addressBook = new AddressBooks();

            addressBooksCollection.Add(name, addressBook);
        }