Created
September 5, 2013 00:41
-
-
Save steven-ferguson/6444640 to your computer and use it in GitHub Desktop.
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
class Address | |
def initialize | |
@street | |
@city | |
@state | |
@type | |
end | |
def complete_address | |
"#{@street} #{@city}, #{@state}" | |
end | |
def edit_street(street) | |
@street = street | |
end | |
def edit_city(city) | |
@city = city | |
end | |
def edit_state(state) | |
@state = state | |
end | |
def edit_type(type) | |
@type = type | |
end | |
def street | |
@street | |
end | |
def city | |
@city | |
end | |
def state | |
@state | |
end | |
def type | |
@type | |
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
class Contact | |
def initialize(name) | |
@name = name | |
@phone_numbers = [] | |
@addresses = [] | |
@emails = [] | |
end | |
def phone_numbers | |
@phone_numbers | |
end | |
def addresses | |
@addresses | |
end | |
def emails | |
@emails | |
end | |
def name | |
@name | |
end | |
def add_number(phone_number) | |
@phone_numbers << phone_number | |
end | |
def add_address(address) | |
@addresses << address | |
end | |
def add_email(email) | |
@emails << email | |
end | |
def delete(type, index_of_content) | |
if type == "Email" | |
delete_email(@emails[index_of_content]) | |
elsif type == "Address" | |
delete_address(@addresses[index_of_content]) | |
else | |
delete_number(@phone_numbers[index_of_content]) | |
end | |
end | |
def delete_number(number_to_delete) | |
@phone_numbers.delete(number_to_delete) | |
end | |
def delete_email(email_to_delete) | |
@emails.delete(email_to_delete) | |
end | |
def delete_address(address_to_delete) | |
@addresses.delete(address_to_delete) | |
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 './lib/address_book' | |
require './lib/email' | |
require './lib/phone_number' | |
require './lib/address' | |
@address_book = [] | |
def contact_menu | |
user_options = {'a' => "add a contact", "v" => "view all contacts", 's' => 'select a contact and see details or edit', 'x' => 'to exit'} | |
user_options.each do |key, value| | |
puts "Enter '#{key}' to #{value}." | |
end | |
user_choice = gets.chomp | |
if user_choice == 'a' | |
add_contact | |
elsif user_choice == 'v' | |
view_all_contacts | |
elsif user_choice == 's' | |
select_contact | |
elsif user_choice == 'x' | |
puts "Good-bye!" | |
else | |
puts "That is not a valid option." | |
contact_menu | |
end | |
end | |
def add_contact | |
puts "Enter a name for your contact" | |
contact_name = gets.chomp | |
new_contact = Contact.new(contact_name) | |
@address_book << new_contact | |
puts "#{new_contact.name} has been created." | |
add_menu(new_contact) | |
end | |
def add_menu(contact) | |
user_options = {'n' => 'add a phone number', 'a' => 'add an address', 'e' => 'add an email', 'x' => 'exit to menu'} | |
user_options.each do |key, value| | |
puts "Press #{key} to #{value}." | |
end | |
user_choice = gets.chomp | |
if user_choice == 'n' | |
create_phone(contact) | |
elsif user_choice == 'a' | |
create_address(contact) | |
elsif user_choice == 'e' | |
create_email(contact) | |
elsif user_choice == 'x' | |
contact_menu | |
else | |
puts "That is nat a valid option" | |
end | |
end | |
def create_phone(contact) | |
puts "\nEnter the phone number." | |
phone = PhoneNumber.new(gets.chomp) | |
puts "\nWhat type of phone is this (work, mobile, etc.)" | |
phone.edit_type(gets.chomp) | |
contact.add_number(phone) | |
puts "#{contact.phone_numbers.last.number} has been added to #{contact.name}\n\n" | |
add_menu(contact) | |
end | |
def create_email(contact) | |
puts "\nEnter the email address" | |
email = Email.new(gets.chomp) | |
puts "\nWhat kind of email is this (work, personal, etc.)" | |
email.edit_type(gets.chomp) | |
contact.add_email(email) | |
puts "#{contact.emails.last.address} has been added to #{contact.name}\n\n" | |
add_menu(contact) | |
end | |
def create_address(contact) | |
address = Address.new | |
puts "\nEnter the street address" | |
address.edit_street(gets.chomp) | |
puts "\nEnter the city" | |
address.edit_city(gets.chomp) | |
puts "\nEnter the state" | |
address.edit_state(gets.chomp) | |
puts "\nEnter a type (work, home, etc)" | |
address.edit_type(gets.chomp) | |
contact.add_address(address) | |
puts "\n#{address.complete_address} added to #{contact.name}." | |
add_menu(contact) | |
end | |
def view_all_contacts | |
@address_book.each_with_index do |contact, index| | |
puts "#{index + 1}. #{contact.name}" | |
end | |
puts "\n\n" | |
end | |
def select_contact | |
view_all_contacts | |
puts "Enter the number of the contact you would like to select" | |
selected_contact_index = gets.to_i - 1 | |
selected_contact = @address_book[selected_contact_index] | |
puts "#{selected_contact.name} has been selected" | |
display_list("Address", selected_contact) | |
display_list("phone_number", selected_contact) | |
display_list("Email", selected_contact) | |
user_options = {'a' => 'add a new contact', 'd' => 'delete contact information', 'e' => 'edit contact details', 'x' => 'go back to main menu'} | |
user_options.each do |key, value| | |
puts "Press #{key} to #{value}." | |
end | |
user_choice = gets.chomp | |
if user_choice == 'a' | |
add_menu(selected_contact) | |
elsif user_choice == 'd' | |
delete_menu(selected_contact) | |
elsif user_choice == 'e' | |
edit_menu(selected_contact) | |
elsif user_choice == 'x' | |
contact_menu | |
else | |
puts "That is not a valid option" | |
end | |
end | |
def delete_menu(contact) | |
user_options = {'a' => 'delete an address', 'n' => 'delete a phone number', 'e' => 'delete an email'} | |
user_options.each do |key, value| | |
puts "Press #{key} to #{value}." | |
end | |
user_choice = gets.chomp | |
if user_choice == 'a' | |
delete_information("Address", contact) | |
elsif user_choice == 'n' | |
delete_information("Phone number", contact) | |
elsif user_choice == 'e' | |
delete_information("Email", contact) | |
end | |
end | |
def edit_menu(contact) | |
user_options = {'a' => 'edit an address', 'n' => 'edit a phone number', 'e' => 'edit an email'} | |
user_options.each do |key, value| | |
puts "Press #{key} to #{value}." | |
end | |
user_choice = gets.chomp | |
if user_choice == 'a' | |
address_edit(contact) | |
elsif user_choice == 'n' | |
phone_edit(contact) | |
elsif user_choice == 'e' | |
email_edit(contact) | |
end | |
end | |
def delete_information(information_type, contact) | |
display_list(information_type, contact) | |
puts "Select the #{information_type} to delete" | |
selected_index = gets.to_i - 1 | |
contact.delete(information_type, selected_index) | |
puts "#{information_type} deleted successfully." | |
contact_menu | |
end | |
def address_edit(contact) | |
display_list("Address", contact) | |
puts "Select an address to edit" | |
selected_index = gets.to_i - 1 | |
selected_address = contact.addresses[selected_index] | |
puts "\n#{selected_address.complete_address} has been selected." | |
user_options = {1 => "street", 2 => "city", 3 => "state", 4 => "type"} | |
user_options.each do |key, value| | |
puts "Enter #{key} to edit the #{value}." | |
end | |
puts "Enter 5 to go back." | |
user_choice = gets.to_i | |
if user_choice == 1 | |
puts "\n Enter the new street" | |
selected_address.edit_street(gets.chomp) | |
puts "Street has been changed to #{selected_address.street}." | |
contact_menu | |
elsif user_choice == 2 | |
puts "\nEnter the new city" | |
selected_address.edit_city(gets.chomp) | |
puts "\n\nCity has been changed to #{selected_address.city}." | |
contact_menu | |
elsif user_choice == 3 | |
puts "\nEnter the new state" | |
selected_address.edit_state(gets.chomp) | |
puts "\n\nState has been changed to #{selected_address.state}." | |
contact_menu | |
elsif user_choice == 4 | |
puts "\nEnter the type" | |
selected_address.edit_type(gets.chomp) | |
puts "\n\nType has been changed to #{selected_address.type}." | |
contact_menu | |
elsif user_choice == 5 | |
edit_menu(contact) | |
else | |
puts "That is not a valid option." | |
address_edit(contact) | |
end | |
end | |
def email_edit(contact) | |
display_list("Email", contact) | |
puts "Select the email to edit" | |
selected_index = gets.to_i - 1 | |
puts "\n#{contact.emails[selected_index].address} has been selected." | |
puts "Enter 1 to edit the email address." | |
puts "Enter 2 to edit the type." | |
puts "Enter 3 to go back." | |
user_choice = gets.to_i | |
if user_choice == 1 | |
puts "\nEnter the new address" | |
contact.emails[selected_index].edit_address(gets.chomp) | |
puts "\n\nAddress changed to #{contact.emails[selected_index].address}." | |
contact_menu | |
elsif user_choice == 2 | |
puts "\nEnter a type for the email" | |
contact.emails[selected_index].edit_type(gets.chomp) | |
puts "\n\nType changed to #{contact.emails[selected_index].type}." | |
contact_menu | |
elsif user_choice == 3 | |
edit_menu(contact) | |
else | |
puts "That is not a valid input." | |
email_edit(contact) | |
end | |
end | |
def phone_edit(contact) | |
display_list("Phone", contact) | |
puts "Select a Phone Number to edit" | |
selected_index = gets.to_i - 1 | |
puts "\n#{contact.phone_numbers[selected_index].number} has been selected." | |
puts "Enter 1 to edit number." | |
puts "Enter 2 to edit type" | |
puts "Enter 3 to go back" | |
user_choice = gets.to_i | |
if user_choice == 1 | |
puts "\nEnter new Number" | |
contact.phone_numbers[selected_index].edit_number(gets.chomp) | |
puts "\n\nNumber has been changed to #{contact.phone_numbers[selected_index].number}." | |
contact_menu | |
elsif user_choice == 2 | |
puts "\nEnter a new type for the phone" | |
contact.phone_numbers[selected_index].edit_type(gets.chomp) | |
puts "\n\nType has been changed to #{contact.phone_numbers[selected_index].type}." | |
contact_menu | |
elsif user_choice == 3 | |
edit_menu(contact) | |
else | |
puts "That is not a valid input." | |
phone_edit(contact) | |
end | |
end | |
def display_list(information_type, contact) | |
if information_type == "Address" | |
puts "Address(es):" | |
contact.addresses.each_with_index do |address, index| | |
puts "#{index + 1}: #{address.complete_address} (#{address.type})" | |
end | |
elsif information_type == "Email" | |
puts "Email Address(es):" | |
contact.emails.each_with_index do |email, index| | |
puts "#{index + 1}. #{email.address} (#{email.type})" | |
end | |
else | |
puts "Phone Number(s):" | |
contact.phone_numbers.each_with_index do |phone, index| | |
puts "#{index + 1}: #{phone.number} (#{phone.type})" | |
end | |
end | |
end | |
contact_menu |
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 'rspec' | |
require 'address_book' | |
require 'email' | |
require 'phone_number' | |
require 'address' | |
describe Contact do | |
it "is initialized with a name" do | |
test_contact = Contact.new("test") | |
test_contact.should be_an_instance_of Contact | |
end | |
it "has an array of phone numbers" do | |
test_contact = Contact.new("name") | |
test_contact.phone_numbers.should eq [] | |
end | |
it "has an array of addresses" do | |
test_contact = Contact.new("name") | |
test_contact.addresses.should eq [] | |
end | |
it "has an array of email addresses" do | |
test_contact = Contact.new("name") | |
test_contact.emails.should eq [] | |
end | |
it "it lets you add a phone number" do | |
test_contact = Contact.new("test") | |
phone = PhoneNumber.new("8675309") | |
test_contact.add_number(phone) | |
test_contact.phone_numbers.should eq [phone] | |
end | |
it "lets you add an email address" do | |
test_contact = Contact.new("test") | |
email = Email.new("[email protected]") | |
test_contact.add_email(email) | |
test_contact.emails.should eq [email] | |
end | |
it "adds an address to an array" do | |
test_contact = Contact.new("test") | |
address = Address.new | |
address.edit_street("123 place") | |
test_contact.add_address(address) | |
test_contact.addresses.should eq [address] | |
end | |
describe "create_address" do | |
it "takes in a street, city, and state and combines them into a single address and adds the address to the addresses" do | |
test_contact = Contact.new("test") | |
test_contact.create_address("123 South St", "Portland", "OR").should eq "123 South St, Portland, OR" | |
end | |
end | |
it "lets you delete information" do | |
test_contact = Contact.new("test") | |
test_contact.add_email("[email protected]") | |
test_contact.delete('Email', 0) | |
test_contact.emails.should eq [] | |
end | |
it "lets you delete a contact phone number" do | |
test_contact = Contact.new("test") | |
phone = PhoneNumber.new("123456") | |
test_contact.add_number(phone) | |
test_contact.delete_number(phone) | |
test_contact.phone_numbers.should eq [] | |
end | |
it "lets you delete an email address" do | |
test_contact = Contact.new("test") | |
email = Email.new("[email protected]") | |
test_contact.add_email(email) | |
test_contact.delete_email(email) | |
test_contact.emails.should eq [] | |
end | |
it "lets you delete an address" do | |
test_contact = Contact.new("test") | |
address = Address.new | |
address.edit_street("123 place") | |
test_contact.add_address(address) | |
test_contact.delete_address(address) | |
test_contact.addresses.should eq [] | |
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 'rspec' | |
require 'address' | |
describe Address do | |
it "has a street" do | |
address = Address.new | |
address.edit_street("123 South St") | |
address.street.should eq "123 South St" | |
end | |
it "has a city" do | |
address = Address.new | |
address.edit_city("Portland") | |
address.city.should eq "Portland" | |
end | |
it "has a state" do | |
address = Address.new | |
address.edit_state("Texas") | |
address.state.should eq "Texas" | |
end | |
it "has a type" do | |
address = Address.new | |
address.edit_type("work") | |
address.type.should eq "work" | |
end | |
it "has a complete address" do | |
address = Address.new | |
address.edit_street("123 South St") | |
address.edit_city("Portland") | |
address.edit_state("OR") | |
address.complete_address.should eq "123 South St Portland, OR" | |
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
class Email | |
def initialize(address) | |
@address = address | |
end | |
def address | |
@address | |
end | |
def type | |
@type | |
end | |
def edit_type(type) | |
@type = type | |
end | |
def edit_address(new_address) | |
@address = new_address | |
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 'rspec' | |
require 'email' | |
describe Email do | |
it 'intitializes with an address' do | |
email = Email.new("[email protected]") | |
email.should be_an_instance_of Email | |
end | |
it 'has an address' do | |
email = Email.new("[email protected]") | |
email.address.should eq "[email protected]" | |
end | |
it 'has a type' do | |
email = Email.new("[email protected]") | |
email.edit_type("work") | |
email.type.should eq "work" | |
end | |
it 'can have the address edited' do | |
email = Email.new("[email protected]") | |
email.edit_address("[email protected]") | |
email.address.should eq "[email protected]" | |
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
class PhoneNumber | |
def initialize(number) | |
@number = number | |
end | |
def number | |
@number | |
end | |
def type | |
@type | |
end | |
def edit_number(new_number) | |
@number = new_number | |
end | |
def edit_type(type) | |
@type = type | |
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 'rspec' | |
require 'phone_number' | |
describe PhoneNumber do | |
it "is initialized with a number" do | |
phone = PhoneNumber.new("1232345") | |
phone.should be_an_instance_of PhoneNumber | |
end | |
it "has a phone number" do | |
phone = PhoneNumber.new("1234567") | |
phone.number.should eq "1234567" | |
end | |
it "can be edited" do | |
phone = PhoneNumber.new("1234567") | |
phone.edit_number("456789") | |
phone.number.should eq "456789" | |
end | |
it "has a type (work, home, mobile, etc)" do | |
phone = PhoneNumber.new("11111111") | |
phone.edit_type("work") | |
phone.type.should eq "work" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment