-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
277 lines (246 loc) · 6.88 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require './person'
require './book'
require './rental'
require './teacher'
require './student'
require 'json'
# rubocop:disable Metrics/ClassLength
class Library
def initialize
@rentals = []
@books = []
@people = []
load_data
load_reantals
end
# rubocop:disable Metrics/CyclomaticComplexity
def choose(input)
case input
when 1
list_books
when 2
list_people
when 3
create_person
when 4
create_book
when 5
create_rental
when 6
list_rentals
when 7
save_data
exit
else
puts 'Choose a number between 1 to 7'
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def load_data
if File.empty?('books.json')
puts 'List is empty'
else
books = JSON.parse(File.read('books.json'))
books.each do |book|
@books.push(Book.new(book['Title'], book['Author']))
end
end
if File.empty?('people.json')
puts 'List is empty'
else
people = JSON.parse(File.read('people.json'))
people.each do |person|
if person['Class'] == 'Student'
@people.push(Student.new(person['Age'], person['Name'], person['parent_permission']))
else
@people.push(Teacher.new(person['Age'], person['spec'], person['Name']))
end
end
end
end
# rubocop:enable Metrics/PerceivedComplexity
def load_reantals
if File.empty?('rentals.json') || !File.exist?('rentals.json')
puts 'List is empty'
else
rentals = JSON.parse(File.read('rentals.json'))
rentals.each do |rent|
@rentals.push(Rental.new(rent['date'],
@books.select { |book| book.title == rent['Book'] }[0],
@people.select { |person| person.name == rent['Name'] }[0]))
end
end
end
def list_books
puts 'Books list is empty, but you can add a book' if @books.empty?
@books.each { |book| puts "Title: '#{book.title}', Author: #{book.author}" }
end
def list_people
if @people.empty?
puts 'People list is empty'
return
end
@people.each do |person|
puts "[#{person.class.name}] ID: '#{person.id}', Name: #{person.name}, Age: #{person.age}"
end
end
def create_person
puts 'Do you want to create a student(1) or a teacher(2)? [input the number]'
ans = gets.chomp.to_i
puts 'Please input a number 1 for Student or 2 for Teacher.' if ans != 1 || ans != 2
while [1, 2].include?(ans)
case ans
when 1
create_student
break
when 2
create_teacher
break
end
end
end
# rubocop:disable Metrics/MethodLength
def create_student
print 'Age: '
age = gets.chomp.to_i
while age < 1
print 'Please enter a valid age: '
age = gets.chomp.to_i
end
print 'Name: '
name = gets.chomp.capitalize
while name == ''
print 'Please enter a name: '
name = gets.chomp.capitalize
end
print 'Has parent permission? (Y/N) '
permission = gets.chomp.upcase
case permission
when 'Y'
permission = true
when 'N'
permission = false
else
print 'Please enter Y for Yes or N for No: '
permission = gets.chomp.upcase
end
@people.push(Student.new(age, name, parent_permission: permission))
puts 'Person created successfully'
puts
end
# rubocop:enable Metrics/MethodLength
def create_teacher
print 'Age: '
age = gets.chomp.to_i
while age < 1
print 'Please enter a valid age: '
age = gets.chomp.to_i
end
print 'Name: '
name = gets.chomp.capitalize
while name == ''
print 'Please enter a name: '
name = gets.chomp.capitalize
end
print 'Specialization: '
specialization = gets.chomp
teacher = Teacher.new(age, specialization, name)
@people.push(teacher)
puts 'Person created successfully'
puts
end
def create_book
print 'Book title: '
title = gets.chomp.capitalize
while title == ''
print 'Book title: '
title = gets.chomp.capitalize
end
print 'Book author: '
author = gets.chomp.capitalize
while author == ''
print 'Book author: '
author = gets.chomp.capitalize
end
book = Book.new(title, author)
@books.push(book)
puts 'Book Created successfully'
puts
end
# rubocop:disable Metrics/MethodLength
def create_rental
if @books.empty?
puts 'Sorry there are no books avalibales'
return
end
puts 'Select a book from the follwoing list by number'
@books.each_with_index { |book, index| puts "#{index}) Title: #{book.title}, Author: #{book.author}" }
rented_book = gets.chomp.to_i
# rubocop:disable Lint/UnreachableLoop
while @books[rented_book].nil?
print 'Please select a book by index: '
rented_book = gets.chomp.to_i
break
end
# rubocop:enable Lint/UnreachableLoop
if @people.empty?
puts 'Sorry you have to create a person first'
return
else
puts 'Select a person from the follwoing list by number (not id)'
end
@people.each_with_index do |person, index|
puts "#{index}) [#{person.class}] - Name: #{person.name}, ID: #{person.id}, Age: #{person.age}"
end
renter = gets.chomp.to_i
# rubocop:disable Lint/UnreachableLoop
while @people[renter].nil?
print 'Please select a person by index: '
renter = gets.chomp.to_i
break
end
# rubocop:enable Lint/UnreachableLoop
print 'Date: [YYYY-MM-DD] '
date = gets.chomp
rent_info = Rental.new(date, @people[renter], @books[rented_book])
@rentals.push(rent_info)
puts 'Rental created successfully'
end
# rubocop:enable Metrics/MethodLength
def list_rentals
@people.each { |person| puts "[#{person.class}] ID: #{person.id}, Name: #{person.name}" }
if @rentals.empty?
puts 'There are no rentals'
return
end
print 'ID of person: '
person_id = gets.chomp.to_i
puts 'Rentals:'
@rentals.each do |rent|
puts "Date: #{rent.date}, Book: #{rent.book.title}, Author: #{rent.book.author}" if rent.person.id == person_id
end
end
def save_data
books = []
people = []
rents = []
@books.each do |book|
books.push({ Title: book.title, Author: book.author })
end
@people.each do |person|
if person.is_a? Student
people.push({ Class: person.class, classroom: person.classroom, Name: person.name, Age: person.age })
else
people.push({ Class: person.class, Name: person.name, Age: person.age })
end
end
@rentals.each do |rent|
rents.push({ date: rent.date, Book: rent.book.title, Name: rent.person.name })
end
File.write('books.json', JSON.generate(books))
File.write('people.json', JSON.generate(people))
File.write('rentals.json', JSON.generate(rents))
end
end
# rubocop:enable Metrics/ClassLength