Books and Members
Part of the Object Oriented Programming section of Coddy's Ruby journey. Lesson 50 of 57.
In this project you build the core of a library system. A Library object receives commands, one per line, and replies to each. Over five steps it learns to add books and members, lend and take back books, search the catalogue and print a report.
ADD_BOOK 1 Dune
JOIN ada
BORROW ada 1
RETURN ada 1
SEARCH dune
REPORTThe data model uses what this section taught. Book exposes id and title read-only and borrower through an accessor, with the predicate available?. Member keeps the ids of its loans in an array. Each class lives in its own file, book.rb, member.rb and library.rb, and the library keeps both kinds of objects in hashes that only its own methods touch:
class Book
attr_reader :id, :title
attr_accessor :borrower
def initialize(id, title)
@id = id
@title = title
@borrower = nil
end
def available?
borrower.nil?
end
end
books = { 1 => Book.new(1, "Dune") }
books[1].borrower = "ada"
puts books[1].available?Output:
falsehandle(line) splits the command and branches on its first word with case. Every branch returns its replies as an array, so a command such as REPORT can reply with several lines, and return leaves early with an error reply. The title of a book can contain spaces, so it is everything after the id:
parts = "ADD_BOOK 7 The Hobbit".split
id = parts[1].to_i
title = parts[2..].join(" ")
puts "#{id}: #{title}"Output:
7: The HobbitChallenge
EasyComplete Library#handle for the first two commands:
ADD_BOOK id title: store a new available book and replyadded book 1: Dune; when the id is taken replybook 1 exists.JOIN name: add a member and replywelcome ada; when the name exists replyada is already a member.
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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Classes and Objects
Working With FilesDefining ClassesInstance VariablesMethods and selfto_s and inspectRecap - Library Card7Polymorphism and Duck Typing
PolymorphismDuck TypingAbstract Methodscase and ClassesRecap - Payment Methods2Accessors and Self
Readers and WritersCustom Settersself in MethodsIdentity and EqualityRecap - Contact List8Special Methods
Equality and hashOperator MethodsIndexing and AppendingCustom CollectionsRecap - Fractions11Project: Library Management
Books and MembersBorrowing Books3Class Methods and Variables
Class MethodsClass Instance VariablesConstantsFactory MethodsRecap - Coupon CodesPractice on your own: Online Ruby compiler