Hello, and welcome to the section 20 second challenge.
This challenge is all about using a std list, a doubly linked list.
What we're going to do in this challenge is we're
going to simulate a playlist.
So imagine you've got a CD and you want to put it in your car, you
want to play it and move to the next song and the previous song
and the beginning and so forth.
We're also going to be able to add songs to this.
So that's the idea here.
And we're going to model that playlist using a doubly
linked list of song objects.
So I'm in the section 20 workspace.
And here is the challenge 2 project, and then there's
the challenge 2 solution project, which has my solution.
And I'll go over that in the next video.
So let's talk about this challenge a little bit.
The idea here is that you want to learn how to use a std list, and that's what
we're going to use in this challenge.
We want to create a menu-driven application.
This would be really cool to do graphically, but we haven't
learned how to do that stuff.
So what we want to do is we want to simulate the user selecting the
first song in the playlist and then going to the next, the previous,
playing forward and backwards through the playlist, simple as that.
We're also going to allow users to add new songs to the playlist.
And every time we add a new song, it's going to be added prior
to the currently playing song.
Okay, so here's the menu.
And I've got a sample run coming over here on the right.
And I'll go to that in just a second, but here's the menu, play the first
song, play the next song, play the previous song, add and play a new
song at the current location, and then just list the current playlist,
and that'll list the playlists or, in other words, the songs that are
out there, and it will also list the currently playing song, and q to quit.
So that's it.
It's real simple.
The available playlist is modeled as a list of song objects.
I'll provide the song class, and I'll go over that in just a second.
And these are the songs that I put in the playlist just default.
I just grabbed a bunch of them from the pop charts.
You can change these to whatever you like or just put song one, song two,
song three to keep track of them.
I got kind of bored doing song one song two.
I wanted to see some real song names.
So here they are.
Drake Camilla Cabello and Maroon 5 Imagine Dragons,
a bunch of popular songs.
That's it.
So let me go over the run first.
And then what I'll do is I'll go over the sample code that I'm giving you.
Over here on the right, you can see the program running.
The first run it just displays the playlist right here.
That's the playlist.
So you can see these songs right here are showing up right here.
And the current song that's playing is the one by Drake, which is
the first song in the playlist, you can see it right up here.
So by default, that's that's our starting point.
When we first run the program, the first song will be playing.
Imagine you're putting a CD in your car.
Here's the menu, play the first song.
So anytime you press f, it's going to jump to Drake's song and play it.
Next obviously, it's pretty straightforward what's happening.
If I'm playing Drake's song and I press next, then it's
going to go to Camilla's song.
And if I press next again, The Weekend, Zedd, Maroon 5 Imagine Dragons and so forth.
Now since we have a doubly linked list, these songs are modeled
as doubly linked lists, right.
You've got your links coming back and forth.
And then I've got something called current song and that's an iterator,
that is pointing to the current song.
So when you press next, all we're doing is we're moving the iterator
over here and then press again.
Next, we're going to move it over here.
Now what's going to be interesting is if I'm right here at the
first and I press previous I want to wrap around to here.
And if I'm over here and I press next I want to wrap around to here.
So I always want to be in that playlist.
Okay, so let's walk through this little demo.
Right now when we first start the program, you can see
the playlist and you can see that Drake's song is playing.
That's the very first song in the playlist.
If I press n or next, it says playing next song and it says now it's playing
the Camilo Cabello song, which is this guy right here, never be the same.
If I press next again, and I'm going to scroll up so you can
always have this in context here.
Now it's playing the "pray for me" song, which is the third song.
I'll press next again.
Now it's playing "the middle".
When I press next again, it should be "wait" and then "whatever it takes".
So I'll press next again.
Wait is playing by Maroon 5.
One more time and now "whatever it takes" my Imagine Dragons is playing.
Now I'm at the end of my playlist.
If I press next again, I want it to wrap around back to the Drake song.
So let's do that.
I'll press n, and you could see that -- let me scroll that up, you can
see that Drake's song is now playing.
Now if I'm here at the beginning and I press p for previous, it
should wrap around back down to the Imagine Dragon song.
So I'll press p for previous.
Now I'm right here.
I'm right at the end here, Imagine Dragons is playing.
Let me move that up for you so you can see a little better.
Okay, so that's it.
Now, of course, if I press previous here, it's going
to go to the Maroon 5 song.
And you can see it's just a linked list.
And I'm able to go around it, which is pretty cool.
If at any point you can press l and that'll list what's currently
happening, so you can see the playlist right here that hasn't changed because
we haven't modified it at all but
you can see the playlist right here.
And the current song is the Maroon 5 song.
So anytime you want some information, you press l.
All right, so now let's get to the good stuff.
Let's add a song.
I'm going to scroll up just a little bit okay.
Now I'm going to press a.
And when I press a, it says adding and playing a new song.
Cool, so what's the song name I want to add.
Let's say I want to add "Imagine".
Who's the artist, it's the Beatles.
What's my rating for the song, I'll give it a five.
Now when I press enter, that song will be added before the current song.
And if you remember the current song was "Wait".
That's the one that's currently playing.
So "Imagine" will be added between "The Middle" and "Wait".
So I'll press enter and it will play. It'll be the current song.
So I'm going to press enter and you can see now it's
playing Imagine by the Beatles.
And if I list -- let me scroll up a little bit, if i list my playlist now,
you can see that Imagine is right here.
Let me mark that for you with my pen. You can see that Imagine is
right here now and it's between Wait and The Middle, just what I wanted.
Now suppose I want to press f and I want to go to the first song,
so we're going to go here to the Drake song and then I want to
insert a new song before that.
So let's do that now.
So the first thing I'll do is, let me clear this real
quick, and I'll scroll up.
So I'm going to go to the first song, which is the Drake song.
I'm going to press f right here.
I'm playing Drake's song.
And now I want to add another song.
So let's add what's the song's name, let's say it's New York, and let's
say that this is Frank Sinatra.
And I'll give it a five.
I'm giving everybody fives.
That's it. Now it's playing New York.
Remember, we were at the beginning.
So if I display my playlist, you can see that New York has now been
added at the beginning of the list.
And it's playing right here current song New York.
That's it. That's the program.
You press q to quit.
Now there's a couple of interesting things here that you can extend once
you get this basic functionality going.
Notice that in my menu, I have no way to play the last song in the
list or the ending song in the list, you could certainly add that.
You could add a whole bunch of other things you can save this to file.
You can read this information from a file.
So you can extend this program in a bunch of different ways.
Okay, so now let me show you the source code that I'm giving you
and that is right in here.
I've gone ahead and included the include files that
you're going to need.
And let me show you what the song class looks like.
It's a real simple class.
Obviously, this is what we're modeling, the name of the song,
the artist and the rating, which is an integer in this case.
I have a friend function here, which is my overloaded insertion operator.
So that can display this kind of stuff over here real easy.
I've got a default constructor.
I've got an overloaded constructor here that allows me to create whatever
I need using name artist and rating.
Then I've got some getters that I don't even think I'm using the getters,
but I put them in there anyway.
Now the most important thing we need to do here is -- and it's not so
important in this program, but it's just best practice to always do.
We need to overload these operators, the less than operator
and the equality operator.
Those are the only 2 operators that we must overload if we're going to use
our own custom classes with the stl.
So in this case, less than just -- I'm defining less than as
this -- name of this song is less than the name of the other song.
And the same thing with equality.
If the 2 names are the same, then I'm considering them equal.
Okay, so that's that.
Here I'm overloading the insertion operator, as I mentioned.
I'm giving you the code for the display menu, which is really, really easy.
And then I've got a couple of methods here or functions, I should say,
that you're going to implement.
One is play the current song.
And all this does is it does this right here. Here
we go. So I just went forward, it says playing.
And the song that's playing.
That's all. It just displays that.
And the song that's being played is passed into it.
So that's a really one-liner, right.
You basically just have to output that to an upward stream.
Display playlist.
Here, we're getting the entire playlist and the current song.
And this is the one that you're going to call from the list
functionality it's just going to display all those songs.
So in this case, we're going to see something like this
right here, all that good information is going to display.
Okay, and now here's the main.
Probably just what you expect.
Here's my playlist.
That's my object, and it is a list of song objects.
And I'm initializing it right here.
So that's done.
And I'm setting my current song.
This is the most important piece here.
That object right there.
That current song is an iterator to that's pointing to some
object in that in this playlist.
And I'm setting it to the beginning of the list.
I'm setting it right to there.
It's going to start right there.
From that point on, it's your logic to display the menu, get the input from
the user depending on what they select.
This is the guy that you're going to have to increment or
decrement depending on how you want to walk across this list.
That's it, have fun.
Get the basic functionality working before you extend it.
And then you can extend it like, I said a bunch of different ways.
You can go to the go to the end, go to the beginning, save it to a file.
So next time you run this.
You can just load the information.
The song information from the file.
Because remember, as it is now every time I add a new song to the
playlist, it's not being persisted.
So the next time I run this, it's not going to be there.
So it'd be kind of nice to be able to save that and then load it up again.
Okay, so that's it.
Have fun, it's a nice little challenge project.
It's simpler than you would think.
Think iterators, think stl, don't think about Is and Js and looping
variables and things like that.
Let the stl do the work for you.
All right, have fun, and I'll see you in the next solution video.