-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_saver.rb
181 lines (170 loc) · 5.21 KB
/
file_saver.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
class FileSaver
def initialize(books, people, rentals)
@books = books
@people = people
@rentals = rentals
end
def save_books
serialized_books = @books.map do |book|
{
title: book.title,
author: book.author
}
end
file_content = serialized_books.map { |book| JSON.generate(book) }.join(",\n")
file = File.open('books.json', 'w')
file.puts("[\n#{file_content}\n]")
file.close
end
def save_people
serialized_people = @people.map do |person|
attributes = {
name: person.name,
age: person.age,
id: person.id
}
if person.is_a? Student
attributes[:parent_permission] = person.parent_permission
elsif person.is_a? Teacher
attributes[:specialization] = person.specialization
end
attributes
end
file_content = serialized_people.map { |person| JSON.generate(person) }.join(",\n")
file = File.open('people.json', 'w')
file.puts("[\n#{file_content}\n]")
file.close
end
def save_rentals
serialized_rentals = @rentals.map do |rental|
{
date: rental.date,
book: rental.book.title,
person: rental.person.name
}
end
file_content = serialized_rentals.map { |rental| JSON.generate(rental) }.join(",\n")
file = File.open('rentals.json', 'w')
file.puts("[\n#{file_content}\n]")
file.close
end
# def save_books
# serialized_books = @books.map do |book|
# {
# title: book.title,
# author: book.author
# }
# end
# file_content = "[\n" + serialized_books.map { |book| JSON.generate(book) }.join(",\n") + "\n]"
# file = File.open('books.json', 'w')
# file.puts(file_content)
# file.close
# end
# def save_people
# serialized_people = @people.map do |person|
# attributes = {
# name: person.name,
# age: person.age,
# id: person.id
# }
# if person.is_a? Student
# attributes[:parent_permission] = person.parent_permission
# elsif person.is_a? Teacher
# attributes[:specialization] = person.specialization
# end
# attributes
# end
# file_content = "[\n" + serialized_people.map { |person| JSON.generate(person) }.join(",\n") + "\n]"
# file = File.open('people.json', 'w')
# file.puts(file_content)
# file.close
# end
# def save_rentals
# serialized_rentals = @rentals.map do |rental|
# {
# date: rental.date,
# book: rental.book.title,
# person: rental.person.name
# }
# end
# file_content = "[\n" + serialized_rentals.map { |rental| JSON.generate(rental) }.join(",\n") + "\n]"
# file = File.open('rentals.json', 'w')
# file.puts(file_content)
# file.close
# end
def list_people_ids
if @people.empty?
puts 'No people found'
else
@people.each do |person|
name = person.name
age = person.age
id = person.id
grp = person.class
puts "[#{grp}] - Name: #{name}, ID: #{id}, Age: #{age}]"
end
end
end
def list_all_books
if @books.empty?
puts 'No book found'
else
puts '----------------------------------------------'
puts '---------list of books are as follows:---------'
@books.each_with_index do |book, index|
puts "#{index}) - Title: #{book.title}, Author: #{book.author}"
end
end
end
def list_all_people
if @people.empty?
puts 'No people found'
else
puts '----------------------------------------------'
puts '-------List of people are as follows:---------'
@people.each_with_index do |person, index|
if person.is_a?(Student)
type = 'Student'
info = "Name: #{person.name}, Age: #{person.age}, ID: #{person.id}, " \
"Parent Permission: #{person.parent_permission}"
elsif person.is_a?(Teacher)
type = 'Teacher'
info = "Name: #{person.name}, Age: #{person.age}, ID: #{person.id}, " \
"Specialization: #{person.specialization}"
end
puts "[#{type}] - #{index}) - #{info}"
end
end
end
def list_all_rentals
if @rentals.empty?
puts 'No rentals found'
else
puts '----------------------------------------------'
puts '---------list of rentals are as follows:---------'
@rentals.each_with_index do |rental, index|
if rental
puts "#{index}) - Borrowed Date: #{rental.date}, Book borrowed: #{rental.book.title}, " \
"Borrower: #{rental.person.name}, Designation: #{rental.person.class}"
else
puts 'No rentals found'
end
end
end
end
def list_all_rentals_for_a_given_person_id
list_people_ids
puts 'Please enter the ID of the person'
person_id = gets.chomp.to_i
person = @people.find { |pers| pers.id == person_id }
if person
puts "---------list of rentals for ID of #{person_id} are as follows:---------"
person.rentals.each_with_index do |rental, index|
puts "#{index}) - Borrowed Date: #{rental.date}, " \
"Borrower: #{rental.person.name}, Book borrowed: #{rental.book.title}"
end
else
puts 'rental not found for the chosen id'
end
end
end