Example #1
0
        static void Main(string[] args)
        {
            // Create Contacts for address book
            Contact Abbey = new Contact()
            {
                FirstName = "Abbey", LastName = "Brown",
                Email     = "*****@*****.**",
                Address   = "1900 Lowlands, Nashville, TN 37216"
            };
            Contact Rupert = new Contact()
            {
                FirstName = "Rupert", LastName = "Jones",
                Email     = "*****@*****.**",
                Address   = "9832 Harding Place, Nashville, TN 37207"
            };
            Contact Katerina = new Contact()
            {
                FirstName = "Katerina", LastName = "Freeman",
                Email     = "*****@*****.**",
                Address   = "000 Who Knows, WhiteHouse, TN 37072"
            };


            // Create an AddressBook and add Contacts to it
            AddressBook tacoAddressBook = new AddressBook();

            tacoAddressBook.AddContact(Abbey);
            tacoAddressBook.AddContact(Rupert);
            tacoAddressBook.AddContact(Katerina);

            // Try to add a contact a second time
            tacoAddressBook.AddContact(Abbey);


            // Create a list of emails that match our Contacts
            List <string> emails = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            // Insert an email that does NOT match a Contact (insert method)
            //Takes the argument of index you want to insert and the list item value
            emails.Insert(3, "*****@*****.**");


            //  Search AddressBook by email and print the information about each Contact
            foreach (string email in emails)
            {
                try
                {
                    Contact contact = tacoAddressBook.GetByEmail(email);
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                catch (KeyNotFoundException ex)
                {
                    Console.WriteLine($@"
                ----------------------------
                Unable to find {email} in Address Book.
                ----------------------------
                Developer ERROR Message: {ex.Message}
                "
                                      );
                }
            }
        }
        /*
         *  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
            {
                addressBook.AddContact(bob);
                addressBook.AddContact(sue);
                addressBook.AddContact(juan);
                addressBook.AddContact(sue);
            }
            catch
            {
                Console.WriteLine("You've already added this contact");
            }

            // Try to addd a contact a second time


            // 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 (System.NullReferenceException)
                {
                    Console.WriteLine("There is no contact");
                }
            }
        }
Example #3
0
        /*
         *  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.
         *
         * // Focus on learning to read the error messages and be comfortable with it.
         *  System.NullReferenceException = most common C# error // Object Reference not set to an instance of an object.
         *  Stack trace - describes to you what part of the program stops running - the at
         */


        static void Main(string[] args)
        {
            // Make it pretty
            Console.Title           = "Try Catch";
            Console.ForegroundColor = ConsoleColor.Green;

            // 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
            // Indexes start at 0, so this throws an error
            emails.Insert(1, "*****@*****.**");


            //  Search the AddressBook by email and print the information about each Contact
            foreach (string email in emails)
            {
                // Instantiate contact - make it into a variable you can use again
                Contact contact = addressBook.GetByEmail(email);
                // If the contact returns null and none of those properties below return anything because contact is null
                // Move on to the next contact
                if (contact != null)
                {
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                // Don't need it, but it's good to specify what exactly is null
                else
                {
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Contact is null for {email}");
                }
            }
        }