Skip to content

Commit

Permalink
Adding files for music-album
Browse files Browse the repository at this point in the history
  • Loading branch information
gilberthappi committed Nov 30, 2023
1 parent d366843 commit 5db1d95
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 41 deletions.
10 changes: 5 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ AllCops:
- "Guardfile"
- "Rakefile"
- 'main.rb'
- 'app.rb'
- "node_modules/**/*"

DisplayCopNames: true

Layout/LineLength:
Max: 120
Metrics/CyclomaticComplexity:
Max: 12
Metrics/MethodLength:
Max: 45
Max: 20
Metrics/AbcSize:
Max: 50
Metrics/ClassLength:
Max: 150
Metrics/BlockLength:
IgnoredMethods: ['describe']
Max: 40
Max: 30


Style/Documentation:
Expand Down Expand Up @@ -52,4 +51,5 @@ Style/HashEachMethods:
Style/HashTransformKeys:
Enabled: false
Style/HashTransformValues:
Enabled: false
Enabled: false

253 changes: 235 additions & 18 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
require_relative 'classes/game'
require_relative 'classes/author'
require_relative 'classes/music'
require_relative 'classes/genre'
require 'json'

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

require_relative 'classes/book'
require_relative 'classes/label'
require 'json'

class App
attr_accessor :game, :author, :books, :labels

def initialize
@games = []
@authors = []
@music_albums = []
@genres = []
@labels = []
@books = []
end

def add_label(title, color)
label = Label.new(title, color)
@labels << label
end

def list_all_labels
if @labels.empty?
puts 'No labels added'
puts ' '
else
puts 'List of all labels:'
@labels.each_with_index do |label, index|
puts "#{index + 1}. Label: #{label.title}, Color: #{label.color}"
puts ' '
end
end
end

def list_all_books
if @books.empty?
puts 'No books added'
puts ' '
else
@books.each_with_index do |book, index|
print "#{index + 1}-[Book], "
print "ID: #{book.id}, "
print "Publisher: #{book.publisher}, "
print "Publish Date: #{book.publish_date}, "
print "Cover State: #{book.cover_state}, "
puts "Archived: #{book.can_be_archived?}"
puts ' '
end
end
end

def add_book(publisher, cover_state, publish_date)
book = Book.new(publish_date, publisher, cover_state)
@books << book
puts ' '
puts 'The book is added successfully ✅📖'
puts '--------------------------------------'
puts ' '
end

def ask_multiplayer
Expand All @@ -19,48 +77,207 @@ def ask_multiplayer
when 'n'
return false
else
print 'Please enter a valid input "Y" or "N" : '
print 'Please enter a valid input "Y" or "N" :'
end
end
end

def add_author
puts 'Please add an author'
print 'Enter the first name of the author : '
first_name = gets.chomp
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'
end

def add_game
puts 'Please add a game'
print 'Add the publish date of your game [yyyy/mm/dd] : '
publish_date = gets.chomp
multiplayer = ask_multiplayer
print 'Last game was played [yyyy/mm/dd] : '
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 succesfully/n'
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'
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 add_author
puts 'Please add an author'
print 'Enter the first name of the author : '
first_name = gets.chomp
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 succesfully '
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|
{
'publish_date' => game.publish_date,
'multiplayer' => game.multiplayer,
'last_played_at' => game.last_played_at
}
end
file.write(JSON.generate(data))
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
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|
{
'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
}
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']) }
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
18 changes: 18 additions & 0 deletions classes/genre.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Genre
attr_reader :id, :name
attr_accessor :items

def initialize(id, name)
@id = id
@name = name
@items = []
end

def add_item(item)
return if @items.include?(item)

@items << item
item.genre = self
end
end

15 changes: 15 additions & 0 deletions classes/music.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative 'item'

class MusicAlbum < Item
attr_accessor :on_spotify, :genre

def initialize(publish_date, on_spotify: false, genre: nil, archived: false)
super(publish_date, archived: archived)
@on_spotify = on_spotify
@genre = genre
end

def can_be_archived?
super && @on_spotify
end
end
4 changes: 4 additions & 0 deletions data/album.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[{"id":501,"on_spotify":true,"genre":{"id":716,"name":"Test1"},"published_date":"2023-11-01"},
{"id":619,"on_spotify":true,"genre":{"id":958,"name":"Test2"},"published_date":"2023-11-03"},
{"id":549,"on_spotify":true,"genre":{"id":826,"name":"Test3"},"published_date":"2023-11-11"},
{"id":954,"on_spotify":true,"genre":{"id":665,"name":"Test4"},"published_date":"2023-11-02"}]
3 changes: 2 additions & 1 deletion data/game.json
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
[{"publish_date":"2023-01-02","multiplayer":true,"last_played_at":"2023-10-10"}]
[{"publish_date":"2023-01-02","multiplayer":true,"last_played_at":"2023-10-10"},
{"publish_date":"2023-11-11","multiplayer":true,"last_played_at":"2023-11-02"}]
2 changes: 2 additions & 0 deletions data/genre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[{"id":412,"name":"Test1"},{"id":750,"name":"Test2"},
{"id":125,"name":"Test3"},{"id":665,"name":"Test4"}]
Loading

0 comments on commit 5db1d95

Please sign in to comment.