-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
171 lines (151 loc) · 4.61 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
require_relative './student'
require_relative './teacher'
require_relative './book'
require_relative './rental'
require_relative './file_helper'
require 'json'
class App
attr_accessor :book_list, :people_list, :rental_list
def initialize
@book_list = []
@people_list = []
@rental_list = []
end
def display_books
@book_list.each_with_index do |book, index|
puts "(#{index}) Title: \"#{book.title}\", Author: #{book.author}"
end
end
def display_people
@people_list.each_with_index do |person, index|
puts "(#{index}) [#{person.type}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}"
end
end
def create_person
print 'Do you want to create a student (1) or a teacher (2)? [Input the number]: '
choice = gets.chomp
case choice
when '1'
create_student
when '2'
create_teacher
else
puts 'Invalid option'
nil
end
end
def create_student
print 'Age: '
student_age = gets.chomp
print 'Name: '
student_name = gets.chomp.capitalize
print 'Has parent permission? [y/n]: '
student_parent_permission = gets.chomp
has_permission = case student_parent_permission.downcase
when 'y'
true
else
false
end
print 'Classroom: '
student_classroom = gets.chomp
add_student(student_classroom, student_age, student_name, has_permission)
print "Student created successfully.\n"
end
def add_student(student_classroom, student_age, student_name, has_permission)
new_student = Student.new(student_classroom, student_age, student_name, has_permission)
@people_list << new_student
end
def create_teacher
print 'Age: '
teacher_age = gets.chomp
print 'Name: '
teacher_name = gets.chomp.capitalize
print 'Specialization: '
teacher_specialization = gets.chomp.capitalize
add_teacher(teacher_specialization, teacher_age, teacher_name, true)
print "Teacher created successfully.\n"
end
def add_teacher(teacher_specialization, teacher_age, teacher_name, has_permission)
new_teacher = Teacher.new(teacher_specialization, teacher_age, teacher_name, has_permission)
@people_list << new_teacher
end
def create_book
print 'Title: '
book_title = gets.chomp.capitalize
print 'Author: '
book_author = gets.chomp.capitalize
add_book(book_title, book_author)
print "Book created successfully.\n"
end
def add_book(book_title, book_author)
new_book = Book.new(book_title, book_author)
@book_list << new_book
end
def create_rental
puts 'Select a book from the following list by number'
display_books
rented_book = gets.chomp.capitalize
puts 'Select a person from the following list by number (not by id)'
display_people
renter = gets.chomp.capitalize
puts 'Date [yyyy/mm/dd]: '
date_of_rent = gets.chomp
add_rental(date_of_rent, rented_book, renter)
print "Rental created successfully.\n"
end
def add_rental(date_of_rent, rented_book, renter)
new_rental = Rental.new(date_of_rent, @book_list[rented_book.to_i], @people_list[renter.to_i])
@rental_list << new_rental
end
def display_rental
print 'ID of person: '
renter_id = gets.chomp
puts 'Rentals: '
list_rental_by_id(renter_id.to_i)
end
def list_rental_by_id(renter_id)
@rental_list.each do |rental|
if rental.person.id == renter_id
puts "Date: #{rental.date}, Book \"#{rental.book.title}\" by #{rental.book.author}"
end
end
end
def save_files
instance_variables.each do |var|
file_name = var.to_s.chomp('_list').delete('@')
ary = []
instance_variable_get(var).each do |obj|
hash = { ref: obj, value: to_hash(obj) }
ary << hash
end
File.write("./data/#{file_name}.json", JSON.generate(ary))
end
end
def read_files
instance_variables.each do |var|
file_name = var.to_s.chomp('_list').delete('@')
if File.exist?("./data/#{file_name}.json") && !File.empty?("./data/#{file_name}.json")
ary = JSON.parse(File.read("./data/#{file_name}.json"))
case file_name
when 'book'
read_book(ary)
when 'people'
read_people(ary)
else
read_rental(ary, File.read('./data/book.json'), File.read('./data/people.json'))
end
else
File.write("./data/#{file_name}.json", '[]')
end
end
end
private
def to_hash(obj)
hash = {}
obj.instance_variables.each do |var|
hash[var.to_s.delete('@')] = obj.instance_variable_get(var)
end
hash
end
end