Skip to content

Commit

Permalink
Merge pull request #26 from tafaramafemba/book-label
Browse files Browse the repository at this point in the history
Book, Label and methods
  • Loading branch information
tafaramafemba committed Jun 29, 2022
2 parents 3a56176 + 3f355d7 commit 848dbea
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 26 deletions.
34 changes: 34 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative './classes/book'

class App
attr_reader :all_books

def initialize
@all_books = []
end

def list_books
puts "\nAll books"

if @all_books.length.zero?
puts 'Book list is empty. Choose option (7) to add a book'
else
@all_books.each do |book|
puts "Publication Date: #{book.published_date}, Publisher: #{book.publisher}, State: #{book.cover_state}"
end
end
end

def add_book
puts "\nAdd a book"
print 'Date of publication [yyyy-mm-dd]: '
published_date = gets.chomp
print 'Publisher: '
publisher = gets.chomp
print "Cover state (Enter 'good' or 'bad'): "
cover_state = gets.chomp
new_book = Book.new(published_date, publisher, cover_state)
@all_books.push(new_book)
puts 'Book added successfully!'
end
end
17 changes: 17 additions & 0 deletions classes/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'item'

class Book < Item
attr_reader :publisher, :cover_state
attr_accessor :published_date

def initialize(published_date, publisher, cover_state)
super(id, published_date)
@id = rand(1..1000)
@publisher = publisher
@cover_state = cover_state
end

def can_be_archived?
super || @cover_state == 'bad'
end
end
2 changes: 1 addition & 1 deletion classes/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def move_to_archive

def add_author(author)
@author = author
author.add_item << self unless author.add_item.include?(self)
author.items << self unless author.add_item.include?(self)
end

def add_genre(genre)
Expand Down
8 changes: 4 additions & 4 deletions classes/label.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Label
attr_accessor :title, :color, :items
attr_reader :id
attr_reader :id, :items
attr_accessor :title, :color

def initialize(id, title, color)
@id = id
Expand All @@ -10,7 +10,7 @@ def initialize(id, title, color)
end

def add_item(item)
@items.push(item)
item.add_label = self
@item = item
item.label << self
end
end
Empty file added data/books_data.json
Empty file.
8 changes: 8 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative 'menu'

def main
puts "\nWelcome to your catalogue!"
menu
end

main
41 changes: 41 additions & 0 deletions menu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative 'app'

def app_options
puts "\nPlease choose an option to continue: "
puts '1 - List all books'
puts '2 - List all music albums'
puts '3 - List of games'
puts '4 - List all genres'
puts '5 - List all labels'
puts '6 - List all authors'
puts '7 - Add a book'
puts '8 - Add a music album'
puts '9 - Add a game'
puts '10 - Exit'
puts
end

def menu # rubocop:disable Metrics/CyclomaticComplexity
app = App.new

loop do
app_options
option = gets.chomp.to_i

case option
when 1 then app.list_books
when 2 then app.list_albums
when 3 then app.list_games
when 4 then app.list_genres
when 5 then app.list_labels
when 6 then app.list_authors
when 7 then app.add_book
when 8 then app.add_album
when 9 then app.add_game
when 10
abort("Have a nice day \n\n")
else
puts 'Invalid selection'
end
end
end
35 changes: 35 additions & 0 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative '../classes/book'

describe Book do
context 'Testing Book class' do
before :each do
@book1 = Book.new('2010-03-14', 'Cambridge', 'good')
@book2 = Book.new('2020-01-24', 'Oxford', 'good')
@book3 = Book.new('2018-10-06', 'Penguin', 'bad')
end

it 'returns child of Item' do
expect(@book1).to be_kind_of(Item)
end

it 'returns instance of Book' do
expect(@book1).to be_instance_of(Book)
end

it 'returns published date' do
expect(@book1.published_date).to eq Date.parse('2010-03-14')
end

it 'checks if book can be achived' do
expect(@book1.can_be_archived?).to eq true
end

it 'checks if book can be achived' do
expect(@book2.can_be_archived?).to eq false
end

it 'checks if book can be achived' do
expect(@book3.can_be_archived?).to eq true
end
end
end
21 changes: 0 additions & 21 deletions spec/label_spec.rb

This file was deleted.

0 comments on commit 848dbea

Please sign in to comment.