Skip to content

Instantly share code, notes, and snippets.

@Lvtran1987
Created December 19, 2015 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lvtran1987/1f3447ad6137ac8ea360 to your computer and use it in GitHub Desktop.
Save Lvtran1987/1f3447ad6137ac8ea360 to your computer and use it in GitHub Desktop.
require "./contact.rb"
class AddressBook
attr_reader :contacts
def initialize
@contacts = []
end
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.fname = gets.chomp
print "Middle name: "
contact.mname = gets.chomp
print "Last name: "
contact.lname = gets.chomp
loop do
puts "Add phone number or Address? "
puts "p: Add phone number"
puts "a: Add address"
puts 's: Add search term'
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)
when 's'
print "Search term: "
search = gets.chomp
find_by_name(search)
find_by_phone_number(search)
find_by_address(search)
else
break
end
contacts.push(contact)
end
contacts.push(contact)
end
def find_by_name(name) # => Search feature for the address book.
results = [] # => This is how well search for the contact
search = name.downcase
contacts.each do |contact|
if contact.fname.downcase.include?(search) ||
contact.lname.downcase.include?(search)
results.push(contact)
end
end
puts "Name search results #{search}"
results.each do|contact|
puts contact.to_s('full_name')
contact.print_phone_numbers
contact.print_addresses
puts "-" * 30
end
end
def find_by_phone_number(number)
results = []
search = number.gsub("-", "")
contacts.each do |contact|
contact.phone_numbers.each do |phone_number|
if phone_number.number.gsub("-", "").include?(search)
results.push(contact) unless results.include?(contact)
end
end
end
puts "Number search results #{search}"
results.each do|contact|
puts contact.to_s('full_name')
contact.print_phone_numbers
contact.print_addresses
puts "-" * 30
end
end
def find_by_address(query)
results = []
search = query.downcase
contacts.each do |contact|
contact.addresses.each do |address|
if address.to_s('long').downcase.include?(search)
results.push(contact) unless results.include?(contact)
end
end
end
puts "Address search results #{search}"
results.each do|contact|
puts contact.to_s('full_name')
contact.print_phone_numbers
contact.print_addresses
puts "-" * 30
end
end
def print_contact_list
contacts.each do |contact|
puts contact.to_s('lastfirst')
end
end
end
address_book = AddressBook.new
loi = Contact.new
loi.fname = "Loi"
loi.mname = "Van"
loi.lname = "Tran"
loi.add_phone_number("Home", "850-123-1234")
loi.add_phone_number("Work", "123-456-1234")
loi.add_address("Mansion", "123 Winners circle Star Island", "", "Miami", "FL", "12345")
thao = Contact.new
thao.fname = "Thao"
thao.mname = "Thi"
thao.lname = "Tran"
thao.add_phone_number("Home", "850-508-5978")
thao.add_address("Home", "2405 Nugget Lane.", "", "Tallahassee", "FL", "32303")
address_book.contacts.push(thao)
address_book.contacts.push(loi)
# address_book.run
# address_book.print_contact_list
# address_book.find_by_name("Thao")
# address_book.find_by_name("Loi")
# address_book.find_by_phone_number("850-508-5978")
# address_book.find_by_phone_number("123")
address_book.find_by_address("123")
address_book.find_by_address("2405")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment