-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress_book.rb
executable file
·154 lines (140 loc) · 3.97 KB
/
address_book.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require "./contact"
require "yaml"
class AddressBook
attr_reader :contacts
def initialize
@contacts = []
open()
end #initialize
def run
loop do
puts "Address Book"
puts "a: Add New Contact"
puts "s: Search Contacts"
puts "p: Print Address Book"
puts "e: Exit"
print "Enter your choice: "
input = gets.chomp.downcase
case input
when 'e'
save()
break
when 'p'
print_contact_list
when 'a'
add_contact
when 's'
print "Search term: "
search = gets.chomp
find_by_name(search)
find_by_phone_number(search)
find_by_address(search)
end #case
end #loop
end #def (run)
def open
if File.exist?("contacts.yml")
@contacts = YAML.load_file("contacts.yml")
end
end #def (open)
def save
File.open("contacts.yml", "w") do |file|
file.write(contacts.to_yaml)
end #file.open
end #def (open)
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)
loop do
puts "Add a phone number or address? "
puts "p: Add phone number"
puts "a: Add address"
puts "Enter any other key to go back"
response = gets.chomp.downcase
case response
when 'p'
phone = PhoneNumber.new
print "Phone number type (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 #case
end #loop
end #def (add_contact)
def print_contact_list
puts "Contact List:" + "\n"
contacts.each do |contact|
puts contact.to_s('last_first')
end #each
end #def print_contact_list
def find_by_name(name)
results = []
search = name.downcase
# Collect search results
contacts.each do |contact|
if contact.full_name.downcase.include?(search)
results.push(contact)
end #if
end #each
print_results("Name search results (#{search}):", results)
end #def find_by_name
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 #if
end #each
end #each
print_results("Phone search results (#{search}):", results)
end #def (find_by_phone_number)
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 #if
end #each
end #each
print_results("Address search results (#{search}):", results)
end #def (find_by_address)
def print_results(search_label, results)
puts search_label
results.each do |contact|
puts "\n" + contact.to_s("full_name")
contact.print_phone_numbers
contact.print_addresses
end
end #def (print_results)
end #class AddressBook
address_book = AddressBook.new
address_book.run