Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Explanation: A Better Way With Setters Methods1:32 with Ashley Boucher
Instructions for Workspace
Your instructions are as follows:
1) Change the
out property initialization in the Book class constructor method to
2) Add a setter method called
out() to the Book class. It should receive one argument, a boolean value.
Inside the setter method, set the value of the
_outbacking property to the parameter.
Then, inside the setter method, check the value of the
_outbacking property. If it's
true, calculate the due date and set the Book object's
dueDateproperty. If it's
false, set the
3) Add a getter method to the Book class called
out(). It should return the value of the
_out backing property.
4) Back in the Patron class, refactor your
checkOut() method to use the new
out() setter method instead of your existing code.
How was that challenge?
We're getting pretty far into our library app.
You just finished adding the check out and return book methods to the patron class.
You calculated and set the due date inside the check out method.
But there's another way this could be accomplished that I prefer.
There's a property on the book class called out, and
it holds a Boolean value, true or false.
The book either is or isn't checked out.
The book has a due date property.
These two properties are set manually in the checkout method on the patron class.
But wouldn't it be great if when a book object is marked out,
that it would trigger the due date to be calculated and set as well.
And of course, when the book is marked back in,
it would trigger the dueDate property to be changed back to null.
That is the beauty of setter methods.
Marking a book as out or in and calculating and setting a due date inside
both the checkOut and return book methods is a bulky way to go about these tasks.
If we had a setter method for the out property, we could also figure out the due
date, and set or unset the due date property at the same time.
Meaning our check out and return book methods would become a lot more concise.
Let's refactor our code a little bit to try this new approach.
Open up the attached workspace for an updated readme or
check out the teacher's notes for the new instructions.
The following step provides a text based explanation of what we've discussed in
this video and might offer some more insight if you're feeling confused.
Give the coding a try and then check out the solution before joining me again.
You need to sign up for Treehouse in order to download course files.Sign up