diff --git a/README.md b/README.md index b410980..73cca3d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/classes/book.rb b/classes/book.rb new file mode 100644 index 0000000..fb2d21c --- /dev/null +++ b/classes/book.rb @@ -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 diff --git a/classes/classroom.rb b/classes/classroom.rb new file mode 100644 index 0000000..de43f2c --- /dev/null +++ b/classes/classroom.rb @@ -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 diff --git a/classes/person.rb b/classes/person.rb index ac17f4e..6123859 100644 --- a/classes/person.rb +++ b/classes/person.rb @@ -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 @@ -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 diff --git a/classes/rental.rb b/classes/rental.rb new file mode 100644 index 0000000..fcfeb6f --- /dev/null +++ b/classes/rental.rb @@ -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 diff --git a/classes/student.rb b/classes/student.rb index 1c7ea56..a654d72 100644 --- a/classes/student.rb +++ b/classes/student.rb @@ -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 diff --git a/run.rb b/run.rb index 8f43375..08e4ab9 100644 --- a/run.rb +++ b/run.rb @@ -1,24 +1,31 @@ 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 @@ -26,3 +33,13 @@ 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}"