-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory_test.rb
More file actions
65 lines (59 loc) · 1.9 KB
/
directory_test.rb
File metadata and controls
65 lines (59 loc) · 1.9 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
students = [
{name: "Dr. Hannibal Lecter", cohort: :november},
{name: "Darth Vader", cohort: :november},
{name: "Nurse Ratched", cohort: :november},
{name: "Michael Corleone", cohort: :november},
{name: "Alex DeLarge", cohort: :november},
{name: "The Wicked Witch of the West", cohort: :november},
{name: "Terminator", cohort: :november},
{name: "Freddy Krueger", cohort: :november},
{name: "The Joker", cohort: :november},
{name: "Joffrey Baratheon", cohort: :november},
{name: "Norman Bates", cohort: :november}
]
def input_students
puts "Please enter the name and cohort of each student, separated by a comma"
puts "To finish, just hit return twice"
students = []
loop do
input = gets.chomp
break if input.empty?
name, cohort = input.split(",").map(&:strip)
cohort = cohort.to_sym
if cohort.empty?
cohort = :november
end
while true
case cohort
when :january, :february, :march, :april, :may, :june, :july, :august, :september, :october, :november, :december
students << { name: name, cohort: cohort }
puts "Now we have #{students.count} #{students.count == 1 ? 'student' : 'students'}"
break
else
puts "Invalid cohort. Please enter a valid cohort."
cohort = gets.chomp.to_sym
end
end
end
students
end
def print_header
puts "The students of Villians Academy".center(50)
puts "-----------------".center(50)
end
def print(students)
cohorts = students.map { |student| student[:cohort] }.uniq
cohorts.each do |cohort|
puts "Students in the #{cohort} cohort:"
students.select { |student| student[:cohort] == cohort }.each_with_index do |student, index|
puts "#{index + 1}. #{student[:name]}"
end
end
end
def print_footer(students)
puts "Overall, we have #{students.count} great students".center(50)
end
students = input_students
print_header
print(students)
print_footer(students)