Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to address book program");

            ContactsCollection contactsCollection = new ContactsCollection();
            Contacts           contacts           = new Contacts(contactsCollection);
            CSVHandler         c = new CSVHandler(contactsCollection);
            ReadCsvWriteJSON   readCsvWriteJSON = new ReadCsvWriteJSON(contactsCollection);

            while (true)
            {
                Console.WriteLine("Enter Preference:");
                Console.WriteLine("1.Create Address Book \n 2.Search Person in same city or state \n 3.view Person in city or state \n4.View Person in alphabetic order\n 5.View Person by city state or Zip\n 6.Read data in File\n 7.Write in Csv File \n 8.Read CSV file\n 9.Writing JSON File \n 10.Exit");
                int preference = Convert.ToInt32(Console.ReadLine());
                switch (preference)
                {
                case 1:
                    Console.WriteLine("Enter Address Book Name: ");
                    string name = Console.ReadLine();
                    contactsCollection.CreateAddressBook(name);
                    List <Person> list = contactsCollection.GetAddressBook(name);
                    while (true)
                    {
                        Console.WriteLine("Enter Choice:");
                        Console.WriteLine("1.Add Contact \n 2.Edit Contact \n 3.Delete Contact \n 4.Exit");
                        int choice = Convert.ToInt32(Console.ReadLine());

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

                        case 2:
                            contacts.Edit();
                            break;

                        case 3:
                            contacts.Delete();
                            break;

                        case 4:
                            break;
                        }
                        if (choice == 4)
                        {
                            break;
                        }
                    }
                    break;

                case 2:
                    contactsCollection.SearchPersonInSameCityOrState();

                    break;

                case 3:
                    contactsCollection.ViewPersonByCityOrState();
                    break;

                case 4:
                    contactsCollection.PrintNameInAlphabeticalOrder();
                    break;

                case 5:
                    contactsCollection.SortByCityStateOrZip();
                    break;

                case 6:
                    FileOperations.ReadFromStreamReader();
                    FileOperations.WriteUsingStreamWriter();
                    break;

                case 7:
                    c.WriteInCsvFile();
                    break;

                case 8:
                    c.ReadFromCsv();

                    break;

                case 9:
                    readCsvWriteJSON.WriteJson();
                    break;

                case 10:
                    break;
                }
                if (preference == 10)
                {
                    break;
                }
            }
        }
        public static void Main(string[] args)
        {
            Dictionary <String, List <Contacts> > sorted = new Dictionary <String, List <Contacts> >();
            int c1 = 0;

            while (c1 != 12)
            {
                string bname = "";
                Console.WriteLine("Welcome to Address Book Program");
                List <Contacts> gcontacts = new List <Contacts>();   //stores contacts list for different address books

                Console.WriteLine("1. Add Address Book: ");
                Console.WriteLine("2. Edit a Particular Address Book: ");
                Console.WriteLine("3. Display Address Book: ");
                Console.WriteLine("4. View Person's Details By City: ");
                Console.WriteLine("5. View Person's Details By State: ");
                Console.WriteLine("6. Write Address Book into a File: ");
                Console.WriteLine("7. Read Address Book from a File: ");
                Console.WriteLine("8. Write and Read Address Book(CSV File): ");
                Console.WriteLine("9. Write and Read Address Book(JSON File): ");
                Console.WriteLine("10. Clear Address Book Details from all Files(Txt, CSV, JSON): ");
                Console.WriteLine("11. Retrieve Contacts By City or State From DB ");
                Console.WriteLine("12. Exit");

                Console.WriteLine("Enter your choice: ");
                c1 = Convert.ToInt32(Console.ReadLine());
                switch (c1)
                {
                case 1:
                    Console.WriteLine("Enter the name of Address Book: ");
                    bname = Console.ReadLine();
                    List <Contacts> contacts = new List <Contacts>();    //stores contacts list for a particular book

                    AddressBookMainLogic.edit_data(contacts);

                    gcontacts.AddRange(contacts);
                    sorted.Add(bname, gcontacts);
                    break;

                case 2:
                    Console.WriteLine("Enter the name of Address Book: ");
                    string bname1 = Console.ReadLine();
                    if (sorted.ContainsKey(bname1))
                    {
                        List <Contacts> edit = sorted[bname1];
                        AddressBookMainLogic.edit_data(edit);
                    }
                    else
                    {
                        Console.WriteLine("Mentioned Address Book is not there");
                    }
                    break;

                case 3:
                    foreach (KeyValuePair <String, List <Contacts> > kv in sorted)
                    {
                        string          a     = kv.Key;
                        List <Contacts> list1 = (List <Contacts>)kv.Value;
                        Console.WriteLine("Address Book Name: " + a);
                        foreach (Contacts c in list1)
                        {
                            Console.WriteLine(c);
                        }
                    }
                    break;

                case 4:
                    Console.WriteLine("Enter the City Name: ");
                    string city = Console.ReadLine();
                    int    fg   = 0;

                    Dictionary <string, List <Contacts> > cty = new Dictionary <string, List <Contacts> >();
                    List <Contacts> gtemp = new List <Contacts>();

                    foreach (KeyValuePair <string, List <Contacts> > kv in sorted)
                    {
                        List <Contacts> list1 = kv.Value;    //gives list details per address book
                        List <Contacts> temp  = new List <Contacts>();
                        foreach (Contacts c in list1)
                        {
                            if (c.city.ToLower().Equals(city.ToLower()))
                            {
                                temp.Add(c);
                                fg++;
                            }
                        }
                        gtemp.AddRange(temp);                //Appends person's details per book by city
                    }
                    cty.Add(city, gtemp);

                    if (fg == 0)
                    {
                        Console.WriteLine("Mentioned City Name isn't present in Address Book");
                    }
                    else
                    {
                        Console.WriteLine("Total records for City Name " + city + ": " + fg);
                        foreach (KeyValuePair <string, List <Contacts> > kv in cty)
                        {
                            string          a   = kv.Key;
                            List <Contacts> lst = kv.Value;
                            Console.WriteLine("City Name: " + a);
                            foreach (Contacts c in lst)
                            {
                                Console.WriteLine(c);
                            }
                        }
                    }
                    break;

                case 5:
                    Console.WriteLine("Enter the State Name: ");
                    string state = Console.ReadLine();
                    int    fg1   = 0;

                    Dictionary <string, List <Contacts> > st = new Dictionary <string, List <Contacts> >();
                    List <Contacts> gtemp1 = new List <Contacts>();

                    foreach (KeyValuePair <string, List <Contacts> > kv in sorted)
                    {
                        List <Contacts> list1 = kv.Value;    //gives list details per address book
                        List <Contacts> temp  = new List <Contacts>();
                        foreach (Contacts c in list1)
                        {
                            if (c.state.ToLower().Equals(state.ToLower()))
                            {
                                temp.Add(c);
                                fg1++;
                            }
                        }
                        gtemp1.AddRange(temp);                //Appends person's details per book by state
                    }
                    st.Add(state, gtemp1);

                    if (fg1 == 0)
                    {
                        Console.WriteLine("Mentioned State Name isn't present in Address Book");
                    }
                    else
                    {
                        Console.WriteLine("Total records for State Name " + state + ": " + fg1);
                        foreach (KeyValuePair <string, List <Contacts> > kv in st)
                        {
                            string          a   = kv.Key;
                            List <Contacts> lst = kv.Value;
                            Console.WriteLine("City Name: " + a);
                            foreach (Contacts c in lst)
                            {
                                Console.WriteLine(c);
                            }
                        }
                    }

                    break;

                case 6:
                    if (File.Exists(filePath))
                    {
                        using (StreamWriter stw = File.CreateText(filePath))
                        {
                            foreach (KeyValuePair <String, List <Contacts> > kv in sorted)
                            {
                                string          a     = kv.Key;
                                List <Contacts> list1 = (List <Contacts>)kv.Value;
                                stw.WriteLine("Address Book Name: " + a);
                                foreach (Contacts c in list1)
                                {
                                    stw.WriteLine(c);
                                }
                            }
                            Console.WriteLine("Address Book written into the file successfully!!!");
                        }
                    }
                    else
                    {
                        Console.WriteLine("File doesn't exist!!!");
                    }
                    break;

                case 7:
                    if (File.Exists(filePath))
                    {
                        using (StreamReader str = File.OpenText(filePath))
                        {
                            string s = "";
                            while ((s = str.ReadLine()) != null)
                            {
                                Console.WriteLine(s);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("File doesn't exist!!!");
                    }
                    break;

                case 8:
                    Console.WriteLine("Enter the Address Book Name:");
                    string nameCSV = Console.ReadLine();
                    if (sorted.ContainsKey(nameCSV))
                    {
                        CSVHandler.WriteIntoCSVFile(sorted, nameCSV);
                        Console.WriteLine("Data inserted successfully");
                        CSVHandler.ReadFromCSVFile();
                        Console.WriteLine("Data read successfully");
                    }
                    else
                    {
                        Console.WriteLine("Book Name Not Found");
                    }
                    break;

                case 9:
                    Console.WriteLine("Enter the Address Book Name:");
                    string nameJSON = Console.ReadLine();
                    if (sorted.ContainsKey(nameJSON))
                    {
                        JSONHandler.WriteIntoJSONFile(sorted, nameJSON);
                        Console.WriteLine("Data inserted successfully");
                        JSONHandler.ReadFromJSONFile();
                        Console.WriteLine("Data read successfully");
                    }
                    else
                    {
                        Console.WriteLine("Book Name Not Found");
                    }
                    break;

                case 10:
                    File.WriteAllText(filePath, string.Empty);
                    CSVHandler.ClearData();
                    JSONHandler.ClearData();
                    Console.WriteLine("All Files cleared successfully!!!");
                    break;

                case 11:
                    AddressRepoDB.RetrieveDataByCityorState();
                    break;
                }
            }
        }
        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 static void Main(string[] args)
        {
            int               option, option1;
            string            bookName    = "Default";
            AddressBookBuider addressBook = new AddressBookBuider();

            Console.WriteLine("1. Would you like to work on existing addressbook ? if yes press 1");
            Console.WriteLine("2. Would you like to add  new addressbook ? if yes press 2");
            option1 = Convert.ToInt32(Console.ReadLine());
            switch (option1)
            {
            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
            {
                Console.WriteLine($"you are working on {bookName} addressbook ");
                Console.WriteLine("Enter your option :");
                Console.WriteLine("1. Add new contact ");
                Console.WriteLine("2. Display the contacts");
                Console.WriteLine("3. Edit  contacts");
                Console.WriteLine("4. Delete  contacts");
                Console.WriteLine("5. Add new addressbook");
                Console.WriteLine("6. Switch Addressbook");
                Console.WriteLine("7. Search person in a city or State");
                Console.WriteLine("8. Get count of  persons by city or State");
                Console.WriteLine("9. Sort Entries by Person name");
                Console.WriteLine("10.Read or write addressbook contacts using File IO");
                Console.WriteLine("11.Read or write addressbook contacts using CSV file");
                Console.WriteLine("12.Read or write addressbook contacts using Json file");
                option = Convert.ToInt32(Console.ReadLine());

                switch (option)
                {
                case 1:
                    Console.WriteLine("Enter First Name :");
                    string firstName = Console.ReadLine();
                    Console.WriteLine("Enter Last Name :");
                    string   lastName       = Console.ReadLine();
                    Contacts duplicateCheck = new Contacts(firstName, lastName, null, null, null, null, 0, 0);
                    if (addressBook.CheckFor_Duplicate(duplicateCheck, 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 = Convert.ToInt64(Console.ReadLine());
                    addressBook.AddContact(firstName, lastName, address, city, state, email, zip, phoneNumber, bookName);
                    break;

                case 2:
                    addressBook.DisplayContact(bookName);
                    break;

                case 3:
                    Console.WriteLine("Enter Full Name of contact which to be edited");
                    string NameToEdit = Console.ReadLine();
                    addressBook.EditContact(NameToEdit, bookName);
                    break;

                case 4:
                    Console.WriteLine("Enter Full Name of contact which to be deleted");
                    string NameToDelete = Console.ReadLine();
                    addressBook.DeleteContact(NameToDelete, bookName);
                    break;

                case 5:
                    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 6:
                    Console.WriteLine("Enter Name Of AddressBook From Below List");
                    foreach (KeyValuePair <string, AddressBookBuider> 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 7:
                    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 8: addressBook.GetCountByCityOrState(bookName);
                    break;

                case 9: addressBook.SortEntryByName();
                    break;

                case 10:
                    ReadWriteFileIO fileIO = new ReadWriteFileIO();
                    fileIO.WriteToFile(addressBook.addressBookDictionary);
                    fileIO.ReadFromFile();
                    break;

                case 11:
                    CSVHandler handler = new CSVHandler();
                    handler.WriteToCsv(addressBook.addressBookDictionary);
                    handler.ReadFromCSV();
                    break;

                case 12: ReadWriteJson json = new ReadWriteJson();
                    json.WriteToFile(addressBook.addressBookDictionary);
                    json.ReadFromFile();
                    break;

                default:
                    Console.WriteLine("wrong input");
                    break;
                }
                Console.WriteLine("Do you want to continue to The Main Menu?  press 1 if yes,press 0 for Exit");
                option = Convert.ToInt32(Console.ReadLine());
            }while (option != 0);
        }