/// Counting a number of people in a city or in a state /// Taking the input from the user and printing out the data correspondingly public static void CountPeopleFromCityAndState() { Console.WriteLine("Enter the name of the state"); string state = Console.ReadLine(); Console.WriteLine("Enter the name of the city"); string city = Console.ReadLine(); int cityPeople = 0; int statePeople = 0; foreach (KeyValuePair <string, AddressBookMain> keys in addresssBookMap) { AddressBookMain val = keys.Value; if (val.state.Contains(state)) { statePeople++; } } Console.WriteLine("No. of people in the state " + state + " are " + statePeople); foreach (KeyValuePair <string, AddressBookMain> key in addresssBookMap) { AddressBookMain value = key.Value; if (value.address.Contains(city)) { cityPeople++; } } Console.WriteLine("No. of people in the city " + city + " are " + cityPeople); }
//Adding the values to the dictionary public static void AddToAddressBook() { Console.WriteLine("Enter firstname"); string firstName = Console.ReadLine(); if (addresssBookMap.ContainsKey(firstName)) { Console.WriteLine("The user name already exists"); } else { Console.WriteLine("Enter lastname"); string lastName = Console.ReadLine(); Console.WriteLine("Enter address"); string address = Console.ReadLine(); Console.WriteLine("Enter state"); string state = Console.ReadLine(); Console.WriteLine("Enter zip"); int zip = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter phone number"); double phoneNo = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter email id"); string emailId = Console.ReadLine(); AddressBookMain addressBookMain = new AddressBookMain(firstName, lastName, address, state, zip, phoneNo, emailId); addresssBookMap.Add(firstName, addressBookMain); PrintAddress(firstName); } }
//Printing the values from the dictionary public static void PrintAddress(String key) { foreach (KeyValuePair <string, AddressBookMain> items in addresssBookMap) { AddressBookMain adBook = items.Value; if (adBook.firstName.Equals(key)) { Console.WriteLine("Name:" + adBook.firstName); Console.WriteLine("Last name: " + adBook.lastName); Console.WriteLine("Address: " + adBook.address); Console.WriteLine("State: " + adBook.state); Console.WriteLine("Zip code: " + adBook.zip); Console.WriteLine("Phone: " + adBook.phoneNo); Console.WriteLine("Email: " + adBook.emailId); } } }
//Printing the values from the dictionary public void PrintAddress(String key) { foreach (KeyValuePair <string, AddressBookMain> items in this.addresssBookMap) { AddressBookMain adBook = items.Value; if (adBook.firstName.Equals(key)) { Console.WriteLine(adBook.firstName); Console.WriteLine(adBook.lastName); Console.WriteLine(adBook.address); Console.WriteLine(adBook.state); Console.WriteLine(adBook.zip); Console.WriteLine(adBook.phoneNo); Console.WriteLine(adBook.emailId); } } }
/// Editing the details of the address book /// Taking input from the user and updating the address book after taking the new name public static void EditDetails() { Console.WriteLine("Enter the name you want to edit"); string name = Console.ReadLine(); Console.WriteLine("Enter the new name"); string newName = Console.ReadLine(); if (addresssBookMap.ContainsKey(name)) { addresssBookMap[name].firstName = newName; } AddressBookMain addressBook = new AddressBookMain(newName, addresssBookMap[name].lastName, addresssBookMap[name].address, addresssBookMap[name].state, addresssBookMap[name].zip, addresssBookMap[name].phoneNo, addresssBookMap[name].emailId); addresssBookMap.Add(newName, addressBook); Console.WriteLine("Edited details are"); PrintAddress(newName); }
/// Method to print out a person's detail by his state /// Input taken from the user and searched accordingly public static void SearchByState() { Console.WriteLine("Enter the name of the state"); string state = Console.ReadLine(); foreach (KeyValuePair <string, AddressBookMain> keys in addresssBookMap) { AddressBookMain val = keys.Value; if (val.state.Contains(state)) { Console.WriteLine(val.firstName + "'s Address"); PrintAddress(val.firstName); } else { Console.WriteLine("Address wasn't found"); } } }
//Method to print put a person's detail by the name of the city //Getting input from the user and giving out the existing field records public static void SearchByCity() { Console.WriteLine("Enter name of the city"); string city = Console.ReadLine(); foreach (KeyValuePair <string, AddressBookMain> keys in addresssBookMap) { AddressBookMain val = keys.Value; if (val.address.Contains(city)) { Console.WriteLine(val.firstName + "'s Address:"); PrintAddress(val.firstName); } else { Console.WriteLine("Address wasn't found"); } } }
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!!"); }