generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrental_options.rb
86 lines (73 loc) · 2.56 KB
/
rental_options.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
require_relative 'person_options'
require_relative 'book_options'
require_relative 'rental'
require_relative 'people_enumerable'
require_relative 'books_enumerable'
require_relative 'student'
class RentalOptions
attr_accessor :rentals, :rentals_objects
def initialize(books, people)
@rentals = []
@books = books
@people = people
@rentals_objects = []
end
def fill_rentals
@rentals_objects.each do |rental|
book = Book.new(rental['title'], rental['author'])
person = if rental['type'] == 'student'
Student.new(rental['classroom'], rental['age'], rental['name'], rental['id'],
parent_permission: rental['parent_permission'])
else
Teacher.new(rental['specialization'], rental['age'], rental['name'], rental['id'])
end
new_rental = Rental.new(rental['date'], book, person)
@rentals.push(new_rental)
end
end
def rental_to_object(rental)
rental_object = {
date: rental.date,
title: rental.book.title,
author: rental.book.author,
name: rental.person.name,
age: rental.person.age,
id: rental.person.id
}
if rental.person.is_a?(Student)
rental_object['classroom'] = rental.person.classroom
rental_object['parent_permission'] = rental.person.parent_permission
else
rental_object['specialization'] = rental.person.specialization
rental_object['parent_permission'] = 'Y'
end
rental_object
end
def create_a_rental
puts "\nPlease, select a book from the following list by number: "
BooksEnumerable.new(@books).enumerate_all_books
selected_book = gets.chomp.to_i
puts "\nSelect a person from the following list by number (not id)"
PeopleEnumerable.new(@people).enumerate_all_people
selected_person = gets.chomp.to_i
print 'Date(YYYY-MM-DD): '
date = gets.chomp
create_rental(selected_book, selected_person, date)
end
def rental_by_id
print 'ID of person: '
person_id = gets.chomp.to_i
filtered_rentals = @rentals.filter { |rental| rental.person.id == person_id }
puts 'Rentals: '
filtered_rentals.each { |rental| puts "Date: #{rental.date}, Book #{rental.book.title} by #{rental.book.author}" }
end
private
def create_rental(book_index, person_index, date)
book = @books.books[book_index]
person = @people.people[person_index]
new_rental = Rental.new(date, book, person)
@rentals.push(new_rental)
@rentals_objects.push(rental_to_object(new_rental))
puts 'Rental created successfully'
end
end