-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfruby_public_library.rb
76 lines (63 loc) · 1.22 KB
/
cfruby_public_library.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Library
attr_reader :shelf, :books
def initialize
@shelf = []
@books = []
end
def report_all
puts "Currently in the library we have #{Book.total} books."
end
def add_shelf(shelf)
@shelf.push(self)
end
end
class Shelf
attr_reader :books
def initialize(library)
library.add_shelf(self)
@books = []
end
def check_out(book)
@books.delete(book)
$count += -1
end
def return(book)
@books.push(book)
end
end
class Book
attr_reader :shelf, :title
$count = 0
def initialize(title, library)
@title = title
@library = library
$count += 1
end
def enshelf(shelf)
@shelf = shelf
shelf.return(self)
puts "Thanks for returning your book!"
end
def unshelf
@shelf.check_out(self)
puts "Checking out #{title}..."
end
def self.total
$count
end
end
# COMMANDS =====
Utah = Library.new
Fantasy = Shelf.new(Utah)
Programming = Shelf.new(Utah)
Biography = Shelf.new(Utah)
dragons = Book.new("Dragons!", Utah)
rubywow = Book.new("Ruby!Wow!", Utah)
george = Book.new("The Life of George", Utah)
dragons.enshelf(Fantasy)
george.enshelf(Biography)
rubywow.enshelf(Programming)
Utah.report_all
dragons.unshelf
rubywow.unshelf
Utah.report_all