/* * 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(); // Try...Catch alerting user if contact to be added already exists try { addressBook.AddContact(bob); addressBook.AddContact(sue); addressBook.AddContact(juan); // Try to addd a contact a second time addressBook.AddContact(sue); } catch (Exception ex) { Console.WriteLine("NOTE: You can't add an existing contact, dummy!"); } // 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...Catch alerting user if email does not match any existing contact try { Contact contact = addressBook.GetByEmail(email); Console.WriteLine("----------------------------"); Console.WriteLine($"Name: {contact.FullName}"); Console.WriteLine($"Email: {contact.Email}"); Console.WriteLine($"Address: {contact.Address}"); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine($"NOTE: '{email}' does not match any existing contact"); } } }
/* * 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 // try // { // addressBook.AddContact(sue); // } // catch (ArgumentException) // { // Console.WriteLine($"Contact is currently in address book"); // } // 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 (KeyNotFoundException) { Console.WriteLine("----------------------------"); Console.WriteLine($"No contact in address book has {email} as an email."); } } }
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 addd 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 //looping through the list of emails and finding contact info for specific contact based on that email 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 address not in address book*****"); } } }
static void Main(string[] args) { 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" }; AddressBook addressBook = new AddressBook(); addressBook.AddContact(bob); addressBook.AddContact(sue); addressBook.AddContact(juan); // Let's see what happens... addressBook.AddContact(sue); List <string> emails = new List <string>() { "*****@*****.**", "*****@*****.**", "*****@*****.**" }; // Insert an email that does NOT match a contact... emails.Insert(1, "*****@*****.**"); emails.ForEach(email => { try { Contact contact = addressBook.GetByEmail(email); Console.WriteLine("-----------------------"); Console.WriteLine($"Name: {contact.FirstName} {contact.LastName}"); Console.WriteLine($"Email: {contact.Email}"); Console.WriteLine($"Address: {contact.Address}"); } catch (NullReferenceException ex) { Console.WriteLine($"No contact found with email address: {email}"); } }); }