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? 0:00 We're getting pretty far into our library app. 0:02 You just finished adding the check out and return book methods to the patron class. 0:04 You calculated and set the due date inside the check out method. 0:08 But there's another way this could be accomplished that I prefer. 0:12 Consider this. 0:15 There's a property on the book class called out, and 0:17 it holds a Boolean value, true or false. 0:19 The book either is or isn't checked out. 0:22 The book has a due date property. 0:25 These two properties are set manually in the checkout method on the patron class. 0:26 But wouldn't it be great if when a book object is marked out, 0:32 that it would trigger the due date to be calculated and set as well. 0:35 And of course, when the book is marked back in, 0:38 it would trigger the dueDate property to be changed back to null. 0:41 That is the beauty of setter methods. 0:45 Marking a book as out or in and calculating and setting a due date inside 0:47 both the checkOut and return book methods is a bulky way to go about these tasks. 0:52 If we had a setter method for the out property, we could also figure out the due 0:58 date, and set or unset the due date property at the same time. 1:02 Meaning our check out and return book methods would become a lot more concise. 1:06 Let's refactor our code a little bit to try this new approach. 1:11 Open up the attached workspace for an updated readme or 1:15 check out the teacher's notes for the new instructions. 1:17 The following step provides a text based explanation of what we've discussed in 1:20 this video and might offer some more insight if you're feeling confused. 1:24 Give the coding a try and then check out the solution before joining me again. 1:28
You need to sign up for Treehouse in order to download course files.Sign up