-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_database.rb
67 lines (58 loc) · 1.23 KB
/
contact_database.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
## TODO: Implement CSV reading/writing
require 'csv'
class ContactDatabase
def self.add(contact)
id = CSV.read("contact.csv")
id = id.length
id += 1
CSV.open("contact.csv", "ab") do |csv|
#id = csv.length += 1
csv << [id, contact.name, contact.email]
end
end
def self.read()
CSV.foreach("contact.csv") do |row|
puts row.join(" ")
end
# use row here...
end
def self.show(id)
test = false
CSV.foreach("contact.csv") do |row|
#puts row[0].to_i == id.to_i
if row[0].to_i == id.to_i
test = true
puts row
#elsif row[0].to_i != id.to_i
#puts "That contact cannot be found.
end
end
if test == false
puts "Contact cannot be found "
end
end
def self.find(contact)
test = false
CSV.foreach("contact.csv") do |row|
if row[1].match(contact.to_s)
test = true
puts row
end
end
if test == false
puts "cannot find contact"
end
end
def self.unique?(email)
test = true
CSV.foreach("contact.csv") do |row|
if row[2].match(email.to_s)
test = false
end
end
if test == false
puts "Contact already exists"
end
test
end
end