Skip to content

Instantly share code, notes, and snippets.

@OtavioHenrique
Created July 22, 2018 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OtavioHenrique/ba43a1bf0b78da8c5602ce481179e439 to your computer and use it in GitHub Desktop.
Save OtavioHenrique/ba43a1bf0b78da8c5602ce481179e439 to your computer and use it in GitHub Desktop.
class Book
attr_reader :name, :author, :year
def initialize(name, author, year)
@name = name
@author = author
@year = year
end
def full_description
"Name: #{name}, Author: #{author}, Year: #{year}"
end
end
class Stock
attr_reader :quantity, :maximum_quantity
attr_accessor :books
def initialize(books: [], quantity: 0, maximum_quantity: 10)
@books = books
@quantity = quantity
@maximum_quantity = maximum_quantity
end
def <<(book)
books << book unless crowded
end
def crowded
quantity == maximum_quantity
end
def print_books
books.each do |book|
puts book.full_description
end
end
end
book1 = Book.new("Practical Object-Oriented Design in Ruby","Sandi Metz","2012")
book2 = Book.new("Ruby Under a Microscope","Pat Shaughnessy","2013")
stock = Stock.new
stock << book1
stock << book2
stock.print_books
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment