-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.rb
More file actions
executable file
·143 lines (131 loc) · 3.42 KB
/
app.rb
File metadata and controls
executable file
·143 lines (131 loc) · 3.42 KB
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
require_relative 'book'
require_relative 'student'
require_relative 'teacher'
require_relative 'rentals'
require_relative 'stored'
require 'json'
module Create
def create_a_person
puts 'Do you want to create a student (1) or teacher (2)'
x = gets.chomp.to_i
case x
when 1
create_student
when 2
create_teacher
end
end
def create_student
puts 'classroom:'
clsroom = gets.chomp
puts 'Age:'
age = gets.chomp
puts 'Name:'
name = gets.chomp
puts 'permission:'
parents_permission = gets.chomp
std = Student.new(clsroom, age, name, parents_permission, id: Random.rand(1..1000))
@people.push(std)
person_string
end
def create_teacher
puts 'specialization:'
specilaze = gets.chomp
puts 'Age:'
age = gets.chomp
puts 'Name:'
name = gets.chomp
teach = Teacher.new(specilaze, age, name, id: Random.rand(1..1000))
@people.push(teach)
person_string
end
def person_string
jsonarray = []
@people.each do |item|
if item.instance_of?(Student)
jsonarray.push({ classroom: item.classroom, age: item.age, name: item.name,
parents_permission: item.parents_permission, id: item.id })
else
jsonarray.push({ age: item.age, name: item.name, id: item.id })
end
end
json = JSON.generate(jsonarray)
File.write('people.json', json)
end
def list_all_people
if @people.empty?
puts 'There is no people'
else
puts @people.first.id
@people.each { |p| puts "Name:#{p.name} Age:#{p.age} Class:#{p.class} ID:#{p.id}" }
end
end
end
module Books
def list_all_books
if @book.empty?
puts 'There is no book'
else
@book.each { |b| puts "#{b.title} written by #{b.author}" }
end
end
def create_a_book
puts 'Title:'
tit = gets.chomp
puts 'Author:'
auth = gets.chomp
bo = Book.new(tit, auth)
@book.push(bo)
jsonarray = []
@book.each { |item| jsonarray.push({ title: item.title, author: item.author }) }
json = JSON.generate(jsonarray)
File.write('book.json', json)
end
end
class App
def initialize
@book = []
@people = []
@rental = []
list_all_stored_books
list_all_stored_people
list_all_stored_rentals
end
include Create
include Books
def create_a_rental
puts 'date(yyyy/dd/mm)'
date = gets.chomp
puts 'Select a book:'
@book.each_with_index { |b, i| puts "#{i}) #{b.title} written by #{b.author}" }
bookid = gets.chomp.to_i
book = @book[bookid]
puts 'Select a person:'
@people.each_with_index { |p, i| puts "#{i}) #{p.name}" }
personid = gets.chomp.to_i
person = @people[personid]
rental = Rental.new(date, book, person)
@rental.push(rental)
store_all_rentals
end
def store_all_rentals
jsonarray = []
@rental.each do |item|
jsonarray.push({ date: item.date,
book: { title: item.book.title, author: item.book.author },
person: { id: item.person.id, name: item.person.name, age: item.person.age } })
end
File.write('rental.json', JSON.generate(jsonarray))
end
def list_all_rentals
if @rental.empty?
puts 'There are no rentals'
else
puts 'Please type person id'
id = gets.chomp.to_i
@rental.each do |r|
puts "Date: #{r.date}, Book:#{r.book.title} by #{r.book.author}" if r.person.id == id
end
end
end
end