Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/store data #6

Merged
merged 9 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ AllCops:
- "Guardfile"
- "Rakefile"
- "node_modules/**/*"

Exclude:
- 'student.rb'
- 'teacher.rb'
DisplayCopNames: true

Layout/LineLength:
Expand Down
41 changes: 36 additions & 5 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
require_relative 'inputs'
require_relative 'book'
require_relative 'inputs_output'
require_relative 'student'
require_relative 'classroom'
require_relative 'teacher'
require_relative 'rental'
require 'json'

class App
include InputOutput
attr_accessor :books, :people, :rental

def initialize
@books = []
@people = []
@rental = []
@taked_book = []
@taked_people = []
@taked_rental = []
end

def list_all_books
read_file_book if @books == []
@books.each_with_index do |book, idx|
puts "\n#{idx}) Book title: #{book.title}, written by #{book.author}"
end
end

def list_all_people
read_file_people if @people == []
@people.each_with_index do |person, idx|
puts "\n #{idx}) #{person.type} id: (#{person.id}) Name: #{person.name} age: #{person.age}"
end
end

def create_person
read_file_people if @people == []
selected_person = selected_person_input
name = obtain_person_name
age = obtain_person_age

if selected_person == 1
case selected_person
when 1
create_a_student(name, age)
elsif selected_person == 2
when 2
create_a_teacher(name, age)
else
puts 'Option not recognized, please try another one'
Expand All @@ -46,10 +56,12 @@ def create_a_student(name, age)

classroom = assign_student_classroom
Classroom.new(classroom)

parent_permission = student_has_permission
permission = (parent_permission == 'Y')
@people.push(Students.new(classroom, type, age, name, parent_permission: permission))
@people.push(Students.new(classroom, type, age, name, 1, parent_permission: permission))
@taked_people.push({ classroom: classroom, type: type, age: age, name: name, id: @people.last.id,
parent_permission: permission })
write_file(@taked_people, 'people.json')
puts 'Student was created successfully'
puts "\n"
end
Expand All @@ -58,32 +70,51 @@ def create_a_teacher(name, age)
type = 'Teacher'
specialization = obtain_teacher_specialization

@people.push(Teacher.new(specialization, type, age, name))
@people.push(Teacher.new(specialization, type, age, name, 1))
@taked_people.push({ specialization: specialization, type: type, age: age, name: name, id: @people.last.id })
write_file(@taked_people, 'people.json')
puts 'Teacher was created successfully'
puts "\n"
end

def create_a_book
read_file_book if @books == []
title = obtain_book_title

author = obtain_book_author
puts 'Book was created successfully'
puts "\n"

@books.push(Book.new(title, author))
@taked_book.push({ title: title, author: author })
write_file(@taked_book, 'books.json')
end

def create_a_rental
read_file_rental if @rental == []
book = select_book_for_rental
person = select_who_will_rent

date = define_rental_date
@rental.push(Rental.new(date, @people[person], @books[book]))
puts 'Rental was created successfully'
puts "\n"
if @people[person].type == 'Teacher'
@taked_rental.push({ date: date, book: { title: @books[book].title, author: @books[book].author },
person: { name: @people[person].name, age: @people[person].age, id: @people[person].id,
type: @people[person].type, specialization: @people[person].specialization } })
else
@taked_rental.push({ date: date, book: { title: @books[book].title, author: @books[book].author },
person: { name: @people[person].name, age: @people[person].age, id: @people[person].id,
type: @people[person].type, classroom: @people[person].classroom,
permission: @people[person].parent_permission } })
end

write_file(@taked_rental, 'rentals.json')
end

def rental_person_id
read_file_rental if @rental == []
id = select_person_id_for_rentals

@rental.map do |rental|
Expand Down
1 change: 1 addition & 0 deletions books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"title":"dd","author":"ee"},{"title":"fra","author":"dses"},{"title":"frsc","author":"sd"},{"title":"fra","author":"cc"},{"title":"Lord of ring","author":"Arthur"},{"title":"King","author":"Franck"},{"title":"Gregoire","author":"Alex"}]
18 changes: 18 additions & 0 deletions hash_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module DataToHash
def student_from_hash(people_data)
classroom = people_data['classroom']
type = 'Student'
name = people_data['name']
age = people_data['age']
permission = people_data['parent_permission']
Students.new(classroom, type, age, name, parent_permission: permission)
end

def teacher_from_hash(people_data)
specialization = people_data['specialization']
type = 'Teacher'
name = people_data['name']
age = people_data['age']
Teacher.new(specialization, type, age, name)
end
end
73 changes: 73 additions & 0 deletions inputs_output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module InputOutput
def write_file(taked_book, file_name)
json_file = JSON.generate(taked_book)
File.open(file_name, 'w')
File.write(file_name, json_file)
end

def read_file_book
return [] unless File.exist?('books.json')

file_name = 'books.json'
File.open(file_name)
take_data = File.read('books.json')
@taked_book = JSON.parse(take_data)
convert_book
end

def convert_book
@taked_book.each do |i|
@books.push(Book.new(i['title'], i['author']))
end
end
# Read peoples

def read_file_people
return [] unless File.exist?('people.json')

file_name = 'people.json'
File.open(file_name)
take_data = File.read('people.json')
@taked_people = JSON.parse(take_data)
convert_people
end

def convert_people
@taked_people.each do |i|
if i['type'] == 'Teacher'
@people.push(Teacher.new(i['specialization'], i['type'], i['age'], i['name'], i['id']))
else
@people.push(Students.new(i['classroom'], i['type'], i['age'], i['name'], i['id'],
parent_permission: i['parent_permission']))
end
end
end

# Read rental in file rentals
def read_file_rental
return [] unless File.exist?('rentals.json')

file_name = 'rentals.json'
File.open(file_name)
take_data = File.read('rentals.json')
@taked_rental = JSON.parse(take_data)
convert_rental
end

def convert_rental
@taked_rental.each do |i|
if i['person']['type'] == 'Student'
@rental.push(Rental.new(i['date'],
Students.new(i['person']['classroom'], i['person']['type'],
i['person']['age'], i['person']['name'],
i['person']['id'], i['person']['permission']),
Book.new(i['book']['title'], i['book']['author'])))
else
@rental.push(Rental.new(i['date'],
Teacher.new(i['person']['specialization'], i['person']['type'],
i['person']['age'], i['person']['name'], i['person']['id']),
Book.new(i['book']['title'], i['book']['author'])))
end
end
end
end
6 changes: 5 additions & 1 deletion main.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'app'
require_relative 'manage_data'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • While running ruby main.rb I am getting the error ``require_relative': cannot load such file --what you can do is that please removerequire_relative 'manage_data'` form main.rb.

def options_menu
puts 'Please choose your menu option!'
Expand Down Expand Up @@ -34,7 +35,10 @@ def main
loop do
options_menu
choice = gets.chomp.to_i
exit if choice == 7
if choice == 7
app.save_people_data
exit
end
handle_menu_choice(choice, app)
Copy link

@roshan-bajgain roshan-bajgain Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • While exiting the app I got the error shown below please fix this error and you are good to go. the problem is app.save_people_data is undefined please make sure that `On exit from the app the data should be saved (all the data.).

Error

Screenshot 2023-09-07 145257

end
end
Expand Down
1 change: 1 addition & 0 deletions people.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"specialization":"dec","type":"Teacher","age":"24","name":"earl","id":705},{"classroom":"3","type":"Student","age":"34","name":"des","id":778,"parent_permission":true},{"specialization":"des","type":"Teacher","age":"24","name":"frav","id":69},{"classroom":"4","type":"Student","age":"33","name":"dev","id":756,"parent_permission":true}]
12 changes: 8 additions & 4 deletions person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
require_relative 'rental'

class Person < Nameable
attr_accessor :name, :age, :type
attr_reader :id, :rentals
attr_accessor :name, :age, :type, :id
attr_reader :rentals

def initialize(type, age, name = 'Unknown', parent_permission: true)
def initialize(type, age, name = 'Unknown', id = 1, parent_permission: true)
super()
@age = age
@name = name
@parent_permission = parent_permission
@id = Random.rand(1..1000)
@id = if id == 1
Random.rand(1..1000)
else
id
end
@type = type
@rentals = []
end
Expand Down
1 change: 1 addition & 0 deletions rentals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"date":"2022/1/1","book":{"title":"greg","author":"meme"},"person":{"name":"earl","age":"24","id":705,"type":"Teacher","specialization":"dec"}}]
16 changes: 13 additions & 3 deletions student.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require_relative 'person'

class Students < Person
attr_reader :classroom
attr_reader :classroom, :parent_permission

def initialize(classroom, type, age, name = 'Unknown', parent_permission: true)
super(type, age, name, parent_permission: parent_permission)
def initialize(classroom, type, age, name = 'Unknown', id = 1, parent_permission: true)
super(type, age, name, id, parent_permission: parent_permission)
@classroom = classroom
end

Expand All @@ -16,4 +16,14 @@ def assign_classroom(classroom)
@classroom = classroom
classroom.add_student(self)
end

def hash_structure
{
classroom: @classroom,
type: 'Student',
age: @age,
name: @name,
parent_permission: @parent_permission
}
end
end
13 changes: 11 additions & 2 deletions teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
class Teacher < Person
attr_reader :specialization

def initialize(specialization, type, age, name = 'Unknown', parent_permission: true)
def initialize(specialization, type, age, name = 'Unknown', id = 1, parent_permission: true)
@specialization = specialization
super(type, age, name, parent_permission: parent_permission)
super(type, age, name, id, parent_permission: parent_permission)
end

def can_use_servicies?
true
end

def hash_structure
{
specialization: @specialization,
type: 'Teacher',
age: @age,
name: @name
}
end
end
Loading