Menu
Coddy logo textTech

Borrowing Books

Part of the Object Oriented Programming section of Coddy's Ruby journey. Lesson 51 of 57.

challenge icon

Challenge

Easy

Add the BORROW command. Keep every command from the previous steps working exactly as before.

  • BORROW name id: check in this order and reply with the first problem: no member bo, no book 9, Dune is not available, ada has reached the limit (members may hold at most LIMIT, 2, books). Otherwise record the loan on both the book and the member and reply ada borrowed Dune.

The supplied code passes every input line to library.handle(line) and prints each returned reply on its own line. Member names are single lower-case words, book ids are whole numbers, and the input holds only the commands described so far. Any other command replies unknown command.

Your code goes in book.rb, member.rb and library.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'book'
require_relative 'member'
require_relative 'library'

# Supplied input/output code: keep it as it is
input = []
while (line = gets)
  input << line.chomp
end
library = Library.new
input.each do |line|
  library.handle(line).each { |reply| puts reply }
end

All lessons in Object Oriented Programming

Practice on your own: Online Ruby compiler