Skip to content

Commit

Permalink
Merge pull request #41 from hafiz1379/styling-app
Browse files Browse the repository at this point in the history
Catalog of my Things | Styling app
  • Loading branch information
JuanDiegoGuerra authored Oct 18, 2023
2 parents f2ea9b3 + 03e6f9e commit 9de3c7a
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 101 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ AllCops:
Layout/LineLength:
Max: 140
Metrics/MethodLength:
Max: 20
Max: 30
Metrics/AbcSize:
Max: 50
Metrics/ClassLength:
Max: 160
Max: 170
Metrics/BlockLength:
IgnoredMethods: ['describe']
Max: 30
Expand Down
22 changes: 22 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@ GEM
specs:
ast (2.4.2)
base64 (0.1.1)
coderay (1.1.3)
diff-lcs (1.5.0)
json (2.6.3)
language_server-protocol (3.17.0.3)
method_source (1.0.0)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
racc
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
racc (1.6.2)
rainbow (3.1.1)
regexp_parser (2.8.1)
rexml (3.2.5)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (1.56.3)
base64 (~> 0.1.1)
json (~> 2.3)
Expand All @@ -31,8 +50,11 @@ GEM

PLATFORMS
x64-mingw-ucrt
x86_64-linux

DEPENDENCIES
pry (~> 0.14.2)
rspec (~> 3.12.0)
rubocop (>= 1.0, < 2.0)

BUNDLED WITH
Expand Down
97 changes: 53 additions & 44 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
require_relative 'classes/music_album'
require_relative 'classes/music/load_music_genre'
require_relative 'classes/music/save_music_genre'
require_relative 'modules/music_album_module'
require_relative 'modules/commands_module'
require 'date'
require 'colorize'

class App
attr_accessor :music_albums, :genres
Expand All @@ -23,6 +26,7 @@ def initialize

def run
loop do
render_ascii_art
display_menu
choice = gets.chomp.to_i
action = @actions[choice]
Expand All @@ -40,7 +44,7 @@ def find_label_by_title(title)

private

def load_books_from_json(filename = 'books.json')
def load_books_from_json(filename = 'json/books.json')
return unless File.exist?(filename)

data = JSON.parse(File.read(filename))
Expand All @@ -57,7 +61,7 @@ def load_books_from_json(filename = 'books.json')
end
end

def write_books_to_json(filename = 'books.json')
def write_books_to_json(filename = 'json/books.json')
books_data = @books.map do |book|
{
'id' => book.id,
Expand All @@ -73,7 +77,6 @@ def write_books_to_json(filename = 'books.json')

def initialize_collections
@books = []
@music_albums = []
@games = []
@labels = initialize_labels
@genres = []
Expand Down Expand Up @@ -101,25 +104,40 @@ def initialize_labels
[
Label.new('1', 'New', 'Green'),
Label.new('2', 'Older', 'Yellow'),
Label.new('3', 'Gift', 'Red')
Label.new('3', 'Gift', 'magenta')
]
end

def render_ascii_art
puts '
██╗ ██╗███████╗██╗ ██████╗ ██████╗ ███╗ ███╗███████╗
██║ ██║██╔════╝██║ ██╔════╝██╔═══██╗████╗ ████║██╔════╝
██║ █╗ ██║█████╗ ██║ ██║ ██║ ██║██╔████╔██║█████╗
██║███╗██║██╔══╝ ██║ ██║ ██║ ██║██║╚██╔╝██║██╔══╝
╚███╔███╔╝███████╗███████╗╚██████╗╚██████╔╝██║ ╚═╝ ██║███████╗ ██╗██╗██╗
╚══╝╚══╝ ╚══════╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═╝╚═╝╚═╝
'.green
puts 'To start, pick an option from the list below (1-10):'.bold
puts ''
end

def display_menu
puts '1. Add Book'
puts '2. Add Music Album'
puts '3. Add Game'
puts '4. List All Books'
puts '5. List All Music Albums'
puts '6. List All Games'
puts '7. List All Labels'
puts '8. List All Genres'
puts '9. List All Authors'
puts '10. Exit'
puts '1. Add Book .'.bold.black.on_light_magenta
puts '2. Add Music Album .'.bold.black.on_light_yellow
puts '3. Add Game .'.bold.black.on_light_cyan
puts '4. List All Books .'.bold.black.on_light_magenta
puts '5. List All Music Albums .'.bold.black.on_light_yellow
puts '6. List All Games .'.bold.black.on_light_cyan
puts '7. List All Labels .'.bold.black.on_light_magenta
puts '8. List All Genres .'.bold.black.on_light_yellow
puts '9. List All Authors .'.bold.black.on_light_cyan
puts '10. Exit -----------------.'.bold.white.on_black
end

def exit_app
puts 'Goodbye!'
puts 'Thanks for using the app! '.bold.white.on_green
puts 'Developed by: Hafiz - Jhon - JD '.bold.white.on_green
save_genres
exit
end
Expand All @@ -135,64 +153,55 @@ def add_book

def list_all_genres
puts 'The list is empty, please create a Genre!' if @genres.empty?

return if @genres.empty?

puts 'List of all genres:'.bold.black.on_light_yellow
puts '-----------------------------'.black.on_light_yellow
@genres.each_with_index do |genre, index|
puts "#{index + 1}. #{genre.name}"
end
puts '------------------'
puts '-----------------------------'.black.on_light_yellow
puts '[Press ENTER to continue]'
gets.chomp
end

def add_game
GameModule.add_game(@games)
end

def list_all_games
Commands.clear_screen
GameModule.list_all_games(@games)
Commands.clear_screen
end

def list_all_authors
Commands.clear_screen
AuthorModule.list_all_authors(@authors)
Commands.clear_screen
end

def list_all_music_albums
puts 'The list is empty, please create a Music Album!' if @music_albums.empty?
puts 'List of all music albums:'
@music_albums.each_with_index do |album, index|
next unless album.is_a?(MusicAlbum)

spotify_status = album.on_spotify ? 'Yes' : 'No'
puts "#{index + 1}. Published: #{album.published_date}, Archived: #{album.archivedtoo}, Spotify: #{spotify_status}"
end
puts '------------------'
Commands.clear_screen
MusicAlbumModule.list_all_music_albums(@music_albums)
Commands.clear_screen
end

def add_music_album
print 'Enter published date YYYY-MM-DD: '
date_input = gets.chomp
begin
published_date = Date.parse(date_input)
current_date = Date.today
difference = (current_date - published_date).to_i / 365

puts "The album was published #{difference} years ago"
rescue ArgumentError
puts 'invalid date format. Please enter the date in YYYY-MM-DD format'
end

print 'Is it on Spotify? (true/false): '
on_spotify = gets.chomp.downcase == 'true'
music_album = MusicAlbum.new(published_date: published_date, on_spotify: on_spotify)
music_albums << music_album

puts 'Music album added successfully!'
puts '------------------'
MusicAlbumModule.add_music_album(@music_albums)
save_music_albums
end

def list_all_books
Commands.clear_screen
BookModule.list_books(@books)
Commands.clear_screen
end

def list_all_labels
Commands.clear_screen
LabelModule.list_labels(@labels)
Commands.clear_screen
end
end
6 changes: 2 additions & 4 deletions classes/music/load_music_genre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
def load_music_albums
stored_music_albums = begin
JSON.parse(File.read('json/music_albums.json'))
rescue StandardError => e
puts "Error reading 'music_albums.json': #{e.message}"
rescue StandardError
[]
end
music_albums = []
Expand All @@ -21,8 +20,7 @@ def load_music_albums
def load_genre
stored_genre = begin
JSON.parse(File.read('json/genres.json'))
rescue StandardError => e
puts "Error reading 'genres.json': #{e.message}"
rescue StandardError
[]
end
genres = []
Expand Down
44 changes: 22 additions & 22 deletions json/books.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
[
{
"id": 2400,
"publisher": "1",
"cover_state": "good",
"publish_date": null,
"label": "New"
},
{
"id": 8047,
"publisher": "2",
"cover_state": "bad",
"publish_date": null,
"label": "Gift"
},
{
"id": 8163,
"publisher": "3",
"cover_state": "bad",
"publish_date": "2022-11-30",
"label": "New"
}
]
{
"id": "98c1",
"publisher": "John",
"cover_state": "good",
"publish_date": null,
"label": "Older"
},
{
"id": "9a1b",
"publisher": "John",
"cover_state": "bad",
"publish_date": null,
"label": "Gift"
},
{
"id": "d218",
"publisher": "John",
"cover_state": "good",
"publish_date": null,
"label": "Gift"
}
]
4 changes: 2 additions & 2 deletions json/games.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"multiplayer": true,
"last_played_at": "2000-01-01"
"multiplayer": false,
"last_played_at": "2000-08-06"
}
]
2 changes: 1 addition & 1 deletion json/genres.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"bad"},{"name":"classic"},{"name":"Gaming"}]
[{"name":"Horror"},{"name":"Romance"},{"name":"Adventure"}]
2 changes: 1 addition & 1 deletion json/music_albums.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"published_date":"2020-10-10","on_spotify":true},{"published_date":"2012-12-02","on_spotify":false},{"published_date":"2021-01-08","on_spotify":false},{"published_date":"2022-02-12","on_spotify":true},{"published_date":"1998-07-07","on_spotify":false},{"published_date":"2023-10-17","on_spotify":true},{"published_date":"2023-03-03","on_spotify":false}]
[{"published_date":"2000-05-30","on_spotify":true},{"published_date":"2000-05-03","on_spotify":false},{"published_date":"2000-05-25","on_spotify":false},{"published_date":"2030-01-20","on_spotify":false}]
6 changes: 3 additions & 3 deletions modules/author_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ def self.list_all_authors(collection)
puts 'Please, create an AUTHOR. [Press ENTER to continue]'
gets.chomp
else
puts 'Authors'
puts '-----------------------------'
puts 'Authors'.bold.black.on_light_cyan
puts '-----------------------------'.black.on_light_cyan
collection.each do |author|
puts "ID: #{author.id}, Name: #{author.first_name} #{author.last_name}, Items(Quantity): #{author.items.size}"
end
puts '-----------------------------'
puts '-----------------------------'.black.on_light_cyan
end
end
end
15 changes: 9 additions & 6 deletions modules/book_module.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module BookModule
def self.list_books(books)
puts 'List of Books:'
puts 'List of Books:'.bold.black.on_light_magenta
puts '-----------------------------'.black.on_light_magenta
books.each_with_index do |book, index|
label_title = book.label ? book.label.title : 'N/A'
puts "#{index + 1}. ID: #{book.id}, " \
"Publisher: #{book.publisher}, " \
"Cover State: #{book.cover_state}, " \
"Label: #{label_title}"
end
puts '------------------'
puts '-----------------------------'.black.on_light_magenta
puts '[Press ENTER to continue]'
gets.chomp
end

def self.add_book(books, _genres, _authors, labels)
Expand All @@ -28,23 +31,23 @@ def self.collect_attributes
publisher: input('Enter the publisher:'),
cover_state: cover_state_input,
label: nil,
publish_date: input('Enter the publish date (DD-MM-YYYY):')
publish_date: input('Enter the publish date (YYYY-MM-DD):')
}
end

def self.input(prompt)
puts prompt
print prompt
gets.chomp
end

def self.cover_state_input
puts "Enter the cover state: 1 for 'good', 2 for 'bad'"
puts 'Enter the cover state (1-Good, 2-Bad):'
loop do
choice = gets.chomp
return 'good' if choice == '1'
return 'bad' if choice == '2'

puts "Invalid input. Please enter 1 for 'good' or 2 for 'bad'."
puts 'Invalid input. Please enter (1-Good, 2-Bad).'
end
end

Expand Down
Loading

0 comments on commit 9de3c7a

Please sign in to comment.