-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_library.rb
194 lines (169 loc) · 4.76 KB
/
dir_library.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#interactive menu
def interactive_menu
program_menu
looping_via_menu
end
def central_print(text,option,tag)
puts text.center(option,tag)
end
def string_is_number(option)
option.match(/\A[+-]?\d+?(_?\d+)*(\.\d+e?\d*)?\Z/) == nil ? 7 : option.to_i
end
def menu_option_output_phrase
puts @program_phrases["menu_option"]
string_is_number($stdin.gets.chomp)
end
def looping_via_menu
loop do
menu_choice(menu_option_output_phrase)
end
end
#menu choice
def menu_choice(choice)
case choice
when 1
try_load_students
when 2
save_students
when 3
input_student
when 4
puts find_student
when 5
print_students
when 6
students_statistic
when 7
program_menu
when 8
system("clear")
when 0
exit
else
program_menu
end
end
def text_centering(text,option)
print text.to_s.center(option,' ')
end
def menu_text_output
central_print("\n", CENTER_ONE_LINE, '-')
puts @program_phrases["menu_choises"]
central_print("\n", CENTER_ONE_LINE, '-')
end
# program menu selection text only
def program_menu
central_print("\n", CENTER_TWO_LINES, '-')
puts @program_phrases["menu_hint"]
menu_text_output
end
#header of the list of the students
def print_header
puts @program_phrases["students_list"]
puts @program_phrases["lines"]
text_centering("Name",20)
text_centering("Gender",5)
text_centering("Course",10)
puts "\n", @program_phrases["lines"]
end
#footer of the list of the students with the size of list
def print_footer(students = @students)
puts @program_phrases["lines"]
print students.size, @program_phrases["footer_student_show"],"\n"
puts @program_phrases["lines"]
end
#Get students from console and return
def input_student
puts @program_phrases["exit_loop"]
loop do
break if (name = input_student_loop("enter_name")) == '0'
break if (ssex = input_student_loop("enter_sex"))== '0'
break if (course = input_student_loop("enter_course")) == '0'
@students << {:name => name, :sex => ssex, :course => course.to_sym}
end
end
def input_student_loop(phrase_name)
puts @program_phrases[phrase_name]
$stdin.gets.chomp.capitalize
end
#saving students from console
def save_students
File.open(@filename, "w") do |file|
@students.each do |student|
csv_line = [student[:name], convert_gender(student[:sex]), student[:course]].join(",")
file.puts csv_line
end
end
puts @program_phrases["file_saved"]
end
#conver gender if its male -> 0, else 1
def convert_gender(input)
input.to_s.downcase.start_with?("m") ? 0 : 1
end
def student_output_line(student,iter)
text_centering("#{iter+1}: ", 3)
text_centering(student[:name],17)
text_centering(student[:sex],5)
text_centering(student[:course].to_sym,10)
puts
end
def looping_via_students(students)
students.each_with_index do |student, iter|
student_output_line(student,iter)
end
end
#print students to console
def print_students(students = @students)
print_header
looping_via_students(students)
print_footer(students)
end
def return_student_hash_value(student,criteria)
student.values.select { |val| return val if val.to_s == criteria }
return nil
end
#find a student
def find_student
puts @program_phrases["find_criteria"]
criteria = $stdin.gets.chomp.capitalize
print_students( @students.select { |student| return_student_hash_value(student,criteria) })
end
def load_students
students = []
file = File.open(@filename, "r")
file.readlines.each do |line|
name, ssex, course = line.chomp.split(',')
students << {:name => name.capitalize, :sex => man_or_woman(ssex), :course => course.to_sym}
end
file.close
@students = students
end
def man_or_woman(ssex)
return ssex == "0" ? "Man" : "Woman"
end
def man_or_woman_students_counter(gender)
@students.select{ |student| student[:sex] == gender}
end
def students_list_name_starts_A(letter)
@students.select{ |student| student[:name].start_with?(letter)}
end
#students statistic
def students_statistic
puts @program_phrases["lines"]
print @program_phrases["stat_title"], @students.length, "\n"
print @program_phrases["stat_man"], man_or_woman_students_counter("Man").length
print @program_phrases["stat_woman"], man_or_woman_students_counter("Woman").length
print @program_phrases["name_starts_A"], students_list_name_starts_A("A").length, "\n"
puts @program_phrases["lines"]
end
#reading students from the file
def try_load_students
if File.exists?(@filename) # if it exists
load_students
print_students(@students.sort_by{|name, sex, course| name[:name]})
puts @program_phrases["file_loaded"]
else # if it doesn't exist
puts @program_phrases["file_error"]
exit # quit the program
end
end