forked from lerfast/m4capstone-catalog-of-my-things-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.rb
289 lines (262 loc) · 11.5 KB
/
options.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
278
279
280
281
282
283
284
285
286
287
288
289
require_relative 'classes/game'
require_relative 'classes/music_album'
require_relative 'classes/book'
require_relative 'classes/label'
require_relative 'classes/item'
require_relative 'modules/decorator'
require_relative 'modules/list'
require_relative 'modules/load_logic/load_items'
require_relative 'modules/load_logic/load_categories'
require_relative 'modules/save_logic/save_files'
require_relative 'modules/list_options'
require 'colorize'
require 'json'
module Options
include Decorator
include List
include LoadItems
include LoadCategories
include SaveFiles
include ListOptions
def display_options
puts '
██╗ ██╗███████╗██╗ ██████╗ ██████╗ ███╗ ███╗███████╗
██║ ██║██╔════╝██║ ██╔════╝██╔═══██╗████╗ ████║██╔════╝
██║ █╗ ██║█████╗ ██║ ██║ ██║ ██║██╔████╔██║█████╗
██║███╗██║██╔══╝ ██║ ██║ ██║ ██║██║╚██╔╝██║██╔══╝
╚███╔███╔╝███████╗███████╗╚██████╗╚██████╔╝██║ ╚═╝ ██║███████╗ ██╗██╗██╗
╚══╝╚══╝ ╚══════╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═╝╚═╝╚═╝
'.light_blue
loop do
puts 'Please choose an option by entering a number:'
puts '1 - List all books.'.light_green
puts '2 - List all music albums.'.light_cyan
puts '3 - List of games.'.light_yellow
puts '4 - List all genres.'.light_magenta
puts '5 - List all labels.'.cyan
puts '6 - List all authors.'.red
puts '7 - Add a book.'.light_green
puts '8 - Add a music album.'.light_cyan
puts '9 - Add a game.'.light_yellow
puts '0 - Quit.'.light_blue
option = gets.chomp
if number?(option)
process_input(option.to_i)
else
show_error
end
end
end
def number?(obj)
obj = obj.to_s unless obj.is_a? String
/\A[+-]?\d+(\.\d+)?\z/.match(obj)
end
OPTION_ACTIONS = {
1 => :list_books,
2 => :list_albums,
3 => :list_games,
4 => :list_genres,
5 => :list_labels,
6 => :list_authors,
7 => :add_book,
8 => :add_album,
9 => :add_game,
0 => :quit
}.freeze
def process_input(option)
action = OPTION_ACTIONS[option]
if action
send(action)
else
show_error
end
end
def list_books
if @books.empty?
puts 'No books available.'
else
puts "
__...--~~~~~-._ _.-~~~~~--...__
// `V' \\\\
// | \\\\
//__...--~~~~~~-._ | _.-~~~~~~--...__\\\\
//__.....----~~~~._\\ | /_.~~~~----.....__\\\\
====================\\\\|//====================
`---`
".colorize(color: :light_green, mode: :bold)
List.list_items(@books)
ListOptions.list_options(@books)
end
end
def list_albums
if @albums.empty?
puts 'No albums available.'
else
puts "
___|\\_______|________|_______________________O__________@____________
___|/_______|________|_|___|__________|__@__|_____@__|_|____O._______||
__/|____4___|__O_____|_|___|__O.______|_|@__|____|___|_|___|O.______o||
_(_/^\\__4__@|_|_____@__|___|_|________|_|@__|____|___|_|___|________o||
__\\|/'_____@__|________|__@|_|________|_|________|___|_____|_________||
d | @ | |
".colorize(color: :light_cyan, mode: :bold)
List.list_items(@albums)
ListOptions.list_options(@albums)
end
end
def list_games
if @games.empty?
puts 'No games available.'
else
puts "
__________________| |____________________________________________
,--. ,--. ,--. ,--.
|oo | _ \\ `. | oo | | oo|
o o|~~ |(_) / ; | ~~ | | ~~|o o o o o o o o o o o
|/\\/\\| '._,' |/\\/\\| |/\\/\\|
__________________ ____________________________________________
| |
".colorize(color: :light_yellow, mode: :bold)
List.list_items(@games)
ListOptions.list_options(@games)
end
end
def list_genres
puts "
█▀▀ █▀▀ █▄░█ █▀█ █▀▀ █▀
█▄█ ██▄ █░▀█ █▀▄ ██▄ ▄█".light_magenta
List.list_genres(@genres)
ListOptions.list_options(@genres)
end
def list_labels
puts "
█░░ ▄▀█ █▄▄ █▀▀ █░░ █▀
█▄▄ █▀█ █▄█ ██▄ █▄▄ ▄█".cyan
List.list_labels(@labels)
ListOptions.list_options(@labels)
end
def list_authors
puts "
▄▀█ █░█ ▀█▀ █░█ █▀█ █▀█ █▀
█▀█ █▄█ ░█░ █▀█ █▄█ █▀▄ ▄█".red
List.list_authors(@authors)
ListOptions.list_options(@authors)
end
def add_book
puts "
█▀▀ █▀█ █▀▀ ▄▀█ ▀█▀ █▀▀ ▄▀█ █▄▄ █▀█ █▀█ █▄▀
█▄▄ █▀▄ ██▄ █▀█ ░█░ ██▄ █▀█ █▄█ █▄█ █▄█ █░█".light_green
print 'Publisher: '
publisher = gets.chomp
while true
print 'Cover state (select 1 for "good" or 2 for "bad" ): '
cover_option = gets.chomp.to_i
case cover_option
when 1 then cover_state = 'good'
when 2 then cover_state = 'bad'
else
puts "Wrong option \n\n"
next
end
break
end
publish_date = verify_publish_date('Enter the publish date of the book (YYYY-MM-DD):')
book = Book.new(publisher: publisher, cover_state: cover_state, publish_date: publish_date)
Decorator.decorate(book, @authors, @genres, @labels)
@books << book
puts "
╔══╗──────╔╗───────╔╗─╔╗────╔╗
║╔╗║──────║║───────║║─║║────║║
║╚╝╚╦══╦══╣║╔╗╔══╦═╝╠═╝╠══╦═╝║
║╔═╗║╔╗║╔╗║╚╝╝║╔╗║╔╗║╔╗║║═╣╔╗║
║╚═╝║╚╝║╚╝║╔╗╗║╔╗║╚╝║╚╝║║═╣╚╝║
╚═══╩══╩══╩╝╚╝╚╝╚╩══╩══╩══╩══╝".colorize(color: :light_green, mode: :bold)
end
def add_album
puts "
█▀▀ █▀█ █▀▀ ▄▀█ ▀█▀ █▀▀ ▄▀█ █▄░█ ▄▀█ █░░ █▄▄ █░█ █▀▄▀█
█▄▄ █▀▄ ██▄ █▀█ ░█░ ██▄ █▀█ █░▀█ █▀█ █▄▄ █▄█ █▄█ █░▀░█".light_cyan
album_publish_date = verify_publish_date
album = MusicAlbum.new(album_publish_date)
Decorator.decorate(album, @authors, @genres, @labels)
@albums << album
puts "
╔═══╦╗╔╗─────────────╔╗─╔╗────╔╗
║╔═╗║║║║─────────────║║─║║────║║
║║─║║║║╚═╦╗╔╦╗╔╗╔══╦═╝╠═╝╠══╦═╝║
║╚═╝║║║╔╗║║║║╚╝║║╔╗║╔╗║╔╗║║═╣╔╗║
║╔═╗║╚╣╚╝║╚╝║║║║║╔╗║╚╝║╚╝║║═╣╚╝║
╚╝─╚╩═╩══╩══╩╩╩╝╚╝╚╩══╩══╩══╩══╝".colorize(color: :light_cyan, mode: :bold)
end
def add_game
puts "
█▀▀ █▀█ █▀▀ ▄▀█ ▀█▀ █▀▀ ▄▀█ █▀▀ ▄▀█ █▀▄▀█ █▀▀
█▄▄ █▀▄ ██▄ █▀█ ░█░ ██▄ █▀█ █▄█ █▀█ █░▀░█ ██▄".light_yellow
game_publish_date = verify_publish_date
puts 'Thats the game has multiplayer? (y/n)'
game_multiplayer = gets.chomp
game_last_played_date = verify_publish_date('Enter the last played date of the game (YYYY-MM-DD):')
game = Game.new(game_publish_date, game_multiplayer, game_last_played_date)
Decorator.decorate(game, @authors, @genres, @labels)
@games << game
puts "
╔═══╗──────────────╔╗─╔╗────╔╗
║╔═╗║──────────────║║─║║────║║
║║─╚╬══╦╗╔╦══╗╔══╦═╝╠═╝╠══╦═╝║
║║╔═╣╔╗║╚╝║║═╣║╔╗║╔╗║╔╗║║═╣╔╗║
║╚╩═║╔╗║║║║║═╣║╔╗║╚╝║╚╝║║═╣╚╝║
╚═══╩╝╚╩╩╩╩══╝╚╝╚╩══╩══╩══╩══╝".colorize(color: :light_yellow, mode: :bold)
end
def valid_date?(date_str)
date_str.match?(/^\d{4}-\d{2}-\d{2}$/)
end
def verify_publish_date(prompt = 'Enter the publish date of the item (YYYY-MM-DD):')
publish_date = ''
loop do
puts prompt
publish_date = gets.chomp
break if valid_date?(publish_date)
puts 'Invalid date! Please enter a valid date in the format YYYY-MM-DD.'
end
publish_date
end
def load_genres_from_json
LoadCategories.load_genres(@games, @albums, @books)
end
def load_labels_from_json
LoadCategories.load_labels(@games, @albums, @books)
end
def load_authors_from_json
LoadCategories.load_authors(@games, @albums, @books)
end
def load_books_from_json
LoadItems.load_books
end
def load_albums_from_json
LoadItems.load_albums
end
def load_games_from_json
LoadItems.load_games
end
def show_error
puts 'Error! Please select a valid option.'
end
def quit
SaveFiles.erase_previous_data
SaveFiles.save_books(@books)
SaveFiles.save_albums(@albums)
SaveFiles.save_games(@games)
SaveFiles.save_authors(@authors)
SaveFiles.save_genres(@genres)
SaveFiles.save_labels(@labels)
puts 'Data saved.'
puts "
░██████╗░░█████╗░░█████╗░██████╗░██████╗░██╗░░░██╗███████╗██╗
██╔════╝░██╔══██╗██╔══██╗██╔══██╗██╔══██╗╚██╗░██╔╝██╔════╝██║
██║░░██╗░██║░░██║██║░░██║██║░░██║██████╦╝░╚████╔╝░█████╗░░██║
██║░░╚██╗██║░░██║██║░░██║██║░░██║██╔══██╗░░╚██╔╝░░██╔══╝░░╚═╝
╚██████╔╝╚█████╔╝╚█████╔╝██████╔╝██████╦╝░░░██║░░░███████╗██╗
░╚═════╝░░╚════╝░░╚════╝░╚═════╝░╚═════╝░░░░╚═╝░░░╚══════╝╚═╝".colorize(color: :light_blue, mode: :bold)
exit
end
end