/
directory2.rb
executable file
·113 lines (102 loc) · 2.78 KB
/
directory2.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
$months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
@student_list = []
def get_students
puts "Hello! Let's make a directory of students"
puts "Please enter the student name."
name = gets.strip
while !name.empty?
puts "Which cohort is #{name} in?"
cohort = gets.strip.downcase
cohort = check_cohort cohort
puts "What is #{name}'s hobby?"
hobby = gets.strip
puts "What is #{name}'s country of birth?"
country = gets.strip
@student_list << {:name => name, :cohort => cohort,:hobby => hobby, :country => country}
if @student_list.length <= 1
puts "There is now #{@student_list.length} student in our directory. Add another student or Enter twice to exit"
else
puts "There are now #{@student_list.length} students in our directory. Add another student or Enter twice to exit"
end
name = gets.strip
end
save_students
@student_list
end
def check_cohort cohort_input
while !$months.include?(cohort_input)
puts 'That doesn\'t look like a month, please enter a month'
cohort_input = gets.strip.downcase
end
cohort_input.to_sym
end
def print_students
# student_list.sort_by! {|student| student[:cohort]}
$months.each do |month|
@student_list.each_with_index do |student,index|
puts "#{student[:name]} in the #{student[:cohort].capitalize} cohort comes from #{student[:country]} and enjoys #{student[:hobby]}" if student[:cohort]==month.to_sym
end
end
end
def print_header
puts "The students at Maker's Academy are:"
end
def print_footer
if @student_list.length <= 1
puts "There is #{@student_list.length} lovely student in our cohort!"
else
puts "There are #{@student_list.length} lovely students in our cohort!"
end
end
def print_menu
puts ""
puts 'Please choose from the following options:'
puts '1. Input the students'
puts '2. Show the students'
puts '9. Exit programme'
end
def show_students
if @student_list.length >0
print_header
puts ""
print_students
puts ""
print_footer
else
puts 'No students to show, try adding some students!'
end
end
def process(choice)
case choice
when '1'
#input students
@student_list = get_students
when '2'
#show students
show_students
when '9'
puts "Goodbye"
exit
else
puts "I don't know what you meant, please type a number to choose from our menu."
end #end case
end
def interactive_menu
puts 'Welcome to our directory.'
loop do
print_menu
process(gets.chomp)
end # end loop
end #end menu def
def save_students
#open the file for writing
file = File.open("students.csv","w")
@student_list.each do |student|
student_data = [student[:name], student[:cohort], student[:hobby], student[:country]]
csv_line = student_data.join(",")
file.puts csv_line
end
file.close
end
interactive_menu
save_students