public void EditAddOrDeleteContact(AddressBook addressBook, string addressBookName) { string[] name; int choice = 0; string[] details; bool flag = true; while (flag) { Console.WriteLine("1.Add New Contact\n2.Edit Contact\n3.Remove contact\n4.Sort By Name\n5.Sort By City\n6.Sort By State\n7.Sort By ZipCode\n8.Write To File\n9.Read from File\n10.Exit"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Enter the details separated by comma"); Console.WriteLine("First Name, Last Name, Address, City, State, ZipCode,Phone No Email"); details = Console.ReadLine().Split(","); string message = addressBook.AddContact(details[0], details[1], details[2], details[3], details[4], details[5], details[6], details[7]); Console.WriteLine(message); break; case 2: Console.WriteLine("Enter the name to edit"); name = Console.ReadLine().Split(" "); if (addressBook.CheckName(name[0], name[1]) == true) { Console.WriteLine("Enter the following details separated by comma"); Console.WriteLine("FirstName,LastName,Address, City, State, ZipCode,Phone No Email"); details = Console.ReadLine().Split(","); addressBook.EditContact(details[0], details[1], details[2], details[3], details[4], details[5], details[6], details[7]); Console.WriteLine("Details editted successfully"); } else { Console.WriteLine("No such contact found"); } break; case 3: Console.WriteLine("Enter the name to be removed"); name = Console.ReadLine().Split(" "); if (addressBook.CheckName(name[0], name[1]) == true) { addressBook.RemoveContact(name[0], name[1]); Console.WriteLine("Contact Removed Successfully"); } else { Console.WriteLine("No such contact found"); } break; case 4: addressBook.SortByName(); break; case 5: addressBook.SortByCity(); break; case 6: addressBook.SortByState(); break; case 7: addressBook.SortByZipCode(); break; case 8: addressBook.ClearFile(); addressBook.WriteToFile(addressBookName); Console.WriteLine("Written to file successfully"); break; case 9: addressBook.ReadFromFile(); break; case 10: flag = false; break; default: break; } } }
static void Main(string[] args) { int choice; string addBookName = ""; MultipleAddressBook multipleAddressBooks = new MultipleAddressBook(); Operations operation = new Operations(); AddressBook addressBook = null; Console.WriteLine("Welcome to Address Book Program"); while (true) { Console.WriteLine("1.Add Address Book\n2.Edit Or Add Contact in Address Book\n3.View Persons By City\n4.View Persons By State\n5.Count By City\n6.Count By State\n7.Exit"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Enter name of Address Book"); addBookName = Console.ReadLine(); multipleAddressBooks.AddAddressBook(addBookName); break; case 2: Console.WriteLine("Enter name of Address Book"); addBookName = Console.ReadLine(); addressBook = multipleAddressBooks.GetAddressBook(addBookName); if (addressBook != null) { operation.EditAddOrDeleteContact(addressBook, addBookName); } else { Console.WriteLine("No such Adress Book"); } break; case 3: Console.WriteLine("Enter City"); string city = Console.ReadLine(); multipleAddressBooks.SetContactByCityDictionary(); multipleAddressBooks.ViewPersonsByCity(city); break; case 4: Console.WriteLine("Enter State"); string state = Console.ReadLine(); multipleAddressBooks.SetContactByStateDictionary(); multipleAddressBooks.ViewPersonsByState(state); break; case 5: multipleAddressBooks.SetContactByCityDictionary(); foreach (var contactByCity in multipleAddressBooks.ContactByCity) { Console.WriteLine("City :" + contactByCity.Key + " Count :" + contactByCity.Value.Count); } break; case 6: multipleAddressBooks.SetContactByStateDictionary(); foreach (var contactByState in multipleAddressBooks.ContactByState) { Console.WriteLine("State :" + contactByState.Key + " Count :" + contactByState.Value.Count); } break; case 7: Environment.Exit(0); break; default: Console.WriteLine("Invalid Choice"); break; } } }
/* * 1. Add the required classes to make the following code compile. * HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address. * * 2. Run the program and observe the exception. * * 3. Add try/catch blocks in the appropriate locations to prevent the program from crashing * Print meaningful error messages in the catch blocks. */ static void Main(string[] args) { // Create a few contacts Contact bob = new Contact() { FirstName = "Bob", LastName = "Smith", Email = "*****@*****.**", Address = "100 Some Ln, Testville, TN 11111" }; Contact sue = new Contact() { FirstName = "Sue", LastName = "Jones", Email = "*****@*****.**", Address = "322 Hard Way, Testville, TN 11111" }; Contact juan = new Contact() { FirstName = "Juan", LastName = "Lopez", Email = "*****@*****.**", Address = "888 Easy St, Testville, TN 11111" }; // Create an AddressBook and add some contacts to it AddressBook addressBook = new AddressBook(); addressBook.AddContact(bob); addressBook.AddContact(sue); addressBook.AddContact(juan); // Try to add a contact a second time addressBook.AddContact(sue); // Create a list of emails that match our Contacts List <string> emails = new List <string>() { "*****@*****.**", "*****@*****.**", "*****@*****.**", }; // Insert an email that does NOT match a Contact emails.Insert(1, "*****@*****.**"); // Search the AddressBook by email and print the information about each Contact foreach (string email in emails) { try { Contact contact = addressBook.GetByEmail(email); Console.WriteLine("----------------------------"); Console.WriteLine($"Name: {contact.FullName}"); Console.WriteLine($"Email: {contact.Email}"); Console.WriteLine($"Address: {contact.Address}"); } catch { Console.WriteLine($"{email} is not a valid email"); } } }