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

OOP school library: preserve data #6

Merged
merged 14 commits into from
May 24, 2023
11 changes: 7 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

require_relative 'modules/book_mod'
require_relative 'modules/people_mod'
require_relative 'modules/Rental_mod'
require_relative 'modules/rental_mod'

require_relative 'modules/save_data'
require_relative 'modules/read_data'

class App
attr_accessor :books, :rentals, :people

def initialize
@books = []
@rentals = []
@people = []
@books = ReadData.new.read_books
@people = ReadData.new.read_people
@rentals = ReadData.new.read_rentals(ReadData.new.read_books, ReadData.new.read_people)
end

include BookMod
Expand Down
6 changes: 6 additions & 0 deletions data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"title": "Grief child",
"author": "George"
}
]
30 changes: 30 additions & 0 deletions data/people.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id": 630,
"name": "Amen",
"age": "20",
"type": "Student",
"parent_permission": true
},
{
"id": 204,
"name": "Shafu",
"age": "30",
"type": "Teacher",
"specialization": null
},
{
"id": 163,
"name": "shafiu",
"age": "10 ",
"type": "Student",
"parent_permission": true
},
{
"id": 357,
"name": "Amen",
"age": "20",
"type": "Teacher",
"specialization": "math"
}
]
7 changes: 7 additions & 0 deletions data/rentals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"date": "02-02-20",
"people_index": 0,
"book_index": 0
}
]
1 change: 1 addition & 0 deletions modules/book_mod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def create_book
book = Book.new(title, author)
@books.push(book)
puts 'Book created successfully!'
SaveData.new.save_books(@books)
end

def list_books_using_id
Expand Down
2 changes: 2 additions & 0 deletions modules/people_mod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def create_student
student = Student.new(age, name, parent_permission)
@people.push(student)
puts 'Student added successfully!'
SaveData.new.save_people(@people)
end

def create_teacher
Expand All @@ -46,6 +47,7 @@ def create_teacher
teacher = Teacher.new(age, name, specialization)
@people.push(teacher)
puts 'Teacher added successfully!'
SaveData.new.save_people(@people)
end

def list_people_using_id
Expand Down
50 changes: 50 additions & 0 deletions modules/read_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'json'

class ReadData
def read_books
books = []
return books unless File.exist?('./data/books.json')

file = File.open('./data/books.json')
data = JSON.parse(file.read)
data.each do |book|
books << Book.new(book['title'], book['author'])
end
file.close
books
end

def read_people
people = []
return people unless File.exist?('./data/people.json')

file = File.open('./data/people.json')
data = JSON.parse(file.read)
file.close
data.each do |person|
if person['type'] == 'Student'
student = Student.new(person['age'], person['name'], person['parent_permission'])
student.id = person['id']
people << student
else
teacher = Teacher.new(person['age'], person['name'], person['parent_permission'])
teacher.id = person['id']
people << teacher
end
end
people
end

def read_rentals(books, people)
rentals = []
return rentals unless File.exist?('./data/rentals.json')

file = File.open('./data/rentals.json')
data = JSON.parse(file.read)
file.close
data.each do |rental|
rentals << Rental.new(rental['date'], books[rental['book_index']], people[rental['people_index']])
end
rentals
end
end
1 change: 1 addition & 0 deletions modules/rental_mod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def create_rental
person = @people[select_person]
rental = Rental.new(date, book, person)
@rentals.push(rental)
SaveData.new.save_rentals(@rentals, @books, @people)
puts 'Rental created successfully!'
end

Expand Down
63 changes: 63 additions & 0 deletions modules/save_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'json'
require 'fileutils'

class SaveData
def exist_file(filename)
FileUtils.mkdir_p('./data')
FileUtils.touch('./data/books.json') if !File.exist?('./data/books.json') && filename == 'books'
FileUtils.touch('./data/people.json') if !File.exist?('./data/people.json') && filename == 'people'
FileUtils.touch('./data/rentals.json') if !File.exist?('./data/rentals.json') && filename == 'rentals'
end

def save_books(books)
book_arr = []
books.each do |book|
book_arr << { title: book.title, author: book.author }
end
return if book_arr.empty?

exist_file('books')
File.write('./data/books.json', JSON.pretty_generate(book_arr))
end

def save_people(people)
people_arr = []
people.each do |person|
person_obj = {
id: person.id,
name: person.name,
age: person.age,
type: person.class.name
}
if person.instance_of?(::Teacher)
person_obj[:specialization] = person.specialization
else
person_obj[:parent_permission] = person.parent_permission
end
people_arr << person_obj
end

return if people_arr.empty?

exist_file('people')
File.write('./data/people.json', JSON.pretty_generate(people_arr))
end

def save_rentals(rentals, books, people)
rentals_arr = []
rentals.each do |rental|
rental_obj = {
date: rental.date,
people_index: people.index(rental.person),
book_index: books.index(rental.book)
}
rentals_arr << rental_obj
end

return if rentals_arr.empty?

exist_file('rentals')

File.write('./data/rentals.json', JSON.pretty_generate(rentals_arr))
end
end
1 change: 1 addition & 0 deletions modules/select.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../app'
require_relative 'read_data'

class Select
def initialize
Expand Down
7 changes: 4 additions & 3 deletions modules/student.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require_relative 'person'

class Student < Person
attr_reader :classroom
attr_reader :classroom, :parent_permission
attr_writer :id

def initialize(age, name, _parent_permission, classroom: 'Unknown')
def initialize(age, name, parent_permission, classroom: 'Unknown')
super(age, name)
@id = Random.rand(1...100)
@classroom = classroom
@parent_permission = parent_permission
end

def play_hooky
Expand Down
4 changes: 3 additions & 1 deletion modules/teacher.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require_relative 'person'

class Teacher < Person
attr_accessor :specialization
attr_writer :id

def initialize(age, name, specialization)
super(age, name)
@id = Random.rand(1...1000)
@specialization = specialization
end

Expand Down