generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
148 lines (120 loc) · 3.3 KB
/
app.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
142
143
144
145
146
147
148
require './student'
require './teacher'
require './book'
require './rental'
class App
attr_reader :books, :people, :rentals
def initialize
@books = []
@people = []
@rentals = []
end
def new_person
print 'Do you want to create a student (1) or a teacher (2)? [Input the number]: '
person_type = gets.chomp
case person_type
when '1'
new_student
when '2'
new_teacher
else
puts 'Invalid input'
new_person
end
end
def permission?
print 'Has parent permission? [Y/N]: '
parent_permission = gets.chomp.downcase
if parent_permission == 'y'
true
elsif parent_permission == 'n'
false
else
puts 'Invalid input'
permission?
end
end
def new_student
print 'Age: '
age = gets.chomp
print 'Name: '
name = gets.chomp
parent_permission = permission?
@people << Student.new(nil, age, name, parent_permission)
puts 'Student created successfully!'
end
def new_teacher
print 'Age: '
age = gets.chomp
print 'Name: '
name = gets.chomp
print 'Specialization: '
specialization = gets.chomp
@people << Teacher.new(specialization, age, name)
puts 'Teacher created successfully!'
end
def new_book
print 'Title: '
title = gets.chomp
print 'Author: '
author = gets.chomp
@books << Book.new(title, author)
puts 'Book created successfully'
end
def list_books
return puts 'No books available!' if @books.empty?
@books.each_with_index do |book, index|
puts "#{index + 1}) - Title: #{book.title}, Author: #{book.author}"
end
end
def list_people
return puts 'No people available!' if @people.empty?
@people.each_with_index do |person, index|
puts "#{index + 1}) - [#{person.class}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}"
end
end
def new_rental
if @books.empty?
puts 'Book array is empty'
elsif @people.empty?
puts 'Person array is empty'
else
rental_book = select_book
rental_person = select_person
date = the_rental_date
rental = Rental.new(date, @books[rental_book], @people[rental_person])
@rentals << rental
puts 'Rental created successfully'
end
end
def select_book
puts 'Select a book from the following list by number:'
@books.each_with_index do |book, index|
puts "#{index} Title: \"#{book.title}\", Author: #{book.author}"
end
gets.chomp.to_i
end
def select_person
puts 'Select a person from the following list by number (not id):'
@people.each_with_index do |person, index|
puts "#{index} Name: #{person.name}, ID: #{person.id}, Age: #{person.age}"
end
gets.chomp.to_i
end
def the_rental_date
print 'Enter rental date: '
gets.chomp
end
def list_rentals
puts 'Enter ID of person: '
list_people
person_id = gets.chomp.to_i
person_rentals = @rentals.select { |rental| rental.person.id == person_id }
return puts 'No rentals found for this ID' if person_rentals.empty?
person_rentals.each_with_index do |rental, index|
the_rental = "Rental #{index + 1} - Book: #{rental.book.title}"
renter = "#{rental.book.author}, Person: #{rental.person.name}, Date: #{rental.date}"
puts "#{the_rental} by #{renter}"
end
end
end