示例#1
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);
        }
 public Program()
 {
     book = new AddressBook();
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Address Book Problem\nChoose one of the option");

            bool loop1 = true;

            while (loop1)
            {
                Console.WriteLine("\n1.Add AddressBook \n2.View AddressBooks");
                Console.WriteLine("3.Searching Contact by City or State\n4.Add AdrdressBook to the IO File\n5.Read AdrdressBook from the IO File ");
                Console.WriteLine("6.Add AdrdressBook to the CSV File\n7.Read AdrdressBook from CSV file\n8.Add AdrdressBook to the Json File\n9.Read AdrdressBook from Json file");
                Console.WriteLine("10.Insert Addressbook in Database\n11.Retrieve Contacts by AddressBookName\n0.Exit ");
                int choice1 = 0;
                try
                {
                    choice1 = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.WriteLine("Invalid Input!! Try again");
                }
                AddressBook addressBook     = new AddressBook();
                string      addressBookName = null;
                switch (choice1)
                {
                case 1:
                    Console.WriteLine("\nAdding a new AddessBook");

                    Console.WriteLine("Enter name for New AddessBook:");

                    addressBookName = Console.ReadLine();

                    bool isKeyAvailable = false;

                    foreach (KeyValuePair <string, AddressBook> keyValue in addressBookDictionary)
                    {
                        if (keyValue.Key.Equals(addressBookName))
                        {
                            isKeyAvailable = true;
                        }
                    }
                    if (isKeyAvailable)
                    {
                        Console.WriteLine("AddessBook Name is available, try other name\n");
                        break;
                    }

                    bool loop2 = true;

                    while (loop2)
                    {
                        Console.WriteLine("\n1.Add a Contact \n2.View Contact By Name \n3.View All Contacts \n4.Edit Contact By name");
                        Console.WriteLine("5.Delete Contact By Name \n6.Exit ");
                        int choice = 0;
                        try
                        {
                            choice = Convert.ToInt32(Console.ReadLine());
                        }
                        catch
                        {
                            Console.WriteLine("Invalid Input!! Try again");
                        }

                        switch (choice)
                        {
                        case 1:
                            Console.WriteLine("\nAdding a new Contact\n");
                            addressBook.AddContact();
                            break;

                        case 2:
                            addressBook.ViewContact();
                            break;

                        case 3:
                            addressBook.ViewAllContacts();
                            break;

                        case 4:
                            addressBook.EditContact();
                            break;

                        case 5:
                            addressBook.DeleteContact();
                            break;

                        default:
                            loop2 = false;
                            break;
                        }
                        Console.WriteLine("______________________________________________________");
                    }
                    addressBookDictionary.Add(addressBookName, addressBook);
                    addressBooksList.Add(addressBook);

                    break;

                case 2:
                    Console.WriteLine("Available AddressBooks: ");

                    foreach (KeyValuePair <String, AddressBook> keyValue in addressBookDictionary)
                    {
                        Console.WriteLine("AddressBook Name: " + keyValue.Key);
                    }
                    break;

                case 3:
                    Console.WriteLine("Your Searching Contact by City or State");
                    AddressBookMain.ContactsByCityOrState();
                    break;

                case 4:
                    Console.WriteLine("Adding AddressBook into IO File");

                    AddressBookMain.AddAddressBookToFileIO();
                    break;

                case 5:
                    Console.WriteLine("Read AddressBook from IO File");

                    AddressBookMain.ReadAddressBookToFileIO();
                    break;

                case 6:
                    Console.WriteLine("Adding AddressBook into CSV File");

                    AddressBookMain.AddAddressBookToCsv();
                    break;

                case 7:
                    Console.WriteLine("Reading AddressBook from CSV File");

                    AddressBookMain.ReadAddressBookFromCsv();
                    break;

                case 8:
                    Console.WriteLine("Adding AddressBook into Json File");

                    AddressBookMain.AddAddressBookToJsonFile();
                    break;

                case 9:
                    Console.WriteLine("Reading AddressBook from Json File");

                    AddressBookMain.ReadAddressBookFromJsonFile();
                    break;

                case 10:
                    Console.WriteLine("Insert Contact to AddressBook Database");

                    AddressBookMain.insertAddressBooktoDB();
                    break;

                case 11:
                    Console.WriteLine("Retrieving Contacts by AddressBookName");

                    AddressBookMain.RetrieveFromDB();
                    break;


                default:
                    loop1 = false;
                    break;
                }

                Console.WriteLine("______________________________________________________");
            }
            Console.WriteLine("Thanks for Using the Application!!");
        }
        /// <summary>
        /// Adds the or access address book.
        /// </summary>
        public void AddOrAccessAddressBook()
        {
            AddressBook addressBook = GetAddressBook();

            // Condition to check whether the address book returned by the Get address book function returns a null
            if (addressBook == null)
            {
                Console.WriteLine("Action aborted");
                return;
            }

Outer:
            Console.WriteLine("******************************************");
            Console.WriteLine("Welcome to the {0}'s Address Book", addressBookName.ToUpper());
            Console.WriteLine("******************************************");
            Console.WriteLine("1. Create A New Contact");
            Console.WriteLine("2. Edit a contact");
            Console.WriteLine("3. Delete a contact");
            Console.WriteLine("4. Display Stored Contact");
            Console.WriteLine("5. Display Contact Name as per State");
            Console.WriteLine("6. Display Contact Name as per City");
            Console.WriteLine("7. Display Count of contact as per State");
            Console.WriteLine("8. Display Count of contact as per City");
            Console.WriteLine("9. Handle File Input Output Operation");
            Console.WriteLine("Press any Key to Exit!!!!!!!");

            switch (Convert.ToInt32(Console.ReadLine().ToLower()))
            {
            case ADD_CONTACT:
                addressBook.AddContact();
                break;

            case EDIT_CONTACT:
                addressBook.EditContactDetails();
                break;

            case GET_ALL_CONTACTS:
                addressBook.DisplayDetails();
                break;

            case DELETE_CONTACT:
                addressBook.DeleteDetails();
                break;

            case GET_ALL_CONTACTS_BY_STATE:
                addressBook.DisplayByState();
                break;

            case GET_ALL_CONTACTS_BY_CITY:
                addressBook.DisplayByCity();
                break;

            case GET_COUNT_BY_STATE:
                addressBook.DisplayCountByState();
                break;

            case GET_COUNT_BY_CITY:
                addressBook.DisplayCountByCity();
                break;

            case FILE_IO:
                FileReadWriteClass.GuidanceToFileIO(addressBook);
                break;

            default:
                Console.WriteLine("\nInvalid option. Exiting from the address book");
                return;
            }

            Console.WriteLine("\nType y to continue in same address Book or any other key to exit");
            if (!(Console.ReadLine().ToLower() == "y"))
            {
                return;
            }
            else
            {
                goto Outer;
            }
        }
        /// <summary>
        /// Traverse all address books to get the contact details as per state or city
        /// </summary>
        public void TraverseAllAddressBooksToOrderByCity()
        {
            //Dictionary to store the list of contacts as per city name
            Dictionary <string, List <string> > nameByCity = new Dictionary <string, List <string> >();
            List <string> distinctCity = new List <string>();

            if (addressBookList.Count == 0)
            {
                Console.WriteLine("No record found");
            }
            else
            {
                //Address Book traversing to get distinct name
                foreach (KeyValuePair <string, AddressBook> keyValuePair in addressBookList)
                {
                    //Creating instance of the address book usingthe key value of the address book
                    AddressBook           addressBook = new AddressBook(keyValuePair.Key);
                    List <ContactDetails> contacts    = keyValuePair.Value.contactList;
                    foreach (ContactDetails details in contacts)
                    {
                        //Adding only the distinct city
                        //In if using the distinct case element
                        //Else add to the city
                        if (distinctCity.Contains(details.city))
                        {
                            continue;
                        }
                        else
                        {
                            distinctCity.Add(details.city);
                        }
                    }
                }
                //Iterating the city name  in distinct city list and then iterating the entire address book to add list of contacts
                foreach (var cityName in distinctCity)
                {
                    List <string> contactValues = new List <string>();
                    foreach (KeyValuePair <string, AddressBook> keyValuePair in addressBookList)
                    {
                        AddressBook           addressBook = new AddressBook(keyValuePair.Key);
                        List <ContactDetails> contacts    = keyValuePair.Value.contactList;
                        //Adding the contact list in the dictionary
                        foreach (var contactInAddressBook in contacts)
                        {
                            if (contactInAddressBook.city == cityName)
                            {
                                contactValues.Add(contactInAddressBook.firstName + " \t" + contactInAddressBook.secondName);
                            }
                        }
                    }
                    nameByCity.Add(cityName, contactValues);
                }
                //Traversing the dictionary to display the entire mapped set of city and contact name
                foreach (var dictionaryElement in nameByCity)
                {
                    Console.WriteLine("================" + dictionaryElement.Key + "================");
                    List <string> name = dictionaryElement.Value;
                    foreach (string contactName in name)
                    {
                        Console.WriteLine(contactName + "\n");
                    }
                }
            }
        }
        /// <summary>
        /// Traverse all address books to get the contact details as per state or city
        /// </summary>
        public void TraverseAllAddressBooksToOrderByState()
        {
            //Dictionary to store list of contacts by the name of state
            Dictionary <string, List <string> > nameByState = new Dictionary <string, List <string> >();
            List <string> distinctState = new List <string>();

            //Check the count exception case in begining only
            if (addressBookList.Count == 0)
            {
                Console.WriteLine("No record found");
            }
            else
            {
                foreach (KeyValuePair <string, AddressBook> keyValuePair in addressBookList)
                {
                    //Creating an instance of the address book to store the key
                    AddressBook           addressBook = new AddressBook(keyValuePair.Key);
                    List <ContactDetails> contacts    = keyValuePair.Value.contactList;
                    foreach (ContactDetails details in contacts)
                    {
                        //Adding only the distinct state
                        if (distinctState.Contains(details.state))
                        {
                            continue;
                        }
                        else
                        {
                            distinctState.Add(details.state);
                        }
                    }
                }
                //Reiterating the distinct state for adding the contacts
                foreach (var stateName in distinctState)
                {
                    // String list aimed to add the contact values as per distinct state name
                    List <string> contactValues = new List <string>();
                    foreach (KeyValuePair <string, AddressBook> keyValuePair in addressBookList)
                    {
                        AddressBook           addressBook = new AddressBook(keyValuePair.Key);
                        List <ContactDetails> contacts    = keyValuePair.Value.contactList;
                        foreach (var contactInAddressBook in contacts)
                        {
                            //Adding the contacts in case the state name matches the distinct state list element
                            if (contactInAddressBook.state == stateName)
                            {
                                contactValues.Add(contactInAddressBook.firstName + " \t" + contactInAddressBook.secondName);
                            }
                        }
                    }
                    nameByState.Add(stateName, contactValues);
                }
                //Displaying the name of the contact stored in the dictionary
                foreach (var dictionaryElement in nameByState)
                {
                    Console.WriteLine("================" + dictionaryElement.Key + "================");
                    List <string> name = dictionaryElement.Value;
                    foreach (string contactName in name)
                    {
                        Console.WriteLine(contactName + "\n");
                    }
                }
            }
        }
示例#7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Address Book Problem\nChoose one of the option");
            //Constants:

            const int ADD_ADDRESSBOOK = 1;
            const int ADD_CONTACT     = 1;
            const int EDIT_CONTACT    = 2;
            const int DELETE_CONTACT  = 3;
            //variables
            bool b1 = true;

            while (b1)
            {
                Console.WriteLine("\n1.Add AddressBook\n2.Exit\n");
                int choose1 = Convert.ToInt32(Console.ReadLine());

                switch (choose1)
                {
                case ADD_ADDRESSBOOK:

                    AddressBook ad = new AddressBook();
                    bool        b  = true;

                    while (b)
                    {
                        Console.WriteLine("\n1.Add contacts\n2.Edit Contact using name\n3.Delete Contact using name\n4.Exit");

                        int choice = Convert.ToInt32(Console.ReadLine());

                        switch (choice)
                        {
                        case ADD_CONTACT:
                            Console.WriteLine("\nAdding a new Contact\n");
                            ad.AddContact();
                            break;

                        case EDIT_CONTACT:
                            Console.WriteLine("Edit Contact Using name\n");
                            Console.WriteLine("Enter First Name: ");
                            String fname = Console.ReadLine();

                            Console.WriteLine("Enter last Name: ");
                            String lname = Console.ReadLine();

                            bool isEdited = ad.EditContact(fname, lname);

                            if (isEdited)
                            {
                                Console.WriteLine("\nDetails Updated SuccessFully!!\n");
                            }
                            else
                            {
                                Console.WriteLine("\nNo contact exits with this name\nEdite failed!!\n");
                            }
                            break;

                        case DELETE_CONTACT:
                            Console.WriteLine("Delete Contact Using name\n");
                            Console.WriteLine("Enter First Name: ");
                            String fName = Console.ReadLine();

                            Console.WriteLine("Enter last Name: ");
                            String lName = Console.ReadLine();

                            bool isDeleted = ad.DeleteContact(fName, lName);

                            if (isDeleted)
                            {
                                Console.WriteLine("\nContact SuccessFully!!\n");
                            }
                            else
                            {
                                Console.WriteLine("\nNo contact exits with this name\nDelete failed!!\n");
                            }
                            break;

                        default:
                            b = false;
                            break;
                        }
                    }

                    break;

                default:
                    b1 = false;
                    break;
                }

                Console.Out.WriteLine("*******************************************\n");
            }
        }