-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory12.rb
61 lines (54 loc) · 1.75 KB
/
directory12.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
def input_students
months = [:january, :february, :march, :april, :may, :june,
:july, :august, :september, :october, :november, :december]
puts "Please enter the names and details of the students"
puts "To finish, just hit return with the name field blank"
students = []
puts "New student name?"
name = gets[0..-2]
while !name.empty? do
puts "#{name}'s cohort group? (Leave blank if November)"
while true do
cohort = gets.chomp.downcase.to_sym
cohort = :november if cohort.empty?
months.include?(cohort) ? break : puts("Please enter a valid month:")
end
students << {name: name, cohort: cohort }
students.count == 1 ? puts( "Now we have #{students.count} student" ) : puts( "Now we have #{students.count} students" )
puts "New student name?"
name = gets[0..-2]
end
students
end
def print_header
puts "The students of Villains academy"
puts "-----------"
end
def print(students)
students.each_with_index { |student,index|
puts "#{index + 1}. #{student[:name]}, #{student[:cohort].capitalize} cohort".center(80)}
end
def printbycohort(students)
cohorts = []
students.each { |student|
cohorts << student[:cohort] unless cohorts.include?(student[:cohort])
}
cohorts.sort!
cohorts.each { |cohort|
puts "The students in the #{cohort.capitalize} cohort are as follows:"
students.each { |student|
puts "#{student[:name]}" if student[:cohort] == cohort
}
}
end
def print_footer(names)
if names.count == 0
puts "We have no students!"
else
names.count == 1? puts( "Overall we have #{names.count} great student" ) : puts( "Overall we have #{names.count} great students" )
end
end
students = input_students
print_header
printbycohort(students)
print_footer(students)