-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.rb
144 lines (127 loc) · 2.9 KB
/
directory.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
require "date"
@students = []
def input_students
while true
name = enter_name
if name.empty?
break
end
cohort = check_cohort
hobby = check_hobby
origin = check_origin
@students << {:name => name, :cohort => cohort, :hobby => hobby, :origin => origin}
p "Now we have #{@students.length} #{pluralise('student', @students.length)} with their details"
puts ""
end
@students
end
def enter_name
p "Please enter the student name or press Enter twice to return to menu"
STDIN.gets.chomp.capitalize
end
def check_cohort
loop do
p "What cohort are you in?"
@cohort = STDIN.gets.chomp
break if Date.parse(@cohort) rescue nil
end
Date.parse(@cohort).strftime(format="%B")
end
def check_hobby
p "What is your hobby"
STDIN.gets.chomp
end
def check_origin
p "What country were you born in?"
STDIN.gets.chomp
end
def print_header
p "The #{pluralise('student', @students.length)} of my cohort at Makers Academy with their hobbies"
p "------------------------"
end
def print_students_list
@students.sort_by!{|student| Date.parse(student[:cohort].to_s)}
@students.each do |student|
p "#{student[:name]} is in the #{student[:cohort]} cohort and enjoys #{student[:hobby]}. #{student[:name]} is from #{student[:origin]}."
end
end
def print_footer
p "Overall we have #{@students.length} mighty #{pluralise('student', @students.length)}"
puts ""
end
def pluralise(word, count)
if count == 1
return word
else
return word + "s"
end
end
def print_menu
puts "1. Input the students"
puts "2. Show the students"
puts "3. Save the list to students.csv"
puts "4. Load students from file"
puts "9. Exit"
end
def save_students
file = File.open("students.csv", "w")
@students.each do |student|
student_data = [student[:name], student[:cohort], student[:hobby],student[:origin]]
csv_line = student_data.join(",")
file.puts csv_line
end
file.close
end
def show_students
if @students.length != 0
print_header
print_students_list
print_footer
else
puts "No students to show. Please input some students"
puts ""
end
end
def load_students (filename="students.csv")
file=File.open(filename,"r")
file.readlines.each do |line|
name, cohort, hobby, origin = line.chomp.split(',')
@students << {:name => name,:cohort => cohort.to_sym, :hobby => hobby, :origin => origin}
end
file.close
end
def try_load_students
filename = ARGV.first
return load_students if filename.nil?
if File.exists?(filename)
load_students(filename)
puts "Loaded #{@students.length} from #{filename}"
else
puts "Sorry, #{filename} doesn't exist"
exit
end
end
def process(selection)
case selection
when "1"
input_students
when "2"
show_students
when "3"
save_students
when "4"
try_load_students
when "9"
exit
else
puts "Try again bud!"
end
end
def interactive_menu
loop do
print_menu
process(STDIN.gets.chomp)
puts ""
end
end
interactive_menu