-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
152 lines (140 loc) · 4.71 KB
/
Copy pathapp.rb
File metadata and controls
152 lines (140 loc) · 4.71 KB
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
require_relative 'book'
require_relative 'label'
require_relative 'item'
require_relative 'game'
require_relative './store/preserve_data'
require_relative 'author'
require_relative './src/music/genre'
require_relative './src/music/music_album'
require_relative './handler'
require 'json'
require 'date'
class App < Handler
def initialize
super
@genres = []
@music_albums = []
@store = PreserveData.new
@books = File.empty?('./store/books.json') ? [] : @store.load_data('./store/books.json')
@labels = File.empty?('./store/labels.json') ? [] : @store.load_data('./store/labels.json')
@games = File.empty?('./store/games.json') ? [] : @store.load_data('./store/games.json')
@authors = File.empty?('./store/authors.json') ? [] : @store.load_data('./store/authors.json')
end
def list_books
@books.each do |book|
puts "#{book['id']} - #{book['author']} - #{book['published_date']}"
end
end
def list_labels
@labels.each do |label|
puts "#{label['id']} - #{label['title']} - #{label['color']}"
end
end
def create_book
puts 'Enter author'
author = gets.chomp
puts 'Enter published date (YYYY-MM-DD))'
published_date = gets.chomp
puts 'Enter publisher'
publisher = gets.chomp
puts 'Enter cover state'
cover_state = gets.chomp
puts 'Enter label title'
label_title = gets.chomp
puts 'Enter label color'
label_color = gets.chomp
label = create_label(label_title, label_color)
book = create_book_object(author, published_date, publisher, cover_state, label)
@books << book
@labels << label
@store.save_data(@books, './store/books.json')
@store.save_data(@labels, './store/labels.json')
puts 'Book created successfully'
end
def create_label(title, color)
label = Label.new(title, color)
{ 'id' => label.id, 'title' => label.title, 'color' => label.color }
end
def create_book_object(author, published_date, publisher, cover_state, label)
book = Book.new(author, published_date, publisher, cover_state, label)
{
'id' => book.id,
'author' => book.author,
'published_date' => book.published_date,
'publisher' => book.publisher,
'cover_state' => book.cover_state,
'label' => book.label
}
end
def list_games
if @games.empty?
puts 'No games found in the system currently'
else
puts '=================================================================:'
puts 'List of Games:'
puts '=================================================================:'
puts 'ID - Game Name - Last Played Date - Published Date - Multiplayer'
@games.each do |game|
puts "#{game['id']} - #{game['game_name']} - #{game['last_played_at']} - #{game['publish_date']} - #{game['multiplayer']}"
end
puts '=================================================================:'
end
end
def list_authors
if @authors.empty?
puts 'No authors found in the system currently'
else
puts '=============================================:'
puts 'List of Authors:'
puts '=============================================:'
puts 'ID - First Name - Last Name'
@authors.each do |author|
puts "#{author['id']} - #{author['first_name']} - #{author['last_name']}"
end
puts '=============================================:'
end
end
def create_games
puts 'write Name of the game'
game_name = gets.chomp
puts 'Write last played date (YYYY-MM-DD))'
last_played_at = gets.chomp
puts 'Enter published date (YYYY-MM-DD))'
publish_date = gets.chomp
puts 'Enter multiplayer (yes/no)'
multiplayer = gets.chomp.downcase == 'yes'
puts 'Enter first name'
first_name = gets.chomp
puts 'Enter last name'
last_name = gets.chomp
author = create_author(first_name, last_name)
@authors << author
@store.save_data(@authors, './store/authors.json')
game = create_game_object(game_name, last_played_at, publish_date, multiplayer)
@games << game
@store.save_data(@games, './store/games.json')
puts 'Game is created successfully'
end
def create_author(first_name, last_name)
author = Author.new(first_name, last_name)
{
'id' => author.id,
'first_name' => author.first_name,
'last_name' => author.last_name
}
end
def create_game_object(game_name, last_played_at, publish_date, multiplayer)
game = Game.new(game_name, last_played_at, publish_date, multiplayer)
{
'id' => game.id,
'game_name' => game.game_name,
'last_played_at' => game.last_played_at,
'publish_date' => game.publish_date,
'multiplayer' => game.multiplayer
}
end
def exit_app
puts 'Thanks for using the app!'
exit
end
end