Guest User

Untitled

a guest
Jul 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class Book
  2. attr_reader :name, :author, :year
  3.  
  4. def initialize(name, author, year)
  5. @name = name
  6. @author = author
  7. @year = year
  8. end
  9.  
  10. def full_description
  11. "Name: #{name}, Author: #{author}, Year: #{year}"
  12. end
  13. end
  14.  
  15. class Stock
  16. attr_reader :quantity, :maximum_quantity
  17. attr_accessor :books
  18.  
  19. def initialize(books: [], quantity: 0, maximum_quantity: 10)
  20. @books = books
  21. @quantity = quantity
  22. @maximum_quantity = maximum_quantity
  23. end
  24.  
  25. def <<(book)
  26. books << book unless crowded
  27. end
  28.  
  29. def crowded
  30. quantity == maximum_quantity
  31. end
  32.  
  33. def print_books
  34. books.each do |book|
  35. puts book.full_description
  36. end
  37. end
  38. end
  39.  
  40. book1 = Book.new("Practical Object-Oriented Design in Ruby","Sandi Metz","2012")
  41. book2 = Book.new("Ruby Under a Microscope","Pat Shaughnessy","2013")
  42.  
  43. stock = Stock.new
  44.  
  45. stock << book1
  46. stock << book2
  47.  
  48. stock.print_books
Add Comment
Please, Sign In to add comment