-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
145 lines (129 loc) · 4.23 KB
/
app.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
require 'json'
require_relative 'student'
require_relative 'teacher'
require_relative 'book'
require_relative 'rental'
require_relative 'classroom'
class App
def initialize
create_data
create_file_if_not_exists('./data/people.json')
create_file_if_not_exists('./data/books.json')
create_file_if_not_exists('./data/rentals.json')
load_data_from_files
end
def create_data
return if Dir.exist?('./data')
Dir.mkdir('./data')
end
def load_data_from_files
@people = load_json_file('./data/people.json', [])
@books = load_json_file('./data/books.json', [])
@rentals = load_json_file('./data/rentals.json', [])
end
def load_json_file(file_path, default_value)
file = File.open(file_path)
file_data = file.read
if file_data.empty?
default_value
else
JSON.parse(file_data)
end
end
def create_file_if_not_exists(file_path)
return if File.exist?(file_path)
File.open(file_path, 'w')
end
def list_books
@books.each do |book|
puts "Title: '#{book['title']}', Author: #{book['author']}"
end
end
def list_people
@people.each do |person|
if person['role'] == '[Teacher]'
puts "#{person['role']} ID: #{person['id']}, Name: #{person['name']}, Age: #{person['age']},
Specialization: #{person['specialization']}"
else
puts "#{person['role']} ID: #{person['id']}, Name: #{person['name']}, Age: #{person['age']},
Classroom: #{person['classroom']}, Parent Permission: #{person['permission']}"
end
end
end
def list_rentals_for_person(person_id)
filtered_rentals = @rentals.select { |obj| obj['id'] == person_id }
if filtered_rentals.length.positive?
filtered_rentals.each do |rental|
puts "Date: #{rental['date']}, Book: '#{rental['title']}' by #{rental['author']}"
end
else
puts 'No matching ID'
end
end
def create_student(age, classroom, name, permission)
classroom_name = Classroom.new(classroom)
student = Student.new(age, classroom_name, name, parent_permission: permission)
student_input = {
'role' => '[Student]',
'id' => student.id,
'name' => student.name,
'age' => student.age,
'classroom' => classroom_name.label,
'permission' => student.parent_permission
}
@people << student_input
File.write('./data/people.json', JSON.pretty_generate(@people))
end
def create_teacher(age, specialization, name)
teacher = Teacher.new(age, specialization, name)
teacher_input = {
'role' => '[Teacher]',
'id' => teacher.id,
'name' => teacher.name,
'age' => teacher.age,
'specialization' => teacher.specialization
}
@people << teacher_input
File.write('./data/people.json', JSON.pretty_generate(@people))
end
def create_book(title, author)
book = Book.new(title, author)
book_input = {
'title' => book.title,
'author' => book.author
}
@books << book_input
File.write('./data/books.json', JSON.pretty_generate(@books))
end
def list_books_with_index
puts 'Select a book from the following list by number:'
@books.each_with_index do |book, index|
puts "#{index}) Title: '#{book['title']}', Author: #{book['author']}"
end
end
def list_people_with_index
puts 'Select a person from the following list by number (not id):'
@people.each_with_index do |person, index|
if person['role'] == '[Teacher]'
puts "#{index}) #{person['role']} ID: #{person['id']}, Name: #{person['name']}, Age: #{person['age']},
Specialization: #{person['specialization']}"
else
puts "#{index}) #{person['role']} ID: #{person['id']}, Name: #{person['name']}, Age: #{person['age']},
Classroom: #{person['classroom']}, Parent Permission: #{person['permission']}"
end
end
end
def create_rental(date, person_index, book_index)
person = @people[person_index]
book = @books[book_index]
rental = Rental.new(date, person, book)
rental_input = {
'id' => rental.person['id'],
'title' => rental.book['title'],
'author' => rental.book['author'],
'date' => rental.date
}
@rentals << rental_input
File.write('./data/rentals.json', JSON.pretty_generate(@rentals))
end
end