-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.rb
76 lines (64 loc) · 1.6 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
# library.rb
require_relative 'person'
require_relative 'teacher'
require_relative 'student'
require_relative 'book'
require_relative 'classroom'
require_relative 'rental'
# This class joins all the other classes together.
# It is responsible for creating and storing instances of the other classes,
class Library
def initialize
@people = []
@books = []
@classrooms = []
@rentals = []
end
def create_teacher(name, age, specialization)
teacher = Teacher.new(name, age, specialization)
@people << teacher
teacher
end
def create_student(name, age, parent_permission)
student = Student.new(name, age, parent_permission)
@people << student
student
end
def create_book(title, author)
book = Book.new(title, author)
@books << book
book
end
def create_classroom(label)
classroom = Classroom.new(label)
@classrooms << classroom
classroom
end
def create_rental(book_rent, person_rent)
person = @people.find { |p| p == person_rent }
book = @books.find { |b| b == book_rent }
if person && book
date = Time.now.strftime('%Y-%m-%d')
rental = Rental.new(date, book, person)
@rentals << rental
rental
else
puts 'Person or Book not found.'
end
end
# Add other methods to interact with the classes as needed
def list_books
@books
end
def list_people
@people
end
def list_rentals_for_person(person_id)
person = @people.find { |p| p.id == person_id }
if person
@rentals.select { |rental| rental.person == person }
else
puts 'Person not found.'
end
end
end