Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we've fleshed out our classes and objects and updated our plan of attack, let's revisit writing the `checkOut()` and `returnBook()` methods on the Patron class.
Instructions for Workspace
Your instructions are as follows:
1) Inside the Patron class, add two methods: checkOut()
and returnBook()
- The
checkOut()
method should receive one argument, a Book object. - It should update the
currentBook
property to the Book object that's passed in. - It should update that Book object's
out
property totrue
. - It should update that Book object's
patron
property to the Patron object itself - It should calculate the due date (14 days in the future) and then set the Book object's
dueDate
property to that due date.
- The
returnBook()
method should receive one argument, a Book object. - It should update the
currentBook
property tonull
- It should update that Book object's
out
property tofalse
- It should update that Book object's
patron
property tonull
- It should update that Book object's
dueDate
property tonull
[MUSIC]
0:00
Now that the book and patron classes
have been fleshed out a little more,
0:04
let's keep going with the patron
class methods we discussed earlier.
0:08
Check out and return book.
0:11
The check out method is going to
utilize all of the things we did in
0:14
this last stage.
0:16
This method will receive one argument,
a book object.
0:18
What do you think will happen next?
0:21
Well, first we should set
the patron's current book property
0:23
to the book object that was passed in.
0:26
This way,
when a library employee looks up a patron,
0:28
they will see what book, if any,
that patron has checked out.
0:31
Then we can set all the associated
properties on the book object.
0:35
Its out property should be set to true.
0:39
It's patron property should be
set to the patron object itself.
0:42
That can be accomplished by setting
the property to the this keyword.
0:46
This, when used by itself inside a class,
refers to the entire object.
0:50
Finally, the book's dueDate property
should be calculated and set.
0:55
For our purposes, books will be due
two weeks, or 14 days, from checkout.
0:59
The return book method, is basically
the inverse of the check out method.
1:05
The book that's being returned,
is passed in as an argument,
1:09
the patron's current book property
should be set back to null,
1:12
since they're no longer
borrowing the book.
1:15
The book should be marked back in, and
1:17
as a result, its due date property
will be set back to null.
1:19
Then we set the book's patron
property back to null as well.
1:22
I've attached a work space to this video.
1:26
Open it up and
try to code these out on your own.
1:29
There's a read me file inside the work
space with detailed instructions.
1:31
When you're done, or if you get stuck,
head to the next step to see the solution.
1:35
Remember, everything you're
doing is just for practice.
1:39
You need to sign up for Treehouse in order to download course files.
Sign up