Skip to content

Commit

Permalink
Merge pull request #40 from AndreaM2429/preserve-book/label
Browse files Browse the repository at this point in the history
Preserve book/label
  • Loading branch information
AndreaM2429 authored Sep 14, 2023
2 parents efde81e + 1fd69d0 commit ec9f5af
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 68 deletions.
86 changes: 38 additions & 48 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
require_relative 'game'
require_relative 'preserve_games'
require_relative 'preserve_author'
require_relative 'preserve_book'
require_relative 'preserve_label'

class App
def initialize
@albums = []
@genres = []
@books_list = []
@labels = []
@labels = PreserveLabels.new.gets_labels || []
@preserve_album_genre = PreserveAlbumGenre.new(@albums, @genres)
@authors = PreserveAuthor.new.gets_author || []
@games = PreserveGames.new.gets_games || []
loading_data
@books_list = PreserveBooks.new.gets_books(@genres, @authors, @labels) || []
end

def list_all_genre
Expand Down Expand Up @@ -52,7 +54,11 @@ def list_all_books
else
puts "\nList of books"
@books_list.each_with_index do |book, index|
puts "#{index}: #{book.label.title} by #{book.author.first_name} #{book.author.last_name}"
puts "#{index}:
Title: #{book.label.title}
Author: #{book.author.first_name} #{book.author.last_name}
Archived: #{book.archived}
Genre: #{book.genre}"
end
end
end
Expand All @@ -74,39 +80,39 @@ def add_a_book
author_name = gets.chomp
print "Author's last name: "
author_last_name = gets.chomp
print "Book's publish date [dd/mm/yyyy]: "
publish_date = gets.chomp
print "Book's publisher: "
publisher = gets.chomp
print "Book's genre: "
genre = gets.chomp
print "Book's cover color: "
color = gets.chomp
print "Book's cover state: "
cover_state = gets.chomp

book = create_book(publish_date, publisher, cover_state, genre)
book = create_book
book.genre = create_genre(genre)
book.author = create_author(author_name, author_last_name)
book.label = create_label(title, color)
book.archived = book.can_be_archived?
@books_list << book
end

def create_author(first, last)
author = Author.new(first_name: first, last_name: last)
@authors << author
@authors << author unless @authors.find { |a| a.first_name == author.first_name }
author
end

def create_label(title, color)
label = Label.new(title, color)
@labels << label
@labels << label unless @labels.find { |l| l.title == label.title }
label
end

def create_book(publish_date, publisher, cover_state, genre)
new_book = Book.new(publish_date, publisher, cover_state, nil, false)
new_book.genre = Genre.new(genre)
new_book
def create_book
print "Book's publish date [dd/mm/yyyy]: "
publish_date = gets.chomp
print "Book's publisher: "
publisher = gets.chomp
print "Book's cover state: "
cover_state = gets.chomp
Book.new(publish_date: publish_date, publisher: publisher, cover_state: cover_state)
end

def add_a_music_album
Expand Down Expand Up @@ -143,28 +149,13 @@ def loading_data
@preserve_album_genre.load_genres
end

def add_an_author
print 'Add an Author:'
puts 'Add Author`s first name:'
first_name = gets.chomp.to_s
puts 'Add Author`s last name:'
last_name = gets.chomp.to_s

author = Author.new(first_name: first_name, last_name: last_name)
print " Author added!: #{author.first_name} #{author.last_name} "

@authors << author

PreserveAuthor.new.save_authors(@authors)
end

def list_all_authors
if @authors.empty?
puts "\nThis is empty! :("
else
puts "\nList all authors:"
@authors.each do |author|
puts " First Name: #{author.first_name}, Last Name: #{author.last_name} "
@authors.each_with_index do |author, index|
puts " #{index}: #{author.first_name} #{author.last_name} "
end
end
end
Expand All @@ -173,30 +164,25 @@ def add_a_game
print 'Is it multiplayer? [y/n]:'
multiplayer = gets.chomp.upcase == 'Y'

print 'Is it archived? [y/n]:'
archived = gets.chomp.upcase == 'Y'

print 'Add a Published date:'
publish_date = gets.chomp.to_s
print 'Add a Published date [dd/mm/yyyy] :'
publish_date = gets.chomp

print 'Add an Author:'
author_name = gets.chomp.to_s

print 'Add a Genre:'
genre_name = gets.chomp.to_s

print 'Add a Label:'
print 'Add a Title:'
label_name = gets.chomp.to_s

game = Game.new(publish_date: publish_date, multiplayer: multiplayer)
game.archived = archived
game.author = author_name
game.genre = genre_name
game.label = label_name
game.archived = game.can_be_archived?
game.author = create_author(author_name, '')
game.genre = create_genre(genre_name)
game.label = create_label(label_name, 'N/A')

@games << game

PreserveGames.new.save_games(@games)
end

def list_all_games
Expand All @@ -208,16 +194,20 @@ def list_all_games
puts "ID: #{game.id}
Published day: #{game.publish_date}
Archived: #{game.archived ? 'Yes' : 'No'}
Label: #{game.label}
Genre: #{game.genre}
Author: #{game.author}"
Label: #{game.label.title}
Genre: #{game.genre.name}
Author: #{game.author.first_name}"
end
end
end

def end_app
PreserveAuthor.new.save_authors(@authors)
PreserveGames.new.save_games(@games)
@preserve_album_genre.save_genres
@preserve_album_genre.save_music_albums
PreserveBooks.new.save_books(@books_list)
PreserveLabels.new.save_labels(@labels)
puts 'Thank you for using this app (•◡•)丿'
exit
end
Expand Down
1 change: 0 additions & 1 deletion authors.json

This file was deleted.

8 changes: 3 additions & 5 deletions book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
class Book < Item
attr_accessor :publisher, :cover_state

def initialize(publish_date, publisher, cover_state, id, archived)
super(publish_date, id, archived)
def initialize(publish_date:, publisher: '', cover_state: 'bad', id: nil)
super(publish_date: publish_date, id: id, archived: archived)
@publisher = publisher
@cover_state = cover_state
end

def can_be_archived?
return true if super || @cover_state == 'bad'

false
super || @cover_state == 'bad'
end
end
4 changes: 3 additions & 1 deletion game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def initialize(
end

def can_be_archived?
super && @last_played_at < (Date.today - (2 * 365))
date = (Date.today - (2 * 365))
last_played_date = Date.parse(@last_played_at)
super && last_played_date < date
end
end
1 change: 0 additions & 1 deletion games.json

This file was deleted.

12 changes: 5 additions & 7 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ def initialize
@app = App.new
@menu = {
'1' => 'list_all_books', '2' => 'list_all_albums',
'3' => 'list_all_movies', '4' => 'list_all_games',
'5' => 'list_all_genre', '6' => 'list_all_labels',
'7' => 'list_all_authors', '8' => 'add_a_book',
'9' => 'add_a_music_album', '10' => 'add_a_game',
'11' => 'add_an_author', '12' => 'add_a_genre',
'13' => 'end_app'
'3' => 'list_all_games', '4' => 'list_all_genre',
'5' => 'list_all_labels', '6' => 'list_all_authors',
'7' => 'add_a_book', '8' => 'add_a_music_album',
'9' => 'add_a_game', '10' => 'end_app'
}.freeze
end

Expand All @@ -23,7 +21,7 @@ def menu
action
else
puts "\n(͠°◡͡°) Invalid option select a valid one\n\n"
choose
menu
end
end

Expand Down
42 changes: 42 additions & 0 deletions preserve_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'json'
require_relative 'book'

class PreserveBooks
def gets_books(genres, authors, labels)
return unless File.exist?('./books.json')

saved_books = []
file = File.read('./books.json')
data = JSON.parse(file)

data.each do |element|
genre = genres.select { |gen| gen.name == element['genre'] }
author = authors.select { |aut| aut.first_name == element['author'] }
label = labels.select { |lab| lab.title == element['label'] }

book = Book.new(publish_date: element['publish_date'], publisher: element['publisher'],
cover_state: element['cover_state'], id: element['id'])
book.genre = genre[0]
book.author = author[0]
book.label = label[0]
book.archived = element['archived']

saved_books << book
end
saved_books
end

def save_books(books)
return if books.empty?

data_hash = []

books.each do |book|
data_hash << { id: book.id, publish_date: book.publish_date,
archived: book.archived, genre: book.genre, author: book.author.first_name,
label: book.label.title, publisher: book.publisher, cover_state: book.cover_state }
end

File.write('./books.json', JSON.dump(data_hash))
end
end
14 changes: 9 additions & 5 deletions preserve_games.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'json'
require_relative 'label'
require_relative 'genre'
require_relative 'author'

class PreserveGames
def gets_games
Expand All @@ -13,9 +16,10 @@ def gets_games
game = Game.new(publish_date: game_data['publish_date'], multiplayer: game_data['multiplayer'])
game.id = game_data['id']
game.archived = game_data['archived']
game.author = game_data['author']
game.genre = game_data['genre']
game.label = game_data['label']
game.author = Author.new(first_name: game_data['author'], last_name: '')
game.genre = Genre.new(game_data['genre'])
game.label = Label.new(game_data['label'], 'N/A')

saved_games << game
end
saved_games
Expand All @@ -26,8 +30,8 @@ def save_games(games)

data_hash = []
games.each_with_index do |game, _index|
data_hash << { label: game.label, genre: game.genre, author: game.author, multiplayer: game.multiplayer,
last_played_at: game.last_played_at }
data_hash << { label: game.label.title, genre: game.genre.name, author: game.author.first_name,
multiplayer: game.multiplayer, last_played_at: game.last_played_at }
end
File.write('./games.json', JSON.dump(data_hash))
end
Expand Down
24 changes: 24 additions & 0 deletions preserve_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'json'
require_relative 'label'

class PreserveLabels
def gets_labels
return unless File.exist?('./labels.json')

saved_labels = []
file_data = File.read('./labels.json')
data = JSON.parse(file_data)
data.each { |elem| saved_labels << Label.new(elem['title'], elem['color'], elem['id']) }
saved_labels
end

def save_labels(labels)
return if labels.empty?

data_hash = []
labels.each do |label|
data_hash << { id: label.id, title: label.title, color: label.color, items_class: label.items[0].class }
end
File.write('./labels.json', JSON.dump(data_hash))
end
end

0 comments on commit ec9f5af

Please sign in to comment.