-
Notifications
You must be signed in to change notification settings - Fork 1
/
book_manager.rb
204 lines (172 loc) · 5.18 KB
/
book_manager.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
require_relative 'book'
require_relative 'label'
require_relative 'author'
require_relative 'genre'
require 'json'
require 'date'
require 'fileutils'
class BookManager
attr_accessor :books, :labels, :authors, :genres
def initialize
@books = []
@labels = []
@authors = []
@genres = []
end
def add_a_book
color = input_book_color
publish_date = input_publish_date
publisher = input_publisher
author_first_name, author_last_name = input_author_name
label_title = input_label_title
cover_condition = input_cover_condition
label_id = generate_label_id
author_id = generate_author_id
genre_name = input_genre_name
genre_id = input_genre_id
genre = Genre.new(genre_id, genre_name)
label = Label.new(label_id, label_title, color)
book_id = generate_book_id
author = Author.new(author_id, author_first_name, author_last_name)
book = Book.new(id: book_id, publish_date: publish_date, author: author, label: label, publisher: publisher,
cover_state: cover_condition, genre: genre, source: nil)
@books.push(book)
@labels.push(label)
@authors.push(author)
@genres.push(genre)
display_message('Book added successfully.')
store_book(book)
store_label(label)
store_author(author)
store_genre(genre)
end
def generate_book_id
stored_books = load_data_from_file('data/books.json')
stored_books.size
end
def generate_label_id
stored_labels = load_data_from_file('data/labels.json')
stored_labels.size
end
def generate_author_id
stored_authors = load_data_from_file('data/authors.json')
stored_authors.size
end
def input_genre_id
stored_genres = load_data_from_file('data/genres.json')
stored_genres.size
end
def input_cover_condition
loop do
display_message('Enter the book cover condition (GOOD or BAD): ')
cover_condition = gets.chomp.upcase
return cover_condition if %w[GOOD BAD].include?(cover_condition)
display_message('Invalid cover condition. Please enter either GOOD or BAD.')
end
end
def store_genre(genre)
genre_data = {
id: genre.id,
title: genre.name,
item_ids: genre.items.map(&:id)
}
stored_genres = load_data_from_file('data/genres.json')
stored_genres << genre_data
write_data_to_file('data/genres.json', stored_genres)
end
def store_book(book)
book_data = {
id: book.id,
publisher: book.publisher,
publish_date: book.publish_date,
cover_condition: book.cover_state,
genre: book.genre.name,
label: book.label.title,
archived: book.archived
}
stored_books = load_data_from_file('data/books.json')
stored_books << book_data
write_data_to_file('data/books.json', stored_books)
end
def store_label(label)
label_data = {
id: label.id,
title: label.title,
color: label.color,
item_ids: label.items.map(&:id)
}
stored_labels = load_data_from_file('data/labels.json')
stored_labels << label_data
write_data_to_file('data/labels.json', stored_labels)
end
def store_author(author)
author_data = {
id: author.id,
first_name: author.first_name,
last_name: author.last_name,
item_ids: author.items.map(&:id)
}
stored_authors = load_data_from_file('data/authors.json')
stored_authors << author_data
write_data_to_file('data/authors.json', stored_authors)
end
def load_data_from_file(file_path)
return [] unless File.exist?(file_path)
data = File.read(file_path)
data.empty? ? [] : JSON.parse(data)
rescue Errno::ENOENT
[]
end
def write_data_to_file(file_path, data)
FileUtils.mkdir_p('data')
File.write(file_path, data.to_json)
end
def list_all_books
@books = load_data_from_file('data/books.json')
@books.each do |book|
display_message("Book id: #{book['id']}, Publisher: #{book['publisher']},
Publish Date: #{book['publish_date']}, Cover Condition: #{book['cover_condition']}")
end
end
def list_all_labels
@labels = load_data_from_file('data/labels.json')
@labels.each do |label|
display_message("Label: #{label['title']}, Color: #{label['color']}")
end
end
private
def input_book_color
display_message('Enter the book cover color: ')
gets.chomp
end
def input_publish_date
display_message('Enter the book\'s publish date (YYYY-MM-DD): ')
gets.chomp
end
def input_publisher
display_message('Enter the book publisher: ')
gets.chomp
end
def input_genre_name
display_message('Enter the genre name: ')
gets.chomp
end
def input_author_name
display_message('Enter the book author\'s first name: ')
first_name = gets.chomp
display_message('Enter the book author\'s last name: ')
last_name = gets.chomp
[first_name, last_name]
end
def input_label_title
display_message('Enter the book label: ')
gets.chomp
end
def display_message(message, wrap_at = 80)
lines = message.scan(/\S.{0,#{wrap_at - 2}}\S(?=\s|$)|\S+/)
border_line = '═' * (wrap_at + 2)
puts "╔#{border_line}╗"
lines.each { |line| puts "║ #{line.ljust(wrap_at)} ║" }
puts "╚#{border_line}╝"
end
end