From 0a8da6066a5e675cbdaa9c54f19cc855fe326131 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 20 Dec 2023 13:31:18 +0400 Subject: [PATCH 1/7] Add App class with methods for listing books and labels, and adding a book --- app.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 app.rb diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..f35891e --- /dev/null +++ b/app.rb @@ -0,0 +1,52 @@ +require './classes/item' +require './classes/book' +require './classes/label' + +class App + def initialize + @books = [] + @labels = [] + end + + # list methods + def list_all_books + puts "\nListing all books:" + if @books.empty? + puts 'No books found.' + else + @books.each do |book| + puts "ID: #{book.id}, Author: #{book.author}, Publisher: #{book.publisher}, Cover state: #{book.cover_state}" + end + end + end + + def list_all_labels + puts "\nListing all labels:" + if @labels.empty? + puts 'No labels found.' + else + @labels.each do |label| + puts "Title: #{label.title}, Color: #{label.color}" + end + end + end + + # add methods + def add_book + puts 'Published by:' + publisher = gets.chomp.to_s + puts 'Cover state (good or bad):' + cover_state = gets.chomp.to_s + puts 'Published date (YYYY-MM-DD):' + loop do + @publish_date = gets.chomp + break if @publish_date.match?(/\d{4}-\d{2}-\d{2}/) + return if @publish_date == 'exit' + + puts 'Invalid date format. Please try again.' + end + @books << Book.new(publisher, cover_state, @publish_date) + + puts 'Book successfully added.' + end +end From e5711ed371c05a01865d4e624c4e5c89d5f5b9ad Mon Sep 17 00:00:00 2001 From: George Date: Wed, 20 Dec 2023 13:31:31 +0400 Subject: [PATCH 2/7] Refactor menu to use App instance --- main.rb | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/main.rb b/main.rb index fd65007..059e935 100644 --- a/main.rb +++ b/main.rb @@ -1,6 +1,10 @@ -puts 'Welcome to your Catalog of things' +require_relative 'app' class Menu + def initialize(app) + @app = app + end + def display_menu puts '===== Library Menu =====' puts '1. List all books' @@ -23,38 +27,41 @@ def display_menu def run_options(choice) case choice when 1 - list_all_books + @app.list_all_books when 2 - list_all_music_albums + @app.list_all_music_albums when 3 - list_all_movies + @app.list_all_movies when 4 - list_all_games + @app.list_all_games when 5 - list_all_genres + @app.list_all_genres when 6 - list_all_labels + @app.list_all_labels when 7 - list_all_authors + @app.list_all_authors when 8 - list_all_sources + @app.list_all_sources when 9 - add_book + @app.add_book when 10 - add_music_album + @app.add_music_album when 11 - add_movie + @app.add_movie when 12 - add_game + @app.add_game else - puts 'Invalid option.Please try again.' + puts 'Invalid option. Please try again.' end end # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/CyclomaticComplexity end -menu = Menu.new +app_instance = App.new +menu = Menu.new(app_instance) + +puts 'Welcome to your Catalog of things' loop do menu.display_menu From c1f947bb8edc9595b71c51f5382828a87948e4d9 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 20 Dec 2023 16:06:14 +0400 Subject: [PATCH 3/7] Add label and author to add book method --- app.rb | 46 ++++++++++++++++++++++++++++++++++++++++++---- classes/author.rb | 4 ++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app.rb b/app.rb index f35891e..1bc2edd 100644 --- a/app.rb +++ b/app.rb @@ -1,11 +1,16 @@ require './classes/item' require './classes/book' require './classes/label' +require './classes/game' +require './classes/movie' +require './classes/source' +require './classes/author' class App def initialize @books = [] @labels = [] + @authors = [] end # list methods @@ -15,7 +20,8 @@ def list_all_books puts 'No books found.' else @books.each do |book| - puts "ID: #{book.id}, Author: #{book.author}, Publisher: #{book.publisher}, Cover state: #{book.cover_state}" + puts "ID: #{book.id}, Author: #{book.author.first_name}, Publisher: #{book.publisher}, + Cover state: #{book.cover_state}" end end end @@ -36,7 +42,14 @@ def add_book puts 'Published by:' publisher = gets.chomp.to_s puts 'Cover state (good or bad):' - cover_state = gets.chomp.to_s + loop do + @cover_state = gets.chomp.to_s + break if %w[good bad].include?(@cover_state) + return if @cover_state == 'exit' + + puts 'Invalid cover state. Please try again or type exit to return to the main menu.' + end + puts 'Published date (YYYY-MM-DD):' loop do @publish_date = gets.chomp @@ -45,8 +58,33 @@ def add_book puts 'Invalid date format. Please try again.' end - @books << Book.new(publisher, cover_state, @publish_date) - puts 'Book successfully added.' + book = Book.new(publisher, @cover_state, @publish_date) + choose_label(book) + + @books << book + end + + private + + def choose_label(item) + puts 'Label title:' + title = gets.chomp.to_s + puts 'Label color:' + color = gets.chomp.to_s + label = @labels.find { |l| l.title == title } || Label.new(title, color) + @labels << label unless @labels.include?(label) + item.label = label + end + + def choose_author(item) + puts 'Authors first name:' + first_name = gets.chomp.to_s + puts 'Authors last name:' + last_name = gets.chomp.to_s + author = @authors.find { |a| a.first_name == first_name && a.last_name == last_name } || + Author.new(first_name: first_name, last_name: last_name) + @authors << author unless @authors.include?(author) + item.author = author end end diff --git a/classes/author.rb b/classes/author.rb index 66d50f0..8edd317 100644 --- a/classes/author.rb +++ b/classes/author.rb @@ -3,8 +3,8 @@ class Author attr_accessor :id, :first_name, :last_name, :items - def initialize(id:, first_name:, last_name:) - @id = id + def initialize(first_name:, last_name:, id: nil) + @id = id || Random.rand(1..1000) @first_name = first_name @last_name = last_name @items = [] From 630491b659d3800a77b91fca85634e56b96f3c3b Mon Sep 17 00:00:00 2001 From: George Date: Wed, 20 Dec 2023 16:08:03 +0400 Subject: [PATCH 4/7] Remove movie class import until peer corrects it --- app.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app.rb b/app.rb index 1bc2edd..8563bc6 100644 --- a/app.rb +++ b/app.rb @@ -2,7 +2,6 @@ require './classes/book' require './classes/label' require './classes/game' -require './classes/movie' require './classes/source' require './classes/author' From 8b9d7af7bf559b5677eda7210c0447ff0d69e442 Mon Sep 17 00:00:00 2001 From: Fombi-Favour Date: Wed, 20 Dec 2023 14:00:30 +0100 Subject: [PATCH 5/7] add genre.rb --- classes/genre.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 classes/genre.rb diff --git a/classes/genre.rb b/classes/genre.rb new file mode 100644 index 0000000..6c73765 --- /dev/null +++ b/classes/genre.rb @@ -0,0 +1,18 @@ +require './classes/item' + +class Genre < Item + attr_accessor :name + attr_reader :id, :items + + def initialize(name, id = nil) + super + @id = id || Random.random(1..1000) + @name = name + @items = [] + end + + def add_item(item) + @items << item + item.label = self + end +end From 2376719c486de51497b19e33bad409732da633d9 Mon Sep 17 00:00:00 2001 From: Fombi-Favour Date: Wed, 20 Dec 2023 14:00:49 +0100 Subject: [PATCH 6/7] add musicalbum.rb --- classes/musicalbum.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 classes/musicalbum.rb diff --git a/classes/musicalbum.rb b/classes/musicalbum.rb new file mode 100644 index 0000000..39caae3 --- /dev/null +++ b/classes/musicalbum.rb @@ -0,0 +1,14 @@ +require './classes/item' + +class MusicAlbum < Item + attr_accessor :on_spotify + + def initialize(genre, author, source, label, publish_date, on_spotify) + super(genre, author, source, label, publish_date) + @on_spotify = on_spotify + end + + def can_be_archived? + super || @on_spotify + end +end From b22e1403a7da1a983db82478a71621ea5ea4296e Mon Sep 17 00:00:00 2001 From: George Date: Wed, 20 Dec 2023 17:32:17 +0400 Subject: [PATCH 7/7] Add choose_author method call to create a book --- app.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app.rb b/app.rb index 8563bc6..36b7cb7 100644 --- a/app.rb +++ b/app.rb @@ -60,6 +60,7 @@ def add_book book = Book.new(publisher, @cover_state, @publish_date) choose_label(book) + choose_author(book) @books << book end