So in the previous video, we actually got most of our challenge working.
We got the ability to actually go forward to the playlist,
to the next song on the playlist that is, and also to go back to the previous one.
So let's now hopefully finish off the rest of the challenge in this video.
So what we need to do is, firstly we didn't actually print a menu option.
The actual correct options that are available for the menu selection.
So we'll do that first, so we can see what the relevant options are.
So I'm going to first put in here, in the play method,
once we have determined there are some songs in the playList.
We're going to actually print out a menu.
So we're just gonna create a print menu method.
Call that print menu.
Print that there for
the first time and also going down the list we've actually commented that out.
So actually uncommented it there as well and once you create that method and
of course that method is only going to literally print a menu, so
private standing void print menu.
No parameters, and we'll just put available actions.
Press and the option are going to be are we talked about some of these,
zero to quit.
So it's actually to leave.
And one is to play the next song which we saw working in the previous video.
And two of course of to play previous song.
Song.
Option three is gonna be to replay the current song.
We haven't developed that method yet.
Whoops.
And option four, is going to be to actually list the songs.
4- List songs in the playlist.
And option five is going to print this,
this is essentially going to be this menu option itself.
So 5 print available actions like so.
And that's gonna tree it.
That's that method now.
So let's fix that up.
So, that's good.
So again we've got it working so
it's actually showing the next song in the playlist.
And also, well not only to go forward, but also to go back as well.
So the next thing we actually want to implement is we wanna implement
the ability to actually show a list of songs.
We're gonna actually print the list of available songs.
That's easy.
Print list command.
So we'll do that one first.
That's menu selection four or case four.
So the code for that is going to be private.
Static void.
And it's print list of course.
And the perimeter pass is going to be linked list of song objects.
And, we'll call that playList.
So, firstly, we need to create an iterator.
Iterator = playList.iterator.
And, because we're not going backwards and forwards,
we don't actually need to use a list iterator.
So, I'm just gonna show you how to use a regular iterator, which works for
lots of different types of objects as well.
That's very much the same,
but it doesn't give you the capability of going backwards.
You really just need to continually go through forward and it's perfect for
sort of looping through something once, which is what we're doing this case.
So, let's then print out some equals signs here just to make it clear in the output
that this is the start of the output.
And then we'll just put while iterator.hasNext,
so while there is another song to be displayed, and
we simply put System.out.println, and it's going to be Iterator.next.
Now note that I could have also put .toString, but
that is actually the default option.
In the previous video, I actually put .string, but that is optional.
Sometimes it can be a good idea to put that in, so
it's clear in your code what you're trying to do.
But as you can see, it actually accepts it and works quite nicely without it.
The only thing we need to do at the end of that,
is actually just print out another set of equal signs, so
that we can clearly see that the list is available and actually there.
So that's good.
So that's actually working nicely.
Really the other main thing we need to do now is, the last thing is case three.
And that's actually in the situation where we wanna actually play or replay the song.
So what we can do is by being able to replay the current song,
we can take advantage of the fact that the ListIterator is
actually somewhere between the previous and the next.
So in other words, it's somewhere in the middle.
So one of those will give us the track we last started playing and
from there we can work out which one to use.
All we really need to know is which direction we were moving in at the time.
So for example if we're moving forward, we just after the currently playing song, so
we need to go back because we're in a forward position.
But if we're actually going backwards we would be going to the previous one.
Then we're just before the current song so we need to go forward.
So in both cases we've changed the direction.
So we need to set forward appropriately so, if we're going forward and
go back to play the same track again, and then forward, vice versa.
So it sounds a little bit confusing but let's put the code in and
hopefully we're looking for code it'll actually make more sense.
So the first thing we need to do is determine the direction so
we'll put an if forward, and we'll do the code firstly for
the situation where we were moving forward.
Then we need to check if there's a previous.
So listIterator.hasprevious.
We can put a message now, replaying + listIterator.previous and,
we could put .toString there if you wanted to.
Remembering that, that is optional because that's the default method, anyway.
So, we could actually take that out ourself, if we wanted to, but
I'll put that in there for now.
else.
If it didn't have a previous entry, we can just put we are.
At the start of the list.
Then so at that point we need to do an else and
an option to determine if we're going backwards.
So we put an else, here we put else, so if we weren't going forward,
we do our list interator.hasnext.
And we're replaying plus
listIterator.next.string.
And what I didn't put there, I should have actually made a topper there, but
also we do also need to set the status, cuz we're actually putting,
we've done the check for previous, and we've gone into a forward direction.
We now need to set forward, equal to false.
So, we've checked to see if there was a previous one,
we've gone to the previous one, but then we've set forward to false,
because of course we're telling the system, we're keeping track of the fact
that we're now going back, which we had to do to replay that song.
So, contrasting that and the other situation,
where we were going backwards, we need to actually set forward to true.
Forward equals true,
because we've had to go forward in order to actually replay the song again.
So, hopefully that actually makes sense, and we just need an else here as well.
The else is just going to say else We have reached the end of the list.
So that's essentially the code there and that's again, so if we count when we
are moving forward we then check to see whether there is a previous record.
If there is, we actually move to the previous record and
actually show the name of the actual song as it currently is before moving.
Else, we just put a message to say that we're actually at the start of the list.
And then the opposite applies in the case where we actually weren't moving forward.
We actually go to the next record, replay it and set forward,
the forward direction to forward, no to true,
it'll say which is, means that we're actually going forward.
At this point, all options are now working and we have a fully operational playlist.
So let's actually just check that it does work.
Gonna run that.
Okay we'll make a bit of space here, we probably need more space.
So this is just option one, so top it in the source code, we're going to here.
One playing sort of in a previous video.
That's working fine.
See the list I can't go any further, that's good, now let's go back, going just
back to the front, to the first entry, so forwards and backwards is working nicely.
Let's actually replay the song at the start of the list.
We can't do anything because we haven't played one so
if we play a song, first one, and now if we actually replay, we're replaying it.
And if we go to the next record or the next entry in the playlist I should say,
and replay it.
That's working.
If you go back in the previous direction, number three.
That's also working as well.
And we can go back again at the start of the playlist and
of course we'll get the fact that we're at the start of the list.
So that's all working correctly.
And if we do option four that should be our printout.
That's a list of the songs in the playlist.
You could see that's working.
That's all the entries that were added to the playlist.
And option five should be our menu option.
Which of course just repeats the available menu options.
So that's now working fine.
But there is actually another option.
Remember that we also have a case we talked about optionally in the challenge.
And as this closes down, we'll go back and look at that.
As an optional extra, this is right down the bottom here, provide an option
to remove the current song from the playlist, a hint was listIterator.remove.
So we'll allocate for that.
It'll be case six here.
Case six.
We can do something like playlist.size.
That's greater than zero.
The top list operator.remove.
And we're doing the check to see whether the playlist is greater than zero.
Probably don't need that anyway because up here, we actually did a test
to see whether the play list was greater than zero or if it was equal to zero.
So you think at first glance we would need to test again.
But because we've now introduced the capability of deleting entries,
we do actually want to keep that.
We do actually want to keep that facility in there to be able to
test each time to see whether there's still some records in there because we
could have actually deleted all the records.
If that's the case, we don't want it to throw up with an error automatically but
there's actually a potential issue with this.
Well firstly, we'll just check that it does work.
So if we actually try and delete one, well firstly we'll need to go to a song.
So we're now playing Holy Man.
If we try and actually delete it.
The option 6. And we actually have a look at the songs.
We could see it's actually there.
It seems to have worked.
It's actually removed that song, correctly the current song we're playing.
But if we do a 6 again, we actually get an error.
So, the actual issue here is to do with the way that the ListIterator is
actually set up.
The link list.
So it can only be made once per call to next or previous.
So it actually throws an exception if neither the .next or
the .previous has been called.
So in other words, each time you delete a record you then have to do a .next or
a .previous to avoid getting an error if you try and do it again.
So we need to actually guard against that.
And that's actually built into the actual linked list code itself.
And you notice I talked about an exception.
We haven't actually dealt with exceptions yet, but
that's actually a way to actually trap an error.
By default Java will give you an error like you can see on the screen here legal
state exception.
We can actually trap those using a try catch block and
we'll be doing that later in the course.
So for now what we want to do,
is we want to just change our code to avoid this, exception coming up.
And generally speaking it's a good idea to do that.
If you can code something so
that you're not actually getting these exceptions, that's good.
But later on if you do get exceptions, you can guard against that.
And we'll be doing that in later parts of the course.
Let's go back and actually fix this so
we can delete remove records without any ramifications.
And firstly what I'll do is I'll quickly add this to the list.
The menu option for six, which will be six delete.
Current song from playlist, like so.
Okay so we can now see what that option is.
So the actual code, so what we'll do is.
There's actually two ways we could handle it.
The one would probably be to use the exception which I talked about.
So we could actually try and
catch that error if it come through in that code and perform some action.
But because we haven't actually talked about exceptions yet
we're going to leave that.
So what the other alternative is is at this point is to ensure we call either
next or previous immediately after removing a song and that ensures that if
you try to delete the next record we don't actually get an error again.
So it's actually not really that difficult to do.
What we really need to do here is just after this remove
we put if ListIterator.hasNext, so if it's going to the next record.
Now playing.
ListIterator.next, so in other words and
we're just automatically gonna move to the next record if there is one available.
We'll put else if listIterator.hasPrevious,
Now playing, ListIterator.previous, like so.
And obviously,
you still need a break here as well which I didn't actually have before.
Okay, so at that point, that should actually work.
So what we're now doing is we're actually doing,
if there are still some entries available in the playlist, so
we're checking to see whether the size is greater than zero.
If there is, we're going to remove the current one.
But then what we're doing is that really I'm ensuring that either there's a next
available entry or there's a previous and one of those will have to be true in order
for this to have succeeded, playList.size, because if that was equal to zero.
We wouldn't have gotten into AB executing this code anyway.
So what we're doing is we're making sure that one of these is actually executed,
and that's going to avoid the error.
In the future, because it means that we actually move to the next record or
the previous record,
depending whether there's a song that's actually available going forward or
backwards, and that'll actually stop that error from actually happening.
And really I guess what we're saying we're doing here is if we do actually delete
a song from the playList, the next song automatically starts playing.
And that's really what we're trying to say here.
So it automatically by default it'll go to the next song.
But if we happen to be at the end of the list,
then it would go back to the previous song.
So it would just continue on in this case playing and that's why it's automatic.
So let's just run that and make sure it works.
And we should be done.
So, again, we'll actually just look at our list of songs, and so
next, we'll actually go forward a song.
Play next song.
Playing Holy Man.
So, we now do a delete.
Now playing Soldier of Fortune.
You'll notice that was deleted as well.
We could delete that.
We could delete that, and so on and so forth, and we go back and
have a look at the list of entries that are there.
We could go to the next one.
Next, we're at the start of our list now.
Sorry, that was previous I was selecting.
And to go to the next one.
Next, next, next, next, and we reach the end of the playlist.
And we do a delete there.
Now playing let's go.
It's actually.
Automatically figured out, even though we're at the end of the list.
It went back, because it couldn't go forward, and then deleted an entry.
And we're not getting those exception errors anymore.
And again, looking at the list that's remaining,
we've only got two songs left in our playlist.
So that should be it.
That should actually be the challenge now.
We've actually finished everything.
Okay. So that's the end of this section.
We've now looked at linked lists data,
we went through the theory, behind their implementation.
And of course, we worked on the java implementation of it.
And then we actually created the first the program.
And obviously there's challenge as well.
Hopefully you can get an idea of what a linked list does.
There's actually, linked lists is just one example of many different data structures.
I've got trees, queues, hash tables and maps and many more.
And we'll be dealing with some of those as we go through the course.
So just keep in mind that there's lots of different ways to go about resolving
common computer issues by using a variety of tools that
are actually built into the Java software development kit which is pretty cool, and
link lister is obviously just one of them.
So, thanks for watching.
And it's now time to move on to the next section of the course.