forked from rainbough/Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.rb
141 lines (126 loc) · 3.69 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
141
#Time.now + 14*24*3600 due date is two weeks from now in seconds
class Book
attr_reader :title, :author, :description
attr_accessor :state, :users, :due_date
def initialize(title, author, description)
@title=title
@author=author
@description=description
@users={}
@state = "this book has not been added to the library"
@due_date = nil
end
def user
return "#{@users.last.first_name}, {@users.last.first_name} currently has this book"
end
def user_log
@users.each do |user|
return "#{user.first_name}, #{user.last_name} checked this book out on #{@due_date}"
end
end
def check_out(user, library)
if library.is_a?(Library) == true
library.check_out(self, user)
else
"library error"
end
end
def check_in(user, library)
if library.is_a?(Library) == true
library.check_out(self, user)
else
"library error"
end
end
def is_due
if Time.now > @due_date
@state = "over due"
"this book is over due! it was due on #{@due_date.asctime}"
else
"this book is due on #{@due_date.asctime}"
end
end
end
class User
attr_reader :first_name, :last_name
attr_accessor :books
def initialize(first_name, last_name)
@first_name = first_name
@last_name= last_name
@books = []
end
def books_log
return @books
end
end
class Library
attr_reader :checked_in, :checked_out, :over_due
def initialize()
@checked_in = []
@checked_out=[]
@over_due=[]
@today
end
def add(book)
if book.is_a?(Book) == true
@checked_in.push(book)
book.state = "checked in"
else
"not a book"
end
end
def check_log
@checked_out.each do |book|
string = "#{book.users.last.first_name} #{book.users.last.first_name} has checked out #{book.title} and it is due back on #{book.due_date}"
return string
end
end
def check_out(book, user)
case book.state
when "checked in"
# if (user.)#overdue condition
@checked_in.delete_if{|a| a==book}
book.state = "checked out"
today= Time.now
book.due_date = today + 604800
@checked_out.push(book)
book.users[:today] = user
user.books.push(book)
return"you have checked out #{book.title}! it is due back on #{(book.due_date.asctime)}"
when "checked out"
return "#{book.title} is currently checked out. It will be returned to the library on #{(book.due_date.asctime)}"
when "over due"
return "{book.title} is over due!"
else
return "That book is not in this library."
end
end
def check_in(book, user)
case book.state
when "checked out" || "over_due"
@checked_out.delete_if{|a| a==book}
book.state = "checked in"
@checked_in.push(book)
book.users.user.books.delete_if{|a| a==("#{user.first_name} #{user.last_name}")}
user.books.delete_if{|a| a==(book.title)}
book.due_date = nil
return"you have checked out #{book.title}! it is due back on #{(book.due_date.asctime)}"
when "checked in"
return "#{book.title} is currently checked in. It will be returned to the library on #{(book.due_date.asctime)}"
else
return "That book is not in this library."
end
end
end
library = Library.new
bonnie = User.new("Bonnie", "Mattson")
john = User.new("John", "Huntington")
book1 = Book.new("Matilda", "Dahl", "Smart girl hates parents")
book2 = Book.new("I, Robot", "Asimov", "Robot Emotions")
book3 = Book.new("Master and Commander", "O'Brian", "Tall Ships and cannons")
book4 =Book.new("Bleak House", "Dickens", "Melodrama")
library.add(book1)
library.add(book2)
library.add(book3)
library.add(book4)
puts "I ran"