-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteacher.rb
More file actions
61 lines (47 loc) · 1.43 KB
/
teacher.rb
File metadata and controls
61 lines (47 loc) · 1.43 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
require_relative 'student'
class Teacher < Person
attr_accessor :specialization
def initialize(specialization, age, name = 'Unknown', parent_permission = true)
super(age, name, parent_permission)
@specialization = specialization
end
def can_use_services?
true
end
def to_s
"#{super} [Teacher] Name: #{name}, ID: #{id}, Age: #{age}"
end
def self.create_teacher
puts
puts 'Age:'
age = gets.chomp.to_i
puts 'Name:'
name = gets.chomp
puts 'Specialization:'
specialization = gets.chomp
teacher = new(specialization, age, name)
puts
puts 'Person created successfully'
puts teacher
teacher.save
end
def save
if File.exist?('teachers.json')
teachers_file = File.read('teachers.json')
teachers = JSON.parse(teachers_file)
teachers << { age: age, name: name, specialization: specialization }
File.write('teachers.json', JSON.pretty_generate(teachers))
else
File.write('teachers.json',
JSON.pretty_generate([{ age: age, name: name, specialization: specialization }]))
end
end
def self.load_teachers
return unless File.exist?('teachers.json')
teachers_file = File.read('teachers.json')
teachers = JSON.parse(teachers_file)
teachers.each do |teacher|
new(teacher['age'], teacher['name'], teacher['specialization'])
end
end
end