-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.1.rb
71 lines (63 loc) · 1.89 KB
/
directory.1.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
def input_students
puts "Please enter the names of the students"
puts "To finish, just hit return twice"
# create an empty array
students = []
# get the first name
name = gets.delete_suffix("\n")
# while the name is not empty, repeat this code
while !name.empty? do
# Prompt the user for additional student information
puts "Please enter the student's cohort:"
cohort = gets.delete_suffix("\n").to_sym
puts "Please enter the student's hobbies:"
hobbies = gets.delete_suffix("\n")
puts "Please enter the student's country of birth:"
country = gets.delete_suffix("\n")
puts "Please enter the student's height (in cm):"
height = gets.delete_suffix("\n").to_i
# Create a new student hash and add it to the array
students << {
name: name,
cohort: cohort,
hobbies: hobbies,
country: country,
height: height
}
if students.count == 1
puts "Now we have 1 student"
else
puts "Now we have #{students.count} students"
end
# get another name from the user
name = gets.delete_suffix("\n")
end
# return the array of students
students
end
def print_header
puts "The students of Villains Academy".center(50)
puts "-------------".center(50)
end
def print(students)
cohorts = students.map { |student| student[:cohort] }.uniq
cohorts.each do |cohort|
puts "#{cohort.capitalize} cohort".center(50)
students.each_with_index do |student, index|
if student[:cohort] == cohort
puts "#{index + 1}. #{student[:name]}, #{student[:hobbies]}, #{student[:country]}, #{student[:height]}cm".center(50)
end
end
end
end
def print_footer(students)
if students.count == 1
puts "Overall, we have 1 great student".center(50)
else
puts "Overall, we have #{students.count} great students".center(50)
end
end
students = input_students
print_header
print(students)
print_footer(students)