Created
June 28, 2016 15:29
-
-
Save Leejojo/b6ce261498b9a9acce82073a63f5efbe to your computer and use it in GitHub Desktop.
Contact List App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
# Represents a person in an address book. | |
# The ContactList class will work with Contact objects instead of interacting with the CSV file directly | |
class Contact | |
attr_accessor :name, :email | |
# Creates a new contact object | |
# @param name [String] The contact's name | |
# @param email [String] The contact's email address | |
def initialize(name, email) | |
# TODO: Assign parameter values to instance variables. | |
@name = name | |
@email = email | |
end | |
# Provides functionality for managing contacts in the csv file. | |
class << self | |
# Opens 'contacts.csv' and creates a Contact object for each line in the file (aka each contact). | |
# @return [Array<Contact>] Array of Contact objects | |
def all | |
# TODO: Return an Array of Contact instances made from the data in 'contacts.csv'. | |
return_array = [] | |
CSV.foreach('contacts.csv') do |row| | |
return_array << Contact.new(row[0], row[1]) | |
end | |
return_array | |
end | |
# Creates a new contact, adding it to the csv file, returning the new contact. | |
# @param name [String] the new contact's name | |
# @param email [String] the contact's email | |
def create(name, email) | |
# TODO: Instantiate a Contact, add its data to the 'contacts.csv' file, and return it. | |
new_contact = Contact.new(name, email) | |
row = [new_contact.name, new_contact.email] | |
CSV.open("contacts.csv", "a+") do |person| | |
person << row | |
end | |
end | |
# Find the Contact in the 'contacts.csv' file with the matching id. | |
# @param id [Integer] the contact id | |
# @return [Contact, nil] the contact with the specified id. If no contact has the id, returns nil. | |
def find(id) | |
CSV.read('contacts.csv')[id] | |
end | |
# Search for contacts by either name or email. | |
# @param term [String] the name fragment or email fragment to search for | |
# @return [Array<Contact>] Array of Contact objects. | |
def search(search_term) | |
# TODO: Select the Contact instances from the 'contacts.csv' file whose name or email attributes contain the search term. | |
Contact.all.select do |contact| | |
contact.name.downcase.include?(search_term) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'contact' | |
# Interfaces between a user and their contact list. Reads from and writes to standard I/O. | |
class ContactList | |
# TODO: Implement user interaction. This should be the only file where you use `puts` and `gets`. | |
def run | |
show_options | |
input = gets.chomp.downcase | |
case input | |
when "list" | |
list_contacts | |
when "new" | |
new_contact | |
when "show" | |
show_contact | |
when "search" | |
search_contacts | |
end | |
end | |
private | |
def show_options | |
puts "Here is a list of available commands: Pick one" | |
puts " new - Create a new contact" | |
puts " list - List all contacts" | |
puts " show - Show a contact" | |
puts " search - Search contacts" | |
end | |
def list_contacts | |
row_num = 0 | |
Contact.all.each do |person| | |
row_num += 1 | |
puts "#{row_num}: #{person.name} (#{person.email})" | |
end | |
puts "---" | |
puts "#{row_num} records total" | |
end | |
def new_contact | |
puts "What is the new person's name (first, last)?" | |
new_name = gets.chomp | |
puts "What is the new person's email address?" | |
new_email = gets.chomp | |
Contact.create(new_name, new_email) | |
end | |
def show_contact | |
puts "Enter contact ID" | |
id = gets.chomp.to_i | |
contact = Contact.find(id) | |
if contact | |
puts contact.name + " " + contact.email | |
else | |
puts "not found" | |
end | |
end | |
def search_contacts | |
search_term = gets.chomp.downcase | |
contacts = Contact.search(search_term) | |
contacts.each do |contact| | |
puts contact.name + " " + contact.email | |
end | |
end | |
end | |
ContactList.new.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jane Doe | [email protected] | |
---|---|---|
John D | [email protected] | |
Ron Burgendy | [email protected] | |
joanna lee | [email protected] | |
isaac yoon | [email protected] | |
me you | [email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment