public static void FillAddressBook(AddressBook addressBook)
        {
            int choice;

            do
            {
                Console.WriteLine("\nMenu : \n1.Add Contact \n2.Edit Contact \n3.Delete Contact\n0.Exit");
                choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    Contact contact = new Contact();
                    SetContactDetails(contact);
                    addressBook.AddContact(contact);
                    break;

                case 2:
                    Console.WriteLine("Enter the Phone Number of Contact you wish to Edit");
                    long phoneNumber = long.Parse(Console.ReadLine());
                    int  index       = addressBook.FindByPhoneNum(phoneNumber);
                    if (index == -1)
                    {
                        Console.WriteLine("No Contact Exists With Following Phone Number");
                        continue;
                    }
                    else
                    {
                        Contact contact2 = new Contact();
                        SetContactDetails(contact2);
                        addressBook.ContactList[index] = contact2;
                        Console.WriteLine("Contact Updated Successfully");
                    }
                    break;

                case 3:
                    Console.WriteLine("Enter the First Name of Contact you wish to delete");
                    string fname = Console.ReadLine();
                    int    idx   = addressBook.FindByFirstName(fname);
                    if (idx == -1)
                    {
                        Console.WriteLine("No Contact Exists with Following First Name");
                        continue;
                    }
                    else
                    {
                        addressBook.DeleteContact(idx);
                        Console.WriteLine("Contact Deleted Successfully");
                    }
                    break;
                }
            } while (choice != 0);
        }
        /// <summary>
        /// DeleteContactsOfAddressBook is called when user want to delete a particular contact from a AddressBook
        /// </summary>
        private static void DeleteContactsOfAddressBook()
        {
            Console.WriteLine("\nEnter Name of address book to delete contact details");
            string name = Console.ReadLine();

            if (!addressBookMapper.ContainsKey(name))
            {
                Console.WriteLine("No address book found with this name");
                Console.WriteLine("Please Enter Valid Name from following names:");
                foreach (KeyValuePair <string, AddressBook> tempPair in addressBookMapper)
                {
                    Console.WriteLine(tempPair.Key);
                }
            }
            else
            {
                AddressBook addressBook = addressBookMapper[name];
                addressBook.DeleteContact();
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Address Book Program");
            AddressBook addressBook = new AddressBook();
            int         choice, choice2;
            string      bookName = "default";

            Console.WriteLine("Would You Like To \n1.Work on default AddressBook \n2.Create New AddressBook");
            choice2 = Convert.ToInt32(Console.ReadLine());
            switch (choice2)
            {
            case 1:
                addressBook.AddAddressBook(bookName);
                break;

            case 2:
                Console.WriteLine("Enter Name Of New Addressbook You want to create : ");
                bookName = Console.ReadLine();
                addressBook.AddAddressBook(bookName);
                break;

            default:
                Console.WriteLine("Invalid Input, Proceeding with default AddressBook");
                addressBook.AddAddressBook(bookName);
                break;
            }
            do
            {
                Console.WriteLine($"Working On {bookName} AddressBook\n");
                Console.WriteLine("Choose An Option \n1.Add New Contact \n2.Edit Existing Contact \n3.Delete A Contact \n4.View A Contact \n5.View All Contacts \n6.Add New AddressBook \n7.Switch AddressBook \n8.Search Contact by City/State \n9.Count by City/State \n10.Sort Entries \n11.Read/Write AddressBook to text file \n12.Read/Write AddressBook to csv file \n13.Read/Write AddressBook to JSON file \n0.Exit Application\n");
                choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    Console.WriteLine("Enter First Name :");
                    string firstName = Console.ReadLine();
                    Console.WriteLine("Enter Last Name :");
                    string  lastName = Console.ReadLine();
                    Contact temp     = new Contact()
                    {
                        FirstName = firstName,
                        LastName  = lastName
                    };
                    if (addressBook.CheckDuplicateEntry(temp, bookName))
                    {
                        break;
                    }
                    Console.WriteLine("Enter Address :");
                    string address = Console.ReadLine();
                    Console.WriteLine("Enter City :");
                    string city = Console.ReadLine();
                    Console.WriteLine("Enter State :");
                    string state = Console.ReadLine();
                    Console.WriteLine("Enter Email :");
                    string email = Console.ReadLine();
                    Console.WriteLine("Enter Zip :");
                    int zip = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter Phone Number :");
                    long phoneNumber = long.Parse(Console.ReadLine());
                    addressBook.AddContact(firstName, lastName, address, city, state, email, zip, phoneNumber, bookName);
                    break;

                case 2:
                    Console.WriteLine("Enter Full Name Of Contact To Edit :");
                    string nameToEdit = Console.ReadLine();
                    addressBook.EditContact(nameToEdit, bookName);
                    break;

                case 3:
                    Console.WriteLine("Enter Full Name Of Contact To Delete :");
                    string nameToDelete = Console.ReadLine();
                    addressBook.DeleteContact(nameToDelete, bookName);
                    break;

                case 4:
                    Console.WriteLine("Enter Full Name Of Contact To View :");
                    string nameToView = Console.ReadLine();
                    addressBook.ViewContact(nameToView, bookName);
                    break;

                case 5:
                    addressBook.ViewContact(bookName);
                    break;

                case 6:
                    Console.WriteLine("Enter Name For New AddressBook");
                    string newAddressBook = Console.ReadLine();
                    addressBook.AddAddressBook(newAddressBook);
                    Console.WriteLine("Would you like to Switch to " + newAddressBook);
                    Console.WriteLine("1.Yes \n2.No");
                    int c = Convert.ToInt32(Console.ReadLine());
                    if (c == 1)
                    {
                        bookName = newAddressBook;
                    }
                    break;

                case 7:
                    Console.WriteLine("Enter Name Of AddressBook From Below List");
                    foreach (KeyValuePair <string, AddressBook> item in addressBook.GetAddressBook())
                    {
                        Console.WriteLine(item.Key);
                    }
                    while (true)
                    {
                        bookName = Console.ReadLine();
                        if (addressBook.GetAddressBook().ContainsKey(bookName))
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine("No such AddressBook found. Try Again.");
                        }
                    }
                    break;

                case 8:
                    Console.WriteLine("Would You Like To \n1.Search by city \n2.Search by state");
                    int opt = Convert.ToInt32(Console.ReadLine());
                    switch (opt)
                    {
                    case 1:
                        Console.WriteLine("Enter name of city :");
                        addressBook.SearchPersonByCity(Console.ReadLine());
                        break;

                    case 2:
                        Console.WriteLine("Enter name of state :");
                        addressBook.SearchPersonByState(Console.ReadLine());
                        break;

                    default:
                        Console.WriteLine("Invalid Input.Enter 1 or 2");
                        break;
                    }
                    break;

                case 9:
                    addressBook.DisplayCountByCityandState();
                    break;

                case 10:
                    Console.WriteLine("\n1.Sort By Name \n2.Sort By City \n3.Sort By State \n4.Sort By Zip");
                    int ch = Convert.ToInt32(Console.ReadLine());
                    switch (ch)
                    {
                    case 1:
                        addressBook.SortByName();
                        break;

                    case 2:
                        addressBook.SortByCity();
                        break;

                    case 3:
                        addressBook.SortByState();
                        break;

                    case 4:
                        addressBook.SortByZip();
                        break;

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

                case 11:
                    FileIOOperation fileIO = new FileIOOperation();
                    fileIO.WriteToFile(addressBook.addressBookDictionary);
                    fileIO.ReadFromFile();
                    break;

                case 12:
                    CSVHandler handler = new CSVHandler();
                    handler.WriteToFile(addressBook.addressBookDictionary);
                    handler.ReadFromFile();
                    break;

                case 13:
                    JSONOperation json = new JSONOperation();
                    json.WriteToFile(addressBook.addressBookDictionary);
                    json.ReadFromFile();
                    break;

                case 0:
                    Console.WriteLine("Thank You For Using Address Book System.");
                    break;

                default:
                    Console.WriteLine("Invalid Entry. Enter value between 0 to 8");
                    break;
                }
            } while (choice != 0);
        }
示例#4
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The arguments.</param>
        static void Main(string[] args)
        {
            ///Variables
            int    choice, choice1;
            string bookName = "default";

            Console.WriteLine("Welcome to Address Book Program");
            ///Creates reference of AdressBook.
            AddressBook addressBook = new AddressBook();

            ///Menu for AddressBook.
            Console.WriteLine("Would You Like To \n1.Work on default AddressBook \n2.Create New AddressBook");
            choice1 = Convert.ToInt32(Console.ReadLine());
            switch (choice1)
            {
            case 1:
                addressBook.AddAddressBook(bookName);
                break;

            case 2:
                Console.WriteLine("Enter Name Of New Addressbook You want to create : ");
                bookName = Console.ReadLine();
                addressBook.AddAddressBook(bookName);
                break;
            }
            do
            {
                ///Menu for Contacts.
                Console.WriteLine($"Working On {bookName} AddressBook\n");
                Console.WriteLine("Choose An Option \n1.Add New Contact \n2.Edit Existing Contact \n3.Delete A Contact \n4.View A Contact \n5.View All Contacts \n6.Add New AddressBook \n7.Switch AddressBook \n8.Search Contact by city or state\n" +
                                  "9.View Contacts by City or State.\n10. Count by city or state.\n11.Sort Entries.  \n12.Exit Application\n");
                choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    Console.WriteLine("Enter First Name :");
                    string firstName = Console.ReadLine();
                    Console.WriteLine("Enter Last Name :");
                    string lastName = Console.ReadLine();
                    Console.WriteLine("Enter Address :");
                    string address = Console.ReadLine();
                    Console.WriteLine("Enter City :");
                    string city = Console.ReadLine();
                    Console.WriteLine("Enter State :");
                    string state = Console.ReadLine();
                    Console.WriteLine("Enter Email :");
                    string email = Console.ReadLine();
                    Console.WriteLine("Enter Zip :");
                    int zip = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter Phone Number :");
                    long phoneNumber = long.Parse(Console.ReadLine());
                    addressBook.AddContact(firstName, lastName, address, city, state, email, zip, phoneNumber, bookName);
                    break;

                case 2:
                    Console.WriteLine("Enter First Name Of Contact To Edit :");
                    string nameToEdit = Console.ReadLine();
                    addressBook.EditContact(nameToEdit, bookName);
                    break;

                case 3:
                    Console.WriteLine("Enter First Name Of Contact To Delete :");
                    string nameToDelete = Console.ReadLine();
                    addressBook.DeleteContact(nameToDelete, bookName);
                    break;

                case 4:
                    Console.WriteLine("Enter First Name Of Contact To View :");
                    string nameToView = Console.ReadLine();
                    addressBook.ViewContact(nameToView, bookName);
                    break;

                case 5:
                    addressBook.ViewAllContacts(bookName);
                    break;

                case 6:
                    Console.WriteLine("Enter Name For New AddressBook");
                    string newAddressBook = Console.ReadLine();
                    addressBook.AddAddressBook(newAddressBook);
                    Console.WriteLine("Would you like to Switch to " + newAddressBook);
                    Console.WriteLine("1.Yes \n2.No");
                    int c = Convert.ToInt32(Console.ReadLine());
                    if (c == 1)
                    {
                        bookName = newAddressBook;
                    }
                    break;

                case 7:
                    Console.WriteLine("Enter Name Of AddressBook From Below List");
                    foreach (KeyValuePair <string, AddressBook> item in addressBook.GetAddressBook())
                    {
                        Console.WriteLine(item.Key);
                    }
                    while (true)
                    {
                        bookName = Console.ReadLine();
                        if (addressBook.GetAddressBook().ContainsKey(bookName))
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine("No such AddressBook found. Try Again.");
                        }
                    }
                    break;

                case 8:
                    Console.WriteLine("Would You Like To \n1.Search by city \n2.Search by state");
                    int opt = Convert.ToInt32(Console.ReadLine());
                    switch (opt)
                    {
                    case 1:
                        Console.WriteLine("Enter name of city :");
                        addressBook.SearchPersonByCity(Console.ReadLine());
                        break;

                    case 2:
                        Console.WriteLine("Enter name of state :");
                        addressBook.SearchPersonByState(Console.ReadLine());
                        break;

                    default:
                        Console.WriteLine("Invalid Input.Enter 1 or 2");
                        break;
                    }
                    break;

                case 9:
                    Console.WriteLine("Would You Like To \n1.View by city \n2.View by state");
                    int option = Convert.ToInt32(Console.ReadLine());
                    switch (option)
                    {
                    case 1:
                        Console.WriteLine("Enter name of city :");
                        addressBook.ViewByCity(Console.ReadLine());
                        break;

                    case 2:
                        Console.WriteLine("Enter name of state :");
                        addressBook.ViewByState(Console.ReadLine());
                        break;

                    default:
                        Console.WriteLine("Invalid Input.Enter 1 or 2");
                        break;
                    }
                    break;

                case 10:
                    addressBook.CountPersonByCityOrState();
                    break;

                case 11:
                    Console.WriteLine("\n1.Sort By Name \n2.Sort By City \n3.Sort By State \n4.Sort By Zip");
                    int ch = Convert.ToInt32(Console.ReadLine());
                    switch (ch)
                    {
                    case 1:
                        addressBook.SortByName();
                        break;

                    case 2:
                        addressBook.SortByCity();
                        break;

                    case 3:
                        addressBook.SortByState();
                        break;

                    case 4:
                        addressBook.SortByZip();
                        break;

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

                case 12:
                    Console.WriteLine("Thank You For Using Address Book System.");
                    break;
                }
            }while (choice != 12);
            Console.WriteLine("Writing in AddressBook.txt");
            AddressBookFileIO.WriteUsingStreamWriter(addressBook.addressBookDictionary);
            AddressBookFileIO.ReadUsingStreamReader();
        }
示例#5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Address Book Program");

            Dictionary <string, AddressBook> addressBookMap = new Dictionary <string, AddressBook>();

            int         addressBookChoice = 1;
            AddressBook addressBook       = null;

            while (addressBookChoice != 4)
            {
                Console.WriteLine("\n1. Create new Address Book");
                Console.WriteLine("2. Update existing Address Book");
                Console.WriteLine("3. Search Person in a City or State");
                Console.WriteLine("4. Exit");
                Console.WriteLine("Enter your choice");
                try
                {
                    addressBookChoice = Convert.ToInt32(Console.ReadLine());
                }
                catch (FormatException e)
                {
                    Console.WriteLine(e.Message);
                    break;
                }
                switch (addressBookChoice)
                {
                case 1:
                    Console.WriteLine("\nEnter the name of address book");
                    string bookName = Console.ReadLine();
                    addressBookMap.Add(bookName, new AddressBook(bookName));
                    break;

                case 2:
                    if (addressBookMap.Count != 0)
                    {
                        bool addressBookExist = false;
                        while (!addressBookExist)
                        {
                            try
                            {
                                Console.WriteLine("\nEnter the Address Book Name");
                                string name = Console.ReadLine();
                                addressBook      = addressBookMap[name];
                                addressBookExist = true;
                            }
                            catch (KeyNotFoundException e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }

                        int choice = 1;

                        while (choice != 9)
                        {
                            Console.WriteLine("\n1. Add a Contact");
                            Console.WriteLine("2. View Address Book");
                            Console.WriteLine("3. Edit Contact");
                            Console.WriteLine("4. Delete Contact");
                            Console.WriteLine("5. View person by city/state");
                            Console.WriteLine("6. Read Contact from CSV");
                            Console.WriteLine("7. Write Contact to CSV");
                            Console.WriteLine("8. Retrieve Contact from database");
                            Console.WriteLine("9. Back to main menu\n");
                            Console.WriteLine("Enter your choice");
                            try
                            {
                                choice = Convert.ToInt32(Console.ReadLine());
                            }
                            catch (FormatException e)
                            {
                                Console.WriteLine(e.Message);
                                break;
                            }

                            switch (choice)
                            {
                            case 1:
                                addressBook.AddContact();
                                break;

                            case 2:
                                addressBook.viewContacts();
                                break;

                            case 3:
                                addressBook.editContact();
                                break;

                            case 4:
                                addressBook.DeleteContact();
                                break;

                            case 5:
                                Console.WriteLine("1. City\n2. State ");
                                Console.Write("Select : ");
                                int option = Convert.ToInt32(Console.ReadLine());
                                if (option == 1)
                                {
                                    addressBook.groupByCityOrState("city");
                                }
                                else if (option == 2)
                                {
                                    addressBook.groupByCityOrState("state");
                                }
                                else
                                {
                                    Console.WriteLine("Invalid input.");
                                    break;
                                }

                                break;

                            case 6:
                                addressBook.ReadJSON();
                                break;

                            case 7:
                                addressBook.WriteJSON();
                                break;

                            case 8:
                                Console.WriteLine("\n1. Get All Contacts in DB");
                                Console.WriteLine("2. Get By City/State");
                                int x = Convert.ToInt32(Console.ReadLine());
                                addressBook.RetrieveDataFromDB(x);
                                break;

                            case 9:
                                Console.WriteLine("Back to main menu\n");
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("\nNo Address Book Available");
                    }
                    break;

                case 3:
                    Console.WriteLine("\nEnter the place needed to be searched : ");
                    string place = Console.ReadLine();
                    Console.WriteLine("\nPerson found at {0} are : ", place);
                    foreach (KeyValuePair <string, AddressBook> entry in addressBookMap)
                    {
                        addressBook = entry.Value;
                        List <ContactPerson> persons = addressBook.searchPersonByPlace(place);
                        foreach (ContactPerson person in persons)
                        {
                            Console.WriteLine(person.toString());
                        }
                    }
                    break;

                case 4:
                    Console.WriteLine("\nThank you for using the application");
                    break;

                default:
                    break;
                }
            }
        }