Created
July 22, 2018 23:38
-
-
Save OtavioHenrique/ba43a1bf0b78da8c5602ce481179e439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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