class PhoneBook
def run
showMenu()
choice = gets.gets
while choice != "5"
if choice==1
showAllEntries()
elsif choice==2
print "Enter a name to search for: "
target = gets.chomp
index = findEntryByName(target)
if (index != -1)
puts entries[index]
else
puts "Could not find an entry with the name #{target}"
end
elsif choice==3
print "Name: "
name = gets.chomp
print "Phone Number: "
number = gets.chomp
print "Address: "
address = gets.chomp
print "Email: "
email = gets.chomp
addNewEntry(name, phone, addr, email)
elsif choice==4
print "Enter a name to delete: "
target = gets.chomp
index = removeEntry(target)
if (index != -1)
puts "#{target} deleted!"
else
puts "Could not find an entry with the name #{target}"
end
elsif choice==5
exit()
else
puts "Invalid Choice! Please try again"
end
showMenu()
choice = gets.chomp
end
end
private
class Entry
def initilize(name, phone, addr, email)
@name = name
@phone = phone
@addr = addr
@email = email
end
def to_s
"Name: #{@name}\nPhone Number: #{@phone}\nAddress: #{@addr}\nEmail:
#{@email}"
end
def getName
@name
end
end
def initilize
@entries = []
end
def showMenu
puts "1: Show all entries."
puts "2: Find entry by name"
puts "3: Add a new Entry"
puts "4: Remove an Entry"
puts "5: Exit"
puts
end
def showAllEntries
@entries.each do |cntEntry|
puts cntEntry
puts
end
end
def FindEntryByName(target)
foundIndex = 0
@entries.each do |cntEntry|
return foundIndex if cntEntry.getName.downcase == target.downcase
foundIndex+=1
end
return -1
end
def addNewEntry(name, phone, addr, email)
newEntry = Entry.new(name, phone, addr, email)
entries.push(newEntry)
end
def removeEntry(target)
index = FindEntryByName(target)
return -1 if index == -1
@entries.delete_at(index)
1 #return 1 on success
end
end
myBook = PhoneBook.new
myBook.run