-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from NitBravoA92/setup-associations
Feature/Setup-associations
- Loading branch information
Showing
7 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |