This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Our address book functionality is getting there. In this video, we implement the ability to add contacts via the command line.
Code Samples
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
loop do
puts "Add phone number or address? "
puts "p: Add phone number"
puts "a: Add address"
puts "(Any other key to go back)"
response = gets.chomp.downcase
case response
when 'p'
phone = PhoneNumber.new
print "Phone number kind (Home, Work, etc): "
phone.kind = gets.chomp
print "Number: "
phone.number = gets.chomp
contact.phone_numbers.push(phone)
when 'a'
address = Address.new
print "Address Kind (Home, Work, etc): "
address.kind = gets.chomp
print "Address line 1: "
address.street_1 = gets.chomp
print "Address line 2: "
address.street_2 = gets.chomp
print "City: "
address.city = gets.chomp
print "State: "
address.state = gets.chomp
print "Postal Code: "
address.postal_code = gets.chomp
contact.addresses.push(address)
else
print "\n"
break
end
end
contacts.push(contact)
end
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So, when we just left off,
we had added the ability to add a contact
0:00
to append to the internal contacts array.
0:06
Now, what we're gonna do is add
the ability to add addresses and
0:08
phone numbers to this
contact when we add them.
0:13
So, we're gonna do that right here and
we're gonna do this using another loop.
0:17
So we'll say loop do and end.
0:22
So now we'll just display another menu, so
0:27
when we add this contact we'll
say add phone number or address.
0:31
And then we can use the letter
p to add a phone number
0:38
and a to add an address.
0:44
And then we will ask for a response.
0:52
And make it lowercase.
0:57
And now we have our response case method.
1:02
Now what do we do when we have a response?
1:08
Well, when it's the letter p,
we'll add a phone number,
1:10
when it's the letter a we'll add
an address, and if it's anything else,
1:14
we'll go back which will bring
us back into the main menu loop.
1:20
And let's go ahead and
just add a little message for that,
1:25
that says, Any other key to go back.
1:28
Okay, and actually let's go ahead and
print out a new line before we go back.
1:34
All right, so when the letter p happens,
let's go ahead and add a phone number.
1:40
Now, the way that we do this is similar
to how we just added a contact up here.
1:45
First we will initialize a new instance
of the phone number class, and
1:50
then we'll just ask the user for input.
1:56
So it's the letter p.
2:00
We'll say a phone is a new phone number.
2:01
And then we'll print out,
2:09
what kind of phone number is it,
Home, work etc.
2:12
And then the kind of phone number
is standard input once again.
2:21
And now that we have the kind we'll
ask them to input the number.
2:28
And then we can append
2:35
this phone number.
2:41
And that should bring us back to
the menu with the option to add
2:47
another phone number or an address.
2:51
And so adding the address is going
to follow the exact same pattern.
2:55
So I'm just gonna go ahead and
paste that in here.
3:01
So we initialize a new address instance,
ask what kind it is,
3:09
allow them to enter all of
the address information.
3:13
I'm gonna append it to
the internal addresses array.
3:17
At the end of all this, it will be
pushed on to the contacts array.
3:22
So now, let's go ahead and
run this and see how it's looking.
3:28
Go ahead and type in ruby Address_book.rb.
3:32
Let's go ahead and
just print the address book.
3:36
Okay, there's no one in the contact list.
3:40
Oh, the choice is not one,
it is a to add a contact,
3:44
Jason, middle name no middle name,
last name Seifer.
3:47
Do I wanna add a phone number or
an address?
3:51
Yeah, I think I'd like
to add a phone number.
3:53
Home phone number, 123-456-7890.
3:57
That is my actual phone number.
4:02
Please do not call me.
4:03
Okay, and let me add an address too.
4:05
Add a home address, 123 Home Address.
4:08
And that's in Orlando, Florida.
4:15
Postal code is 12345.
4:20
Okay, that seems to be working.
4:22
Let's go back.
4:24
Let's go ahead and print the address
book and see how it looks.
4:25
All right, got the contact list,
with Jason Seifer, looks good.
4:28
So I can go ahead and press e to exit and
next up we'll add the ability to search.
4:38
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up