-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
170 lines (142 loc) · 3.64 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require_relative 'person'
require_relative 'student'
require_relative 'teacher'
require_relative 'corrector'
require_relative 'book'
require_relative 'rental'
class App
attr_accessor :people, :books
def initialize
@people = []
@books = []
@rentals = []
end
def run
puts 'Welcome to School Library App!'
sleep 1
options
end
def case_options(option)
case option
when '1'
list_books
when '2'
list_people
when '3'
create_person
when '4'
create_book
when '5'
create_rental
when '6'
list_rental_by_person_id
else
puts 'Exit'
end
end
def list_books
puts 'There are no books' if @books.empty?
@books.each { |book| puts "Title: #{book.title}, Author: #{book.author}" }
sleep 1
options
end
def list_people
puts 'There are no people' if @people.empty?
@people.each { |person| puts "[#{person.class}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}" }
sleep 1
options
end
def parent_permission?
answer = ''
until %w[y n].include?(answer)
print 'Has parent permission? [Y/N]: '
answer = gets.chomp.downcase
end
answer == 'y'
end
def choose_person
answer = ''
until %w[1 2].include?(answer)
print 'Do you want to create a student (1) or a teacher (2)? [Input the number]: '
answer = gets.chomp.downcase
end
answer
end
def specialization
answer = ''
while answer&.empty?
print 'Specialization: '
answer = gets.chomp
end
end
def create_person
option = choose_person
print 'Age: '
age = gets.chomp.to_i
print 'Name: '
name = gets.chomp
person = case option
when '1'
Student.new(age: age, name: name, parent_permission: parent_permission?, corrector: Corrector.new)
when '2'
Teacher.new(age: age, name: name, specialization: specialization, corrector: Corrector.new)
end
@people << person
puts 'Person created successfully'
sleep 1
options
end
def create_book
print 'Title: '
title = gets.chomp
print 'Author: '
author = gets.chomp
book = Book.new(title: title, author: author)
@books << book
puts 'Book created successfully'
sleep 1
options
end
def create_rental
puts 'Select a book from the following list by number'
@books.each_with_index { |book, idx| puts "#{idx}) Title: #{book.title}, Author: #{book.author}" }
book_idx = gets.chomp.to_i
puts 'Select a person from the following list by number (not id)'
@people.each_with_index do |person, idx|
puts "#{idx}) [#{person.class}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}"
end
person_idx = gets.chomp.to_i
print 'Date: '
date = gets.chomp
rental = Rental.new(date: date, book: @books[book_idx], person: @people[person_idx])
@rentals << rental
puts 'Rental created successfully'
sleep 1
options
end
def list_rental_by_person_id
print 'ID of person: '
id = gets.chomp.to_i
puts 'Rentals:'
puts
@rentals.each do |rental|
puts "Date: #{rental.date}, Book '#{rental.book.title}' by #{rental.book.author}" if rental.person.id == id
end
sleep 1
options
end
def options
puts
puts 'Please choose an option by enterin a number:'
puts '1 - List all books'
puts '2 - List all people'
puts '3 - Create a person'
puts '4 - Create a book'
puts '5 - Create a rental'
puts '6 - List all rentals for a given preson id'
puts '7 - Exit'
option = gets.chomp
case_options option
end
end
App.new.run