-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive_directory.rb
144 lines (125 loc) · 3.19 KB
/
interactive_directory.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
@students = []
def month
year = [:January, :February, :March, :April, :May, :June, :July, :August,:September, :October, :November, :December, :Future]
puts "What month cohort are they in?"
cohort = gets.chomp.capitalize.to_sym
while !year.include?(cohort)
puts "Please try again, or type Future for future cohorts"
cohort = gets.chomp.capitalize.to_sym
end
return cohort
end
def input_students
puts "Please enter the names of the students"
puts "To finish, just hit return twice"
name = STDIN.gets.chomp
while !name.empty? do
@students << {name: name, cohort: month}
puts "Now we have #{@students.count} students"
name = gets.chomp
end
@students
end
def print_header
puts "The Students of Villains Academy".center(80)
puts "-------------".center(80)
end
def print_students
@students.each_with_index do |student, index|
if student[:name].length < 12
puts "#{index+1}: #{student[:name]} (#{student[:cohort]} cohort)"
end
end
end
def print_footer
if @students.count == 1
puts "Overall, we have 1 great student\n"
else
puts "Overall, we have #{@students.count} great students\n"
end
end
def print_menu
puts "Menu"
puts "1. Input the students"
puts "2. Show the students"
puts "3. Save list of students"
puts "4. Load list of students"
puts "9. Exit"
end
def show_students
print_header
print_students
print_footer
end
def select_filename
puts "Which file would you like to load the list of students from?"
puts "Leave blank for the default (students.csv)"
puts "Options:"
puts Dir.entries("./directories/")
return standard_filename
end
def standard_filename
filename = "./directories/" + STDIN.gets.chomp
if filename == "./directories/"
filename = "./directories/students.csv"
end
return filename
end
def save_students
puts "Which file would you like to save to?"
puts "Leave blank to save to default (students.csv)"
File.open(standard_filename, "w") do |file| # open the file for writing
@students.each do |student|
student_data = [student[:name], student[:cohort]]
csv_line = student_data.join(",")
file.puts csv_line
end
end
puts "Saved"
end
def load_students(filename = "./directories/students.csv")
File.open(filename, "r") do |file|
file.readlines.each do |line|
name, cohort = line.chomp.split(",")
@students << {name: name, cohort: cohort.to_sym}
end
end
puts "Loaded #{@students.count} students from #{filename}"
end
def try_load_students
filename = ARGV.first # first argument from the command line
if filename.nil? # load students.csv if no other file provided
filename = "./directories/students.csv"
end
if File.exists?(filename)
load_students(filename)
else # if it doesn't exist
puts "Sorry, #{filename} doesn\'t exist"
exit
end
end
def process(selection)
case selection
when "1"
input_students
when "2"
show_students
when "3"
save_students
when "4"
load_students(select_filename)
when "9"
puts "Goodbye"
exit
else
puts "I don't know what you mean, try again"
end
end
def interactive_menu
loop do
print_menu
process(STDIN.gets.chomp)
end
end
try_load_students
interactive_menu