Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book-label-refactor-PullRequest #33

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 26 additions & 141 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
require_relative 'classes/game'
require_relative 'classes/author'
require_relative 'classes/music'
require_relative 'classes/genre'
require 'json'

class App
attr_accessor :games, :authors, :music_albums, :genres

require_relative 'classes/book'
require_relative 'classes/label'
require 'json'
Expand All @@ -17,8 +10,6 @@ class App
def initialize
@games = []
@authors = []
@music_albums = []
@genres = []
@labels = []
@books = []
end
Expand Down Expand Up @@ -68,7 +59,7 @@ def add_book(publisher, cover_state, publish_date)
end

def ask_multiplayer
print 'Is the game multiplayer [Y/N] : '
print 'Isthe game multiplayer [Y/N] : '
loop do
input = gets.chomp.downcase
case input
Expand All @@ -89,8 +80,8 @@ def add_author
print 'Enter the last name of the author : '
last_name = gets.chomp
author = Author.new(first_name, last_name)
authors << author
puts 'Author is added successfully'
@authors << author
puts ' Author is added successfully '
end

def add_game
Expand All @@ -101,125 +92,40 @@ def add_game
print 'Last the game was played [yyyy/mm/dd] : '
last_time = gets.chomp
game = Game.new(publish_date, multiplayer, last_time)
games << game
puts 'The Game is added successfully'
end

def add_music_album
puts 'Please add a music album'
print 'Is the album on Spotify? [Y/N]: '
on_spotify = gets.chomp.downcase == 'y'
print 'Enter the genre of the album: '
genre_name = gets.chomp
genre = find_or_create_genre(genre_name)
print 'Add the publish date of your album [yyyy/mm/dd] : '
publish_date = gets.chomp
music_album = MusicAlbum.new(publish_date, on_spotify: on_spotify, genre: genre)
music_albums << music_album
puts 'The Music Album is added successfully'
@games << game
puts 'The Game is added sucessfully/n'
end

def list_games
puts 'No game added' if games.empty?
games.each_with_index do |game, index|
puts 'No game added' if @games.empty?
@games.each_with_index do |game, index|
print "Game #{index + 1} - "
print "Publish Date: #{game.publish_date}, "
print "Multiplayer: #{game.multiplayer}, "
print "Last Played at: #{game.last_played_at}\n"
end
end

def list_music_albums
puts 'No music albums added' if music_albums.empty?
music_albums.each do |music_album|
puts "ID: #{music_album.id}, Genre: #{music_album.genre.name}, On Spotify: #{music_album.on_spotify}, Published Date: #{music_album.publish_date}"
puts '-------------------------'
end
end

def list_authors
puts 'No author added' if authors.empty?
authors.each_with_index do |author, index|
puts 'No author added' if @authors.empty?
@authors.each_with_index do |author, index|
puts "Author #{index + 1} - Fullname: #{author.first_name} #{author.last_name}"
end
end

def list_genres
puts 'No genres added' if genres.empty?
genres.each do |genre|
puts genre.name
puts '............'
end
end

def save_data
save_games
save_authors
save_music_albums
save_genres
end

def load_data
load_games
load_authors
load_music_albums
load_genres
end

private

def load_authors
if File.exist?('data/author.json')
data = JSON.parse(File.read('data/author.json'))
self.authors = data.map { |author| Author.new(author['first_name'], author['last_name']) }
else
self.authors = []
end
end

def save_authors
File.open('data/author.json', 'w') do |file|
data = authors.map do |author|
{
'first_name' => author.first_name,
'last_name' => author.last_name
}
end
file.write(JSON.generate(data))
end
end

def find_or_create_genre(name)
existing_genre = genres.find { |genre| genre.name == name }
return existing_genre if existing_genre

new_genre = Genre.new(generate_genre_id, name)
genres << new_genre
new_genre
end

def generate_genre_id
existing_ids = genres.map(&:id)
new_id = nil
loop do
new_id = rand(1..1000)
break unless existing_ids.include?(new_id)
end
new_id
end

def load_games
if File.exist?('data/game.json')
data = JSON.parse(File.read('data/game.json'))
self.games = data.map { |game| Game.new(game['publish_date'], game['multiplayer'], game['last_played_at']) }
else
self.games = []
end
end

def save_games
File.open('data/game.json', 'w') do |file|
data = games.map do |game|
data = @games.map do |game|
{
'publish_date' => game.publish_date,
'multiplayer' => game.multiplayer,
Expand All @@ -230,54 +136,33 @@ def save_games
end
end

def load_music_albums
if File.exist?('data/album.json')
data = JSON.parse(File.read('data/album.json'))
self.music_albums = data.map do |album_data|
genre = find_or_create_genre(album_data['genre']['name'])
MusicAlbum.new(album_data['published_date'], on_spotify: album_data['on_spotify'], genre: genre,
archived: false)
end
def load_games
if File.exist?('data/game.json')
data = JSON.parse(File.read('data/game.json'))
@games = data.map { |game| Game.new(game['publish_date'], game['multiplayer'], game['last_played_at']) }
else
self.music_albums = []
[]
end
end

def save_music_albums
File.open('data/album.json', 'w') do |file|
data = music_albums.map do |music_album|
def save_authors
File.open('data/author.json', 'w') do |file|
data = @authors.map do |author|
{
'id' => music_album.id,
'on_spotify' => music_album.on_spotify,
'genre' => {
'id' => music_album.genre.id,
'name' => music_album.genre.name
},
'published_date' => music_album.publish_date.to_s
'first_name' => author.first_name,
'last_name' => author.last_name
}
end
file.write(JSON.generate(data))
end
end

def load_genres
if File.exist?('data/genre.json')
data = JSON.parse(File.read('data/genre.json'))
self.genres = data.map { |genre_data| Genre.new(genre_data['id'], genre_data['name']) }
def load_authors
if File.exist?('data/author.json')
data = JSON.parse(File.read('data/author.json'))
@authors = data.map { |author| Author.new(author['first_name'], author['last_name']) }
else
self.genres = []
end
end

def save_genres
File.open('data/genre.json', 'w') do |file|
data = genres.map do |genre|
{
'id' => genre.id,
'name' => genre.name
}
end
file.write(JSON.generate(data))
[]
end
end
end
end
9 changes: 4 additions & 5 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ def main
app = App.new
refactor = Refactor.new(app)
app.load_data

loop do
puts 'Please select an option by entering a number:'
puts ' 1. 📖 List all books'
Expand All @@ -28,19 +27,19 @@ def main
when 1
app.list_all_books
when 2
app.list_music_albums
puts 'List of all music albums'
when 3
app.list_games
when 4
app.list_all_labels
when 5
app.list_genres
puts 'List of all genres'
when 6
app.list_authors
when 7
refactor.add_book
when 8
app.add_music_album
puts 'Add a music album'
when 9
app.add_game
when 10
Expand All @@ -54,4 +53,4 @@ def main
end
end

main
main
42 changes: 42 additions & 0 deletions refactor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Refactor
def initialize(app)
@app = app
end

def add_book
print 'Book publisher: '
publisher = gets.chomp
cover_state = ask_cover_state
print 'Book publish date [yyyy/mm/dd]: '
publish_date = gets.chomp
label_name = ask_label_name
label_color = ask_label_color
@app.add_book(publisher, cover_state, publish_date)
@app.add_label(label_name, label_color)
end

def ask_label_name
print "Book's label name: "
gets.chomp
end

def ask_label_color
print "Book's label color: "
gets.chomp
end

def ask_cover_state
print 'Cover state of the book [good/bad]: '
loop do
input = gets.chomp.downcase
case input
when 'good'
return 'good'
when 'bad'
return 'bad'
else
print 'Please enter a valid input "good" or "bad": '
end
end
end
end
Loading