forked from chaddcw/ContactManager-Example-Ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContactDataStore.rb
More file actions
146 lines (96 loc) · 3.55 KB
/
ContactDataStore.rb
File metadata and controls
146 lines (96 loc) · 3.55 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
##############################################################################
# File Name: ContactDataStore.rb
# Author: chadd williams
# Date: 4/3/2014
# Class: CS 360 Fall 2014
# Assignment: Introduction to Ruby and Git
# Purpose: Provide a class containing an array of ContactEntries
##############################################################################
require "ContactEntry.rb"
class ContactDataStore
############################################################################
# Function: initialize
# Description: initialize data in the class
# Paramaeters: none
# Returned: nothing
############################################################################
def initialize
@dataArray = Array.new
puts "initialize"
end
############################################################################
# Function: deleteData
# Description: delete all ContactEntry data in the class
# Paramaeters: none
# Returned: nothing
############################################################################
def deleteData
@dataArray = @dataArray.drop(@dataArray.length)
end
############################################################################
# Function: readData
# Description: read data from the contacts file into the dataArray
# Paramaeters: filename: the file to open
# Returned: nothing
############################################################################
def readData(filename)
@dataArray = Array.new
file = File.open(filename)
file.each_line do |line|
array = line.split(/,/)
entry = Struct::ContactEntry.new(array[0], array[1], array[2])
@dataArray.push(entry)
end
file.close
end
############################################################################
# Function: writeData
# Description: write data to the contacts file from the dataArray
# Paramaeters: filename: the file to open
# Returned: nothing
############################################################################
def writeData(filename = "out.csv")
file = File.new(filename, "w")
@dataArray.each do |singleEntry|
file.puts "#{singleEntry[0]},#{singleEntry[1]},#{singleEntry[2]}"
end
file.close
end
def sortData
end
############################################################################
# Function: addContact
# Description: add a ContactEntry to the array
# Paramaeters: fname: the first name of the entry
# lname: the last name of the entry
# email: the email address of the entry
# Returned: nothing
############################################################################
def addContact(fname,lname,email)
@dataArray.push(Struct::ContactEntry.new(fname,lname,email))
end
def deleteContact
end
def searchData
end
############################################################################
# Function: visitEachContact
# Description: invoke the method func on each entry in the array
# Paramaeters: func: the method to invoke
# Returned: nothing
############################################################################
def visitEachContact(func)
@dataArray.each do |singleEntry|
func.call(singleEntry)
end
end
############################################################################
# Function: printToScreen
# Description: print each ContactEntry to the screen
# Paramaeters: none
# Returned: nothing
############################################################################
def printToScreen
visitEachContact(method ( :printContactEntry))
end
end