-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
139 lines (115 loc) · 3.16 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
require 'json'
require_relative 'person'
require_relative 'student'
require_relative 'rental'
require_relative 'book'
require_relative 'teacher'
require_relative 'classroom'
require_relative 'data_store'
class App
include DataStore
def initialize
@books = load_books
@people = load_people
@rentals = load_rentals
end
def get_num(option)
case option
when '1'
list_all_books
when '2'
list_all_people
when '3'
create_person
when '4'
create_book
when '5'
create_rental
when '6'
list_rentals
else
puts 'Please enter a number between 1 and 7'
end
end
def list_all_books
puts 'There are no books yet! Kindly add books.' if @books.empty?
@books.each { |book| puts "Title: #{book.title}, Author: #{book.author}" }
sleep 0.75
end
def list_all_people
puts 'There are no people yet! Kindly add a student or teacher.' if @people.empty?
@people.map { |person| puts "[#{person.class}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}" }
sleep 0.75
end
def create_person
print 'Do you want to create a student (1) or teacher (2) [Input a number]: '
option = gets.chomp
case option
when '1'
create_student
when '2'
create_teacher
else
puts 'Invalid input. Kindly type 1 or 2'
end
end
def create_student
print 'Age: '
age = gets.chomp
print 'Name: '
name = gets.chomp
print 'Has parent permission? [Y/N]: '
parent_permission = gets.chomp.downcase == 'y'
student = Student.new(age: age, name: name, parent_permission: parent_permission, classroom: @classroom)
@people << student
puts 'Student created successfully'
sleep 0.75
end
def create_teacher
print 'Age: '
age = gets.chomp.to_i
print 'Name: '
name = gets.chomp
print 'Specialization: '
specialization = gets.chomp
teacher = Teacher.new(age: age, name: name, specialization: specialization)
@people << teacher
puts 'Teacher created successfully'
sleep 0.75
end
def create_book
print 'Title: '
title = gets.chomp
print 'Author: '
author = gets.chomp
book = Book.new(title, author)
@books << book
puts 'Book created successfully'
end
def create_rental
puts 'Select a book from the following list by number'
@books.each_with_index { |book, index| puts "#{index}) Title: #{book.title}, Author: #{book.author}" }
book_i = gets.chomp.to_i
puts
puts 'Select a person from the following list by number (not ID)'
@people.each_with_index do |person, index|
puts "#{index}) [#{person.class}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}"
end
person_i = gets.chomp.to_i
puts
print 'Date: '
date = gets.chomp
rental = Rental.new(date, @books[book_i], @people[person_i])
@rentals.push(rental)
puts 'Rental created successfully'
end
def list_rentals
print 'ID of person: '
id = gets.chomp
puts 'Rentals:'
@rentals.each do |rental|
puts "Date: #{rental.date}, Book '#{rental.book.title}' by #{rental.book.author}" if rental.person.id == id.to_i
end
sleep 0.75
end
end