-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory_typos.rb
125 lines (122 loc) · 3.84 KB
/
directory_typos.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
#method for checking if spelling of the month was correct
def spelling(month)
# array for the months
month_array =[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
#checking if the array includes our user unput, if yes returns true
month_array.include?(month)
end
#method for getting list of details from the user input into array
def input_students
puts "Please enter the name of the student"
puts "To finish, just hit return twice"
#create an empty array
students = []
#get the first name
name = gets.delete("\n").capitalize
#while the name is not empty, repeat this code
while !name.empty? do
puts "Please enter country of birth"
birth = gets.chomp
puts "Please enter the hobby"
hobby = gets.chomp
puts "Please enter the cohort"
cohort = gets.chomp.capitalize
#sets default month as a november if the user made no input
if cohort.empty?
cohort = "November"
#if not empty its gonna ask user to correct their spelling until it equals any string from our spelling method
else
until spelling(cohort) == true
puts "Please make sure you have a correct spelling for the cohort!"
cohort = gets.chomp.capitalize
end
end
#add the student hash to the array
students << {name: name, cohort: cohort, birth: birth, hobby: hobby}
#prints appropriative message for the student count
if students.count == 1
puts "Now we have #{students.count} student. Enter the next student or hit return to exit."
else
puts "Now we have #{students.count} students. Enter the next student or hit return to exit."
end
#get another name from the user
name = gets.chomp.capitalize
end
#return the array of students
students
end
#method for printing the header
def print_header
puts "The students of Villains Academy".center(85)
puts "-" * 100
end
#method for printing each student name from the array
def print(names)
names.each.with_index(1) do |name, index|
#if name[:name].start_with?("K") checks if string start with specific character, k in this case
#if name[:name].length < 12 checks length
puts "#{index}.#{name[:name].center(16)}| cohort: #{name[:cohort].center(10)}| Country of birth: #{name[:birth].center(15)}| hobby: #{name[:hobby].center(15)}"
#end
end
#the next part of code is the same as each loop but made with until,
#method print_footer should be deleted if using until.
#this block of code basically will print out our student info until the array will be empty,
#names.pop is what takes it from the array and returns to us
#until names.empty?
#name = names.pop
#puts "#{name[:name]} #{name[:cohort]} cohort"
#end
#puts "Overall, we have #{name.count} great students"
end
#method for printing total number of students
def print_footer(names)
puts "-" * 100
puts "Overall, we have #{names.count} great students".center(50)
end
#method to sort and print our students by the cohorts
def cohort_group(names)
#creating new hash for groups
cohort_hash = {}
puts "Cohort groups"
names.each do |student|
#assigning key value pairs for the new hash
cohort = student[:cohort]
name = student[:name]
#if key(cohort) is empty pushes new names in
if cohort_hash[cohort] == nil
cohort_hash[cohort] = [name]
else
cohort_hash[cohort].push(name)
end
end
#prints the results, key is cohort, value are names
cohort_hash.each do |key, value|
puts "-" * 50
puts "Cohort: #{key}"
puts value
end
end
#assigned variable to our input method
students = input_students
#nothing happens until we call the methods
if students.empty?
puts "No students found"
else
print_header
print(students)
print_footer(students)
cohort_group(students)
end