generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
100 lines (87 loc) · 2.32 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
require_relative 'book_options'
require_relative 'person_options'
require_relative 'rental_options'
require_relative 'storage'
require 'json'
class App
def initialize(options)
@options = options
@book_options = BookOptions.new
@person_options = PersonOptions.new
@rental_options = RentalOptions.new(@book_options, @person_options)
@storage = Storage.new('json', './db/')
@book_options.books_objects = @storage.load_data('books')
@person_options.people_objects = @storage.load_data('people')
@rental_options.rentals_objects = @storage.load_data('rentals')
@person_options.fill_people if @person_options.people.empty?
@book_options.fill_books if @book_options.books.empty?
@rental_options.fill_rentals if @rental_options.rentals.empty?
end
def store_json_data()
books_objects = @book_options.books_objects
person_objects = @person_options.people_objects
rentals_objects = @rental_options.rentals_objects
@storage.save_data('books', books_objects)
@storage.save_data('people', person_objects)
@storage.save_data('rentals', rentals_objects)
end
def check_cases_first(user_response)
case user_response
when 1
list_all_books
when 2
list_all_people
when 3
create_person
else
check_cases_second(user_response)
end
end
def check_cases_second(user_response)
case user_response
when 4
create_book
when 5
create_rental
when 6
rental_by_id
when 7
store_json_data
else
puts 'Please add a valid number'
end
end
def select_option(user_response)
check_cases_first(user_response)
end
private
def list_all_books
@book_options.list_all_books
end
def list_all_people
@person_options.list_all_people
end
def create_person
print 'Do you want to create a new student (1) or a teacher (2)? [Input the number]: '
select_person = gets.chomp.to_i
case select_person
when 1
@person_options.create_a_student
when 2
@person_options.create_a_teacher
end
end
def create_book
print 'Title: '
title = gets.chomp
print 'Author: '
author = gets.chomp
@book_options.create_new_book(title, author)
end
def create_rental
@rental_options.create_a_rental
end
def rental_by_id
@rental_options.rental_by_id
end
end