Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odev-01 #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GEM
remote: https://rubygems.org/
specs:
minitest (5.8.1)

Expand All @@ -7,6 +8,3 @@ PLATFORMS

DEPENDENCIES
minitest

BUNDLED WITH
1.11.2
46 changes: 44 additions & 2 deletions address_book.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
require 'csv'

class AddressBook
def initialize(csv_path)

def initialize(csv_path)
@people = []
populate_people_array(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 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
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

2 changes: 2 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
address_book = AddressBook.new("people.csv")
address_book.print_people
address_book.search_person("Michael")
# address_book.search_person("Lori")

7 changes: 7 additions & 0 deletions person.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class Person
attr_accessor :id, :full_name, :phone_number, :city
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bu sinif icin constructor methodu kullanman object-oriented programlama icin daha uygun.

Ornek: http://stackoverflow.com/questions/8125946/class-constructors-in-ruby


def initialize(id, full_name, phone_number, city)
@id = id
@full_name = full_name
@phone_number = phone_number
@city = city
end
end