-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
141 lines (126 loc) · 3.46 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
require './app'
require './app_funcs/data_processing'
class Main
include ProcessData
def initialize
@people = populate_people
@books = populate_books
@rentals = populate_rentals(@people, @books)
end
def get_user_input(messages)
inputs = []
messages.each do |msg|
print "#{msg}: "
inputs.push(gets.chomp)
end
inputs
end
def student_permission
permission = get_user_input(['Has parent permission to? [Y/N]'])[0].downcase
case permission
when 'y', 'yes'
permit = true
when 'n', 'no'
permit = false
else
puts '=> Invalid input for permission!'
return student_permission
end
permit
end
def person_creation
person_type_msg = 'Do you want to create a student (1) or a teacher (2)? [Input the number]'
person_type, name, age = get_user_input([person_type_msg, 'Name', 'Age'])
person_details = { name: name, age: age, stored_people: @people }
case person_type.to_i
when 1
permission = student_permission
App.new.create_person(person_details, 'student', permission)
when 2
specialization = get_user_input(['Specialization'])[0]
App.new.create_person(person_details, 'teacher', specialization)
else
puts 'Invalid input for person type!'
person_creation
end
end
def book_creation
title, author = get_user_input(%w[Title Author])
App.new.create_book(title, author, @books)
end
def rental_inputs
puts 'Select a book from the following list by number'
@books.map.with_index { |book, index| puts "#{index}) Title: #{book.title}, Author: #{book.author}" }
book_index = gets.chomp.to_i
puts ''
puts 'Select a person from the following list by number'
@people.map.with_index do |person, index|
puts "#{index}) [#{person.class}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}"
end
person_index = gets.chomp.to_i
puts ''
date = get_user_input(['Date'])[0]
[book_index, person_index, date]
end
def rental_creation
if @books.empty? & @people.empty?
puts 'Create book and person first'
elsif @books.empty?
puts 'Create book first'
elsif @people.empty?
puts 'Create person first'
else
book_index, person_index, date = rental_inputs
data = {
people: @people, books: @books,
person_index: person_index, book_index: book_index,
person: @people[person_index], book: @books[book_index], date: date
}
App.new.create_rental(data)
end
end
def rental_listing
person_id = get_user_input(['ID of person'])[0]
App.new.list_rentals(person_id, @people)
end
def options
puts ''
puts 'Please choose an option by entering a number
1 - List all books
2 - List all people
3 - Create a person
4 - Create a book
5 - Create a rental
6 - List all rentals for a given person id
7 - Exit'
end
def select_option(option)
case option
when '1'
App.new.list_all_books(@books)
when '2'
App.new.list_all_people(@people)
when '3'
person_creation
when '4'
book_creation
when '5'
rental_creation
when '6'
rental_listing
when '7'
puts ''
puts '=> Thank you for using this app!'
else
puts '=> Invalid option!'
end
end
def enter_console
options
option = gets.chomp
select_option(option)
enter_console if option != '7'
end
end
puts 'Welcome to School Library App'
Main.new.enter_console