Skip to content

Commit

Permalink
Merge pull request #3 from NitBravoA92/setup-associations
Browse files Browse the repository at this point in the history
Feature/Setup-associations
  • Loading branch information
NitBravoA92 authored Aug 31, 2023
2 parents 94574e0 + bb1f29b commit 7d98bbe
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 18 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,29 @@ Run the following command inside the project folder:
This will display the following output:

```
true
"James, age: 19, classroom: 10"
"¯(ツ)/¯"
true
"Henry, age: 40, specialization: Data Science"
true
"maximilianus"
"Maximilianus"
"Maximilian"
true
"Henry, age: 40, specialization: Data Science"
true
"James, age: 19, classroom: Biology"
"¯(ツ)/¯"
true
"maximilianus"
"Maximilianus"
"Maximilian"
"Jack"
"Jack rents the Book: The Gene: An Intimate History on 2023-08-30"
```

This is just a test code. You can open the `run.rb` file using the code editor of your choice and change the input parameters in the creation of objects with Person, Student and Teacher classes:
This is just a test code. You can open the `run.rb` file using the code editor of your choice and change the input parameters in the creation of objects with Classroom, Person, Student, Teacher, Book and Rental classes:

```
classroom1 = Classroom.new('Biology')
person1 = Person.new(15, 'Rick', parent_permission: true)
student1 = Student.new(10, 19, 'James', parent_permission: false)
student1 = Student.new(classroom1, 19, 'James', parent_permission: false)
teacher1 = Teacher.new('Data Science', 40, 'Henry')
book1 = Book.new('The Gene: An Intimate History', 'Siddhartha Mukherjee')
person3 = Person.new(29, 'Jack')
rent_book1 = Rental.new(book1, person3, '2023-08-30')
```

### Run tests
Expand Down Expand Up @@ -170,6 +176,7 @@ teacher1 = Teacher.new('Data Science', 40, 'Henry')
Upcoming improvements:

- [x] Implement the "Decorator" design pattern to extends the functionalities of the classes
- [x] Implement the association relationships
- [ ] Add basic UI to the program
- [ ] Preserve data
- [ ] Create unit tests
Expand Down
14 changes: 14 additions & 0 deletions classes/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Book
attr_accessor :title, :author, :rentals

def initialize(title, author)
@title = title
@author = author
@rentals = []
end

def new_rental(rental)
@rentals.push(rental)
rental.book = self
end
end
13 changes: 13 additions & 0 deletions classes/classroom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Classroom
attr_accessor :label, :students

def initialize(label)
@label = label
@students = []
end

def enter_student(student)
@students.push(student) unless @students.include?(student)
student.classroom = self
end
end
8 changes: 7 additions & 1 deletion classes/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ def initialize(age, name = 'Unknown', parent_permission: true)
@age = age
@name = name
@parent_permission = parent_permission
@rentals = []
end

attr_reader :id
attr_accessor :name, :age
attr_accessor :name, :age, :rentals

private

Expand All @@ -27,4 +28,9 @@ def can_use_services?
def correct_name
@name
end

def new_rental(rental)
@rentals.push(rental)
rental.person = self
end
end
11 changes: 11 additions & 0 deletions classes/rental.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Rental
attr_accessor :date, :book, :person

def initialize(book, person, date)
@date = date
@book = book
@person = person
book.new_rental(self)
person.new_rental(self)
end
end
1 change: 1 addition & 0 deletions classes/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Student < Person
def initialize(classroom, age, name = 'Unknown', parent_permission: true)
super(age, name, parent_permission: parent_permission)
@classroom = classroom
classroom.enter_student(self) # enter the student in the classroom when an student object is created
end

def play_hooky
Expand Down
29 changes: 23 additions & 6 deletions run.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
require './classes/person'
require './classes/student'
require './classes/classroom'
require './classes/teacher'
require './classes/capitalize_decorator'
require './classes/trimmer_decorator'
require './classes/book'
require './classes/rental'

# create an object with the data of the person: Rick
person1 = Person.new(15, 'Rick', parent_permission: true)
p person1.can_use_services?

# create an object with the data of the student: James
student1 = Student.new(10, 19, 'James', parent_permission: false)
p "#{student1.name}, age: #{student1.age}, classroom: #{student1.classroom}"
p student1.play_hooky
p student1.can_use_services?

# create an object with the data of the teacher: Henry
teacher1 = Teacher.new('Data Science', 40, 'Henry')
p "#{teacher1.name}, age: #{teacher1.age}, specialization: #{teacher1.specialization}"
p teacher1.can_use_services?

# Create an object with the data of the Classroom: 'Biology'
classroom1 = Classroom.new('Biology')

# create an object with the data of the student: James and enter this student into the Classroom: 'Biology'
student1 = Student.new(classroom1, 19, 'James', parent_permission: false)

p "#{student1.name}, age: #{student1.age}, classroom: #{student1.classroom.label}"
p student1.play_hooky
p student1.can_use_services?

# Test the Decorator design pattern implementation
person2 = Person.new(22, 'maximilianus')
p person2.correct_name
capitalized_person2 = CapitalizeDecorator.new(person2)
p capitalized_person2.correct_name
capitalized_trimmed_person2 = TrimmerDecorator.new(capitalized_person2)
p capitalized_trimmed_person2.correct_name

# create an object with the data of the Book: The Gene: An Intimate History
book1 = Book.new('The Gene: An Intimate History', 'Siddhartha Mukherjee')

person3 = Person.new(29, 'Jack')
p person3.correct_name

# Jack rents the Book: The Gene: An Intimate History on August 30, 2023
rent_book1 = Rental.new(book1, person3, '2023-08-30')
p "#{rent_book1.person.correct_name} rents the Book: #{rent_book1.book.title} on #{rent_book1.date}"

0 comments on commit 7d98bbe

Please sign in to comment.