From a26f39a9f20f42108c774f287b51b17919ec5b78 Mon Sep 17 00:00:00 2001 From: selimssevgi Date: Mon, 7 Mar 2016 17:03:10 +0200 Subject: [PATCH 1/2] complete first assignment --- Gemfile.lock | 4 +--- address_book.rb | 36 ++++++++++++++++++++++++++++++++++-- main.rb | 2 ++ person.rb | 2 +- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3cbeae8..d8128af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: https://rubygems.org/ specs: minitest (5.8.1) @@ -7,6 +8,3 @@ PLATFORMS DEPENDENCIES minitest - -BUNDLED WITH - 1.11.2 diff --git a/address_book.rb b/address_book.rb index e5406d1..e192839 100644 --- a/address_book.rb +++ b/address_book.rb @@ -1,13 +1,45 @@ class AddressBook - def initialize(csv_path) + def initialize(csv_path) + @people = [] + populatePeopleArray(csv_path) end def print_people + @people.each do |person| + print_person person + end + end + def print_person person + print "#{person.id}," + print "#{person.full_name}," + print "#{person.phone_number}," + puts "#{person.city}" end def search_person(person_name) - + @people.each do |person| + if person.full_name.include?(person_name) + print_person person + end + end end + + private + def populatePeopleArray(csv_path) + fh = open(csv_path) + fh.readline # read the header + while (line = fh.gets) != nil + line.chomp! # remove new line character + infoArray = line.split(',') + person = Person.new + person.id = infoArray[0] + person.full_name = infoArray[1] + person.phone_number = infoArray[2] + person.city = infoArray[3] + @people.push person + end + end end + diff --git a/main.rb b/main.rb index 3996187..b71a6be 100644 --- a/main.rb +++ b/main.rb @@ -4,3 +4,5 @@ address_book = AddressBook.new("people.csv") address_book.print_people address_book.search_person("Michael") +# address_book.search_person("Lori") + diff --git a/person.rb b/person.rb index ab139ec..df819f3 100644 --- a/person.rb +++ b/person.rb @@ -1,3 +1,3 @@ class Person - + attr_accessor :id, :full_name, :phone_number, :city end From 9a7072f12741a8827e762ce5a26c645d699a3325 Mon Sep 17 00:00:00 2001 From: selimssevgi Date: Thu, 10 Mar 2016 22:14:22 +0200 Subject: [PATCH 2/2] refactoring --- address_book.rb | 16 +++++++++++++--- person.rb | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/address_book.rb b/address_book.rb index e192839..f5977e6 100644 --- a/address_book.rb +++ b/address_book.rb @@ -1,8 +1,10 @@ +require 'csv' + class AddressBook def initialize(csv_path) @people = [] - populatePeopleArray(csv_path) + populate_people_array(csv_path) end def print_people @@ -26,8 +28,16 @@ def search_person(person_name) end end - private - def populatePeopleArray(csv_path) + private + def populate_people_array(csv_path) + CSV.foreach(csv_path) do |row| + @people << Person.new(row[0], row[1], row[2], row[3]) + end + + @people.shift # removo the header + end + + def populate_people_array_ex(csv_path) fh = open(csv_path) fh.readline # read the header while (line = fh.gets) != nil diff --git a/person.rb b/person.rb index df819f3..b1eb364 100644 --- a/person.rb +++ b/person.rb @@ -1,3 +1,10 @@ class Person attr_accessor :id, :full_name, :phone_number, :city + + def initialize(id, full_name, phone_number, city) + @id = id + @full_name = full_name + @phone_number = phone_number + @city = city + end end