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
In this video, we'll review the solution to the first challenge and introduce our second challenge—adding two more methods to our media type class.
Instructions
2nd Challenge
- Add a method named
Loan()
that accepts a string of the name of the person who is borrowing the item.- Store the person's name in a new field named
Loanee
. - Use a boolean field,
OnLoan
, to indicate whether or not an item has been loaned out.
- Store the person's name in a new field named
- Add a method named
Return()
that resets the loan related fields to their default values.-
null
for theLoanee
field andfalse
for theOnLoan
field.
-
- Update the
GetDisplayText()
method to include the loan related fields if the item is on loan.- For example, if an album is currently on loan, it should display the text "Album: Yellow Submarine by The Beatles (Currently on loan to Joe Smith)".
- Test your changes by updating the Program.cs file
Main()
method to call theLoan()
andReturn()
methods.
Help
If you get stuck on any of the following topics or simply need a refresher, click on a topic in list below to view the associated video in the C# Objects course.
Welcome back.
0:00
How did you do?
0:01
Don't worry if you weren't able to
complete every part of the challenge,
0:02
it's normal to struggle when
you're learning something new.
0:06
Let's walk through my solution.
0:09
In the Album.cs file,
I added a method to the Album class.
0:10
Remember that a methods return type is
the data type of the value that will be
0:16
returned from the method.
0:20
So I use string for
the method's return type,
0:21
then I name my method GetDisplayText.
0:26
After the method name,
I added a pair of parentheses.
0:29
If this method needed parameters I'd
define them here between the parentheses.
0:33
But this method won't
define any parameters.
0:37
So I'll leave this part as it is.
0:40
Then I added a set of curly braces to
define the code block for my method body.
0:43
In order for the method to be accessible
from the program.cs file main method,
0:48
we need to include the public access
modifier right before the return type.
0:53
To return a string value from our method,
1:00
we use the return keyword
followed by a string value.
1:02
For my string value,
1:08
I'll use string concatenation to combine
string literals and field values.
1:09
I'll do the same thing that I was
doing in the program.cs main method.
1:14
So Album: " + Title.
1:18
Then another string literal contain
the word by, then the artist field value.
1:25
Cmd+S or Ctrl+S to save the file and
open program.cs.
1:32
Here, I did a little bit of clean up and
1:39
removed all of the code that didn't
pertain to the album media type
1:41
since that's the one media type that we're
working with in this practice session.
1:44
I also renamed the album
variable to album 1.
1:53
And added two more album class instances.
1:59
var album2 = new Album.
2:03
And for the title, how about The Wall?
2:07
And for the artist, Pink Floyd?
2:10
Then var album3.
2:15
New album.
2:18
How about Pet Sounds by The Beach Boys.
2:21
And to write each album's media
type information to the console,
2:29
I passed a call to the album classes,
2:33
get display text method into
the console right line method call.
2:35
First, album1.
2:43
So, album1.GetDisplayText then
2:45
repeat that for album2 and album3.
2:50
And finally, I compiled and
ran the program.
2:58
Ncs star.cs,
in order to include all the cs files
3:04
in our project,
then -out colon Program.exe,
3:09
in order to name the exe file
then let's also run the program.
3:14
&& space mono program.exe.
3:20
And here is the output for
all three of my album media types.
3:27
Are you ready for the second challenge?
3:31
When thinking about the behaviors that
I wanted my media types to support,
3:33
I remembered that sometimes
I let family members and
3:37
friends borrow items
from my media library.
3:39
So I thought it'd be helpful to add
two methods, loan and return, that can
3:42
be used to track when an item is loaned
out to someone and when it's returned.
3:48
The loan method should accept a string of
the name of the person who is borrowing
3:54
the item and store the person's
name in a new field named, Loanee.
3:58
Use a boolean field, OnLoan,
to indicate whether or
4:03
not an item has been loaned out.
4:06
The return method should
reset the loan-related
4:09
fields to their default values.
4:12
Null for the Loanee field and
False for the OnLoan field.
4:14
Unlike the getDisplayText method,
neither of these methods, loan or
4:18
return, will return a value.
4:22
Speaking of the getDisplayText method,
4:25
update it to include the loan related
field items if the item is out on loan.
4:28
For example, if an album is currently
on loan, it should display the text
4:33
Album:Yellow Submarine by the Beatles,
and then in parentheses,
4:37
Currently on loan to Joe Smith,
the name of the person who borrowed it.
4:42
And lastly, to test your changes, update
the program.cs file to call the loan and
4:47
return methods.
4:52
Be sure to use the get display text method
to write the media type information to
4:54
the console after calling the loan and
return methods.
4:58
They'll help you to determine if
the new methods are working properly.
5:02
And that's the second challenge.
5:05
After the break,
I'll show you my solution.
5:08
You need to sign up for Treehouse in order to download course files.
Sign up