Instantly share code, notes, and snippets.

Embed
What would you like to do?
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