-
Notifications
You must be signed in to change notification settings - Fork 1
/
directory3-with-CSV-library.rb
157 lines (130 loc) · 3.78 KB
/
directory3-with-CSV-library.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
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
146
147
148
149
150
151
152
153
154
155
156
157
require 'csv'
@students = []
@months = ["January","February","March","April","May","June","July","August","September","October","November","December"]
@filename = ARGV.first
def check_cohort cohort
while [email protected]?(cohort.capitalize) # is in array
puts "That doesn't look like a month. Try again."
cohort = STDIN.gets.chomp
end
cohort.capitalize
end
def lonely_student
@students.length == 1 ? "student" : "students"
end
def sort_by_cohort # ahhh sort out later what even is this
used_cohorts = @students.map do |student|
student[:cohort].uniq
end
sorted_students = used_cohorts.map do |month|
# @students.
end
# month_array = @students.map { |student| student[:cohort] if student[:cohort] == month }
end
def update_students(name, cohort, hobby, country)
@students << {:name => name, :cohort => cohort.to_sym, :hobby => hobby, :country => country}
end
def input_students
puts "Please enter a name"
name = STDIN.gets.chomp
while !name.empty? do
puts "What cohort is #{name} in?"
cohort = STDIN.gets.chomp
cohort = check_cohort cohort # check if is actually a month
puts "What is one of #{name}'s hobbies?"
hobby = STDIN.gets.chomp
puts "Where does #{name} come from?"
country = STDIN.gets.chomp
update_students(name, cohort, hobby, country)
puts "Your list now contains #{@students.length} #{lonely_student}."
puts "Please enter another name, or press enter twice to exit"
name = STDIN.gets.chomp
end # goes back to menu
end
def show_students
if @students.length > 0
puts "This is the Makers Academy #{lonely_student} list"
puts "-------------------------------------------"
@students.each.with_index(1) do |student, index|
puts "#{index}. #{student[:name]} from the #{student[:cohort]} cohort likes #{student[:hobby]} and is from #{student[:country]}."
end
puts "-------------------------------------------"
puts "Makers Academy has #{@students.length} #{lonely_student}"
else
puts "No students found. Please add or load some students!"
end
end
def check_file_exists
puts "Our student list would like to work with a file to save or load our data."
while !File.exists?(@filename) do
puts "#{@filename} doesn't exist, do you want to create it? (y/n)"
input = STDIN.gets.chomp
case input
when "y"
#create file
File.open(@filename, 'w') {|new_file|}
puts "Successfully creates #{@filename}"
when "n"
#exit
puts "Try specifying a different file:"
@filename = STDIN.gets.chomp
else
puts "Please enter 'y' or 'n' to indicate your choice."
end
puts "Checking #{@filename} exists...."
end
@filename
end
def save_students
CSV.open(@filename, 'wb') do |file|
@students.each do |student|
file << [student[:name], student[:cohort], student[:hobby], student[:country]]
end
end
puts "Successfully saved to #{@filename}!"
end
def load_students
CSV.foreach(@filename) do |student|
update_students(student[0], student[1], student[2], student[3])
end
puts "\nSuccessfully updated the student list from #{@filename}!"
end
def try_load_students
@filename = "students.csv" if @filename.nil?
@filename = check_file_exists
load_students
puts "Successfully loaded #{@students.length} #{lonely_student} from #{@filename}!\n"
end
def print_menu
puts "\n1. Input the students"
puts "2. Show the students"
puts "3. Save the list to students.csv"
puts "4. Load the list from students.csv"
puts "9. Exit\n" # 9 because we'll be adding more items
end
def process selection
case selection
when "1"
input_students
when "2"
show_students
when "3"
save_students
when "4"
load_students
when "9"
puts "Cheerio!"
exit
else
"Please enter a valid option"
end
end
def interactive_menu
puts "Welcome!"
loop do
print_menu
process STDIN.gets.chomp
end
end
try_load_students # Was a @filename passed?
interactive_menu