Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding Contacts: Part 14:41 with Jason Seifer
Our address book functionality is getting there. In this video, we implement the ability to add contacts via the command line.
def run loop do puts "Address Book" puts "a: Add Contact" puts "p: Print Address Book" puts "e: Exit" print "Enter your choice: " input = gets.chomp.downcase case input when 'a' add_contact when 'p' print_contact_list when 'e' break end end end def add_contact contact = Contact.new print "First name: " contact.first_name = gets.chomp print "Middle name: " contact.middle_name = gets.chomp print "Last name: " contact.last_name = gets.chomp contacts.push(contact) end
Okay, so we have the beginning of our menu set up. 0:00 Let's go ahead and add another option to print out our contact list. 0:03 Now, if we scroll down here, we already have this method called print_contact_list 0:08 which does exactly what we want to. 0:13 So, all we need to do is add this as a menu option. 0:16 So we'll go ahead and add the option to print it out. 0:22 There we go. 0:29 That will just display it on the menu. 0:30 Now we need to add this as an option. 0:33 And this is really easy, all we need to do is say. 0:36 When the input is the letter p, we call the print_contact_list method. 0:40 Let's just make sure we spelled it correctly. 0:48 And we did. 0:51 So this should be everything that we need to do. 0:53 Let's go ahead and run this and make sure it works. 0:56 All right. Here are two options, 1:02 p to print the address book, and e to exit. 1:04 Well, let's go ahead and print it. 1:07 Well, it prints the word Contact List, but nothing's in there. 1:09 And why is that? 1:13 It's because we haven't added any contacts. 1:15 So, what we're gonna have to do is add the ability to add contacts in here. 1:18 Now we're gonna do that in two parts here. 1:28 So let's go ahead and add the ability to add a contact. 1:32 And add a menu item for it. 1:40 And we're just going to say when the input letter is a, 1:44 we're gonna call the add contact method, which we have to write now. 1:49 So this is gonna be similar to what we did before. 2:01 And let's go ahead, and since we're adding a contact, 2:05 which we're going to be appending to the internal contacts array, 2:09 let's go ahead and initialize a new contact. 2:13 And now we can just prompt the user to enter the information about their contact. 2:16 Enter their first name, and 2:24 then the same thing with last name. 2:28 Oops, forgot the middle name here. 2:38 Okay, that looks good. 2:49 Now we're in an interesting position here because we could append the contact to 2:51 the contacts array right now, but we still want to be able 2:55 to enter addresses or phone numbers for each contact. 3:00 Now we'll get to that in a minute, but for 3:04 right now let's just append the contacts array. 3:06 Now remember, we've got an attribute reader for the contacts array. 3:11 So we don't have to use the at sign to append it. 3:16 So we'll just say contacts.push and 3:20 it will be this contact right here. 3:26 So let's click down here and exit, all right, clear the screen, 3:32 I'm gonna run this again. 3:36 And see what happens so let me add a contact, Jason, no middle name, Seifer. 3:38 Okay that seems to have worked, now let's go ahead and print the address book. 3:44 Okay, looks like I'm in here, let me add another contact. 3:49 Nick Avery Pettit. 3:56 Okay, that seems to have worked. 4:00 Let's go ahead and print the address book. 4:03 Okay, looks like that's working. 4:06 And let's just see something really quickly here. 4:10 If we exit, and I'm gonna clear my screen, and if I restart again, 4:12 if we go to print the address book, 4:16 you'll notice that it's empty even though we just added Nick and Jason as contacts. 4:18 The reason is, that takes place entirely in memory and we are gonna be writing this 4:24 out to a file but we'll be doing that a little bit later. 4:28 Next up we're gonna be going through and doing the loop for adding addresses and 4:31 phone numbers to our contact before pinning them to the contact's array. 4:35
You need to sign up for Treehouse in order to download course files.Sign up