diff --git a/.rubocop.yml b/.rubocop.yml index 8d05f40..e19247f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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: @@ -52,4 +51,5 @@ Style/HashEachMethods: Style/HashTransformKeys: Enabled: false Style/HashTransformValues: - Enabled: false \ No newline at end of file + Enabled: false + \ No newline at end of file diff --git a/app.rb b/app.rb index fbfa1bc..61526b4 100644 --- a/app.rb +++ b/app.rb @@ -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 @@ -19,26 +77,51 @@ 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}, " @@ -46,21 +129,155 @@ def list_games 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 diff --git a/classes/genre.rb b/classes/genre.rb new file mode 100644 index 0000000..a7d5703 --- /dev/null +++ b/classes/genre.rb @@ -0,0 +1,17 @@ +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 diff --git a/classes/music.rb b/classes/music.rb new file mode 100644 index 0000000..f2e54f1 --- /dev/null +++ b/classes/music.rb @@ -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 diff --git a/data/album.json b/data/album.json new file mode 100644 index 0000000..3700229 --- /dev/null +++ b/data/album.json @@ -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"}] \ No newline at end of file diff --git a/data/game.json b/data/game.json index fcc958f..ea32747 100644 --- a/data/game.json +++ b/data/game.json @@ -1 +1,2 @@ -[{"publish_date":"2023-01-02","multiplayer":true,"last_played_at":"2023-10-10"}] \ No newline at end of file +[{"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"}] \ No newline at end of file diff --git a/data/genre.json b/data/genre.json new file mode 100644 index 0000000..5cdd87d --- /dev/null +++ b/data/genre.json @@ -0,0 +1,2 @@ +[{"id":412,"name":"Test1"},{"id":750,"name":"Test2"}, +{"id":125,"name":"Test3"},{"id":665,"name":"Test4"}] \ No newline at end of file diff --git a/main.rb b/main.rb index d87e9a7..1e9992d 100644 --- a/main.rb +++ b/main.rb @@ -1,42 +1,46 @@ require_relative 'app' +require_relative 'refactor' def main puts 'Welcome to the Catalog of my things App!' puts '----------------------------------------' 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' - puts '2. List all music albums' - puts '3. List of games' - puts '4. List all labels' - puts '5. List all genres' - puts '6. List all authors' - puts '7. Add a book' - puts '8. Add a music album' - puts '9. Add a game' - puts '10. Add an author' - puts '11. Save and Exit' + puts ' 1. 📖 List all books' + puts ' 2. 💿 List all music albums' + puts ' 3. 🎮 List of games' + puts ' 4. 🏷️ List all labels' + puts ' 5. 📑 List all genres' + puts ' 6. 🙏 List all authors' + puts ' 7. 📖 Add a book' + puts ' 8. 💿 Add a music album' + puts ' 9. 🎮 Add a game' + puts ' 10.🙏 Add an author' + puts ' 11.🚪 Exit' options = gets.chomp.to_i case options when 1 - puts 'List of all books' + app.list_all_books when 2 - puts 'List of all music albums' + app.list_music_albums when 3 app.list_games when 4 - puts 'List of all labels' + app.list_all_labels when 5 - puts 'List of all genres' + app.list_genres when 6 app.list_authors when 7 - puts 'Add a book' + refactor.add_book when 8 - puts 'Add a music album' + app.add_music_album when 9 app.add_game when 10