Hello, welcome back to the challenge 2 solution for section 20.
So I'm in the section 20 workspace, and I'm in the
challenge_2_solution project.
And what I'd like to do is just go over my solution.
This is a really, really straightforward program.
If you're not used to thinking abstractly with stl containers
and iterators and so forth, then it becomes much more difficult.
But once you really take advantage of using the power of the stl,
it makes much, much easier.
So here's what I've got going on here.
I've got my song class, which we've already talked about.
It's very straightforward.
I talked about that in the previous video.
And I'm going to scroll down to the methods that we're going to implement.
So here is the display menu which i gave you.
And here's that very first method you had to implement,
play the current song.
So I'm being passed in a song object.
What do I do?
I just say playing, and I display the song.
Simple as that, the song class overloads my stream insertion operator
so it becomes basically a one-liner.
The next one is displaying the actual playlist.
And again, really straightforward.
What am I getting, I'm getting the playlist, which
is a list of song objects.
And I'm also getting the current song because I want to display both
of them that way I have all the information I need in the display.
All I'm doing is arranged based for loop over the playlist.
I'm grabbing the song that's there and displaying it, just like we did here.
So we're using the overloaded insertion operator.
That's going to display all the songs in the playlist in the loop.
And then I'm just going to say the current song that's
playing is current song.
This code is the same as this code.
I could have called this function from there, either way is fine.
And now let's look at the main.
That's the real workhorse.
You can see that I am defining playlist right here.
And this of course is main.
I'm initializing it to these six songs.
You could choose any songs you like.
This right here is so important.
If you forget to do this, you can run into issues.
That's why I gave you that one line in the code.
Current song is an iterator to a song in a std list.
And I'm setting it to the beginning.
So in other words, I'm always starting right at that first song up there.
And then I'm displaying the playlist what.
Am I passing in I'm passing in the playlist, and I'm passing in the song.
Remember, current song is an iterator.
So I need to de-reference that iterator to get to the
song that it's pointing to.
To be double safe and to use defensive programming, you always want to make
sure that that is actually pointing to the list and not off the list.
But in this case, I'm sure I'm not off the list anywhere in this program.
So I'm not doing it, but defensive programming probably would be a
good idea to check to make sure that iterator is not pointing off the list.
Okay, so now we're in the loop.
And we've got a character variable that's where I'm going
to read the user selection into.
I'm displaying the menu.
I'm reading their selection.
I'm making the selection uppercase that way I don't have to use
ors here in my if statements.
And if the selection is f, that means we're playing the first song.
I'm just displaying this little message right here, just playing for a song.
And I'm setting the iterator current song equals playlist.begin.
That's the first element in the list, the first song.
That's it, and I'm calling play current song.
And I'm dereferencing that iterator and passing this song in.
That'll display that, that song is playing.
And these little messages here I've got one here and I've got one here.
You don't really don't need those, but I just put them in
there that way you can explicitly see these functions working.
So let's look at next.
I'll scroll up just a bit.
So here we're going to play the next song.
What do I do.
I increment the iterator.
Now it's possible that I was on the last song, and now I'm off the list.
So I really need to be -- I need to check here.
So if the current song is equal to playlist end, that
means I'm off the list, right.
I just pressed next, and I was on the last song.
So what I need to do here is wrap to the start of the playlist and then
set my iterator to the beginning, and then we probably press current song.
Now if I'm not at the end, then I was somewhere at the beginning or
in the middle, it's not a big deal.
That's it, that's the logic right here.
For previous, it's very similar.
So I'm displaying playing previous song.
And I'm checking to see if the current song is equal to the first song,
then I need to wrap around, right.
So in this case, I need to set the current song to the end.
Now notice what happens here.
Remember, here's my list.
And this is the end of the list, right.
This is just a placeholder.
There's no song there.
So I want to set it to there because in the next line, I want to decrement
it and bring it right back to here.
So that way I don't have to duplicate code all over the place.
So that's it.
I decrement the song.
And I call play current song with the song.
And now it's going to be playing the last song if I was
at the beginning of the list.
Otherwise, it's just going to play the previous one.
The edition right here where we're adding songs.
I've got some variables.
I've got my name, my artist, which is strings and the rating.
I've got this line of code where if I provided in your description
this just clears out anything that's in standard in or cin.
In case, you've got garbage characters in there or extra returns from
processing, this is just a good practice to get into whenever you
really need to just clear out stuff I don't want anything in there.
So I'm just clearing everything out, and then I'm adding and playing a new
song, I'm displaying that message.
And I'm asking the user to enter the name of the song,
the artist and the rating.
And once I have that, I'm creating a new song based on that information.
And I'm inserting using playlist.insert and current song,
remember, that's my iterator.
So I'm inserting it before that.
That's the behavior.
I could use in place here as well, whatever you like.
Now what happened was, remember, here was my iterator,
which was current song.
It's pointing here.
And I just inserted that song here.
So I need to decrement that operator to play it.
So right here is where I'm doing that.
I'm decrementing the iterator right here and I'm playing
the song that I just inserted.
All right, now the last couple is just real simple.
Here list, I'm just calling display playlist, and I'm passing in the
playlist in the current song.
Q, I'm quitting anything else is an illegal choice.
That's it, you can see it's really straightforward, right.
I've got no looping variables.
I'm not worried about keeping track of stuff.
The only thing I'm keeping track of is I've got my list here.
And all I'm doing is I've got that current song that's the only
thing that really concerns me.
I've got that current song iterator.
And all I'm doing is moving that guy back and forth along the list.
Really, really simple.
It never invalidates because when I'm inserting something, I'm
inserting something before it.
So all i need to do is just move it back.
So I'm never going to invalidate that.
And the way that the code is written, it's never going to go off the list.
So that's why I'm not checking to make sure it's in the
list but I certainly could.
All right, I hope you had fun with this.
I hope you've extended it as well.
If you have your solution, you think it's really cool, post it in the Q&A.
We'll all comment on it.
All right, I'll see you in the next video where we're going
to start talking about sets.