forked from rainbough/Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.rb
140 lines (121 loc) · 3.19 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class Library
def initialize
@books = {}
end
# See the books currently in the library.
def to_s
puts "This library has:"
@books.each do | name, book |
print name
print " (checked out by #{book.user})" unless book.user == nil
puts
end
# puts @books
end
# Add a book to the library.
#
# book - An instance of the Book class.
def add_book(book)
@books[book.title] = book
end
# Check out a book.
#
# book - An instance of the Book class from @books.
# user - An instance of the User class.
def check_out(book, user)
user.check_out(@books[book.title])
end
# Check in a book.
#
# book - An instance of the Book class which has been checked out.
# user - An instance of the User class.
def check_in(book, user)
user.check_in(@books[book.title])
end
end
class Book
attr_accessor :author, :title, :description, :user, :due_date
def initialize(author, title, description)
@author = author
@title = title
@description = description
end
# View book details.
def to_s
puts "\"#{@title}\" by #{@author}: #{@description}"
if @user == nil
puts "This book is available."
else
puts "#{@user} has checked this book out."
end
end
end
class User
def initialize(name)
@name = name
@checked_out_books = {}
end
# See the books currently checked out.
def to_s
puts "CHECKED OUT:"
@checked_out_books.each do | name, book |
puts "\"#{name}\" by #{book.author}"
end
end
def overdue_books?
if @checked_out_books.length > 0
@checked_out_books.each do | name, book |
return book.due_date < Time.now
end
end
end
# Check out a book.
#
# book - An instance of the book class from the library.
def check_out(book)
if @checked_out_books.length <= 2 && !overdue_books?
@checked_out_books[book.title] = book
@checked_out_books[book.title].due_date = Time.now + 604800
book.user = @name
puts "Checked out #{book.title}. It's due #{@checked_out_books[book.title].due_date.asctime}"
elsif overdue_books?
puts "You have overdue books. Please return them before checking out anything else."
else
puts "Only 2 books are allowed to be checked out at a time."
end
end
# Check in a book.
#
# book - An instance of the book class from the library.
def check_in(book)
@checked_out_books.each do | name, hash |
if book == hash
@checked_out_books.delete(name)
book.user = nil
book.due_date = nil
end
end
end
end
# ====================================================================
# For testing
# Create library
lib = Library.new
# Create books
book_1 = Book.new("First Author", "Ruby Book 1", "Description of book 1.")
book_2 = Book.new("Second Author", "Ruby Book 2", "Description of book 2.")
book_3 = Book.new("Third Author", "Ruby Book 3", "Description of book 3.")
# Create user
user_1 = User.new("User 1")
user_2 = User.new("User 2")
# Add books to library
lib.add_book(book_1)
lib.add_book(book_2)
lib.add_book(book_3)
# Check out book
lib.check_out(book_1, user_1)
lib.check_out(book_2, user_1)
# Check one back in
lib.check_in(book_1, user_1)
puts
user_1.to_s