-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
172 lines (145 loc) · 4.07 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require_relative 'person'
require_relative 'student'
require_relative 'teacher'
require_relative 'capitalize_decorator'
require_relative 'trimmer_decorator'
require_relative 'rental'
require_relative 'book'
require_relative 'preserve_data'
module Infor
@people = []
@books = []
@rentals = []
class << self
attr_accessor :rentals
end
def self.people
@people
end
def self.books
@books
end
def self.generate_person_id
@people.length
end
end
def new_teacher
puts "Enter the teacher's name:"
name = gets.chomp
puts "Enter the teacher's specialization:"
specialization = gets.chomp
puts "Enter the teacher's age:"
age = gets.chomp.to_i
teacher = Teacher.new(age, specialization, name: name)
teacher.instance_variable_set(:@id, Infor.generate_person_id)
Infor.people << teacher
file_write('people.json', Infor.people)
puts "Teacher #{name} added!"
puts ' '
end
def new_student
puts "Enter the student's name:"
name = gets.chomp
puts "Enter the student's classroom:"
classroom = gets.chomp
puts "Enter the student's age:"
age = gets.chomp.to_i
puts 'Has parent permission? [Y/N]:'
parent_permission = gets.chomp
parent_permission = true if %w[Y y].include?(parent_permission)
parent_permission = false if %w[N n].include?(parent_permission)
student = Student.new(age, classroom, name: name, parent_permission: parent_permission)
student.instance_variable_set(:@id, Infor.generate_person_id)
Infor.people << student
file_write('people.json', Infor.people)
puts "Student #{name} added!"
puts ' '
end
def create_person
puts 'Enter the type of person:'
puts '1 - Student'
puts '2 - Teacher'
person_type = gets.chomp.to_i
case person_type
when 1
new_student
when 2
new_teacher
else
puts 'Invalid option! Please choose a valid option.'
end
end
def list_people
puts 'Listing all people...'
@people = file_read('people.json')
@people.each do |person|
puts "Id: #{person['id']}, Name: #{person['name']}, Age: #{person['age']}"
end
puts ' '
end
def new_book
puts 'Enter book title:'
title = gets.chomp
puts 'Enter book author:'
author = gets.chomp
book = Book.new(title, author)
Infor.books << book
file_write('books.json', Infor.books)
puts "Book '#{title}' added successfully!"
puts ' '
end
def list_books
puts 'Listing all books...'
@books = file_read('books.json')
@books.each do |book|
puts "Title: #{book['title']}, Author: #{book['author']}"
end
puts ' '
end
def student_teacher
@people = file_read('')
@people.each do |person|
puts "#{person['id']} - Name: #{person['name']}, Age: #{person['age']}"
end
puts ' '
end
def new_rental
puts 'Choose a person from the list below by number:'
people = file_read('people.json')
people.each_with_index do |person, index|
puts "#{index} - Name: #{person['name']}, ID: #{person['id']}, Age: #{person['age']}"
end
person_index = gets.chomp.to_i
puts 'Choose a book from the list below by number:'
books = file_read('books.json')
books.each_with_index do |book, index|
puts "#{index} - Title: #{book['title']}, Author: #{book['author']}"
end
book_index = gets.chomp.to_i
puts 'Enter the rental date (YYYY-MM-DD):'
date = gets.chomp
book = books[book_index]
person = people[person_index]
Infor.rentals << Rental.new(date, book, person)
file_write('rentals.json', Infor.rentals)
puts 'Rental added successfully!'
end
def list_rentals
rentals = file_read('rentals.json')
puts 'Enter the person ID to list rentals:'
people = file_read('people.json')
people.each_with_index do |person, index|
puts "#{index} - Name: #{person['name']}, ID: #{person['id']}, Age: #{person['age']}"
end
person_id = gets.chomp.to_i
rentals_available = rentals.select { |rental| rental['person']['id'] == person_id }
if rentals_available.empty?
puts "No rentals available for the person with ID #{person_id}."
else
puts "Listing rentals for the person with ID #{person_id}:"
rentals_available.each do |rental|
puts "Rental Date: #{rental['date']}"
puts
end
end
end