In the previous video,
we actually implemented the song class as well as the album class for our challenge.
Now, time to continue on, and what we're going to do now is we're going to just
test the album class is actually working correctly.
So, to do that we're gonna go to our main class and look for our main method.
So, what we're going to do now is create some albums.
We're going to add some songs to the albums.
And then ultimately we're going to actually creat a linked list.
And add those albums to the linked list.
So, let's make a start.
So I'm going to type album, album = new Album.
String, and the name of the album is going to be Stormbringer, and Stormbringer.
Probably showing my age here.
And the band name is Deep Purple.
Okay, then, album.addSong and
the parameters, you can see, with addSong, is the title and duration.
So, we're going to actually going to add the song.
So, we're going to do The first one's Stormbringer 4.6 and
add some extra songs there and
add the tabs.
Soldier of Fortune.
Albums.
And, we also need, up here, a private variable called.
Field, I should say.
Called albums, which is going to be an arrayList of our album objects.
So, private Array list.
It's going album.
"albums=new array list of album, objects, like so.
It's up here, or down here.
If so we can type albums .add, and we can actually add each album.
So I'm going to add two albums, so I'm going to do this one first, add.
Album.
And that should have been static.
Because we're actually the static method.
Okay? So, we'll actually create a second one.
Album equals new album.
For those about to rock, one of my favorite bands.
And that's Aussie band there, AC/DC.
Let's add some songs, album.addSong.
For those about to rock.
Oops, semicolon there, and let's just add a couple more.
Okay that should be enough.
And we'll just quickly fixes.
Hit one, two, five.
Course you could put whatever songs you want to put in place of this.
Okay, last one.
And I just sped that up just to make it a bit easier so
you didn't have to see me typing all of that.
But you can actually use whatever songs you want obviously in your challenge.
But that's the ones that I've come up with.
All right, so we've got that.
And of course we then need to also add that to the album as well,
to the albums ArrayList.
So it's albums .addAlbum again, like so.
And just a note there, you might be wondering,
well how come we're adding album twice.
But we actually created a new object at this point.
So, by creating a brand new object, that's going to be completely
different to the previous album object that we created above.
So, that's now.
We now have an array list with two songs in it, By two different artists.
And what we are doing now we can songs to the play list.
And if you recall the play list is going to be a link list.
And of course the albums are stored in an array list.
So, the link lists, will be link list Song.
Play list equals new LinkedList of song like so.
So, to actually add something we can type albums.
First way of doing it, albums.get,
zero which is the first album we actually added to the array list.
So, that would be the Deep Purple album.
.addToPlayList.
Now, we're going to use the second one first.
The title And we're going to put the name in.
And this is where you need to get the name exactly the same or it won't work, so.
And we'll try You can't do it.
Whoops.
You can't do it right.
And of course we pass our playList to it.
And so if this name here, that name there doesn't correspond exactly
to the actual name that's actually in the album, in this case this one here.
Then it's actually gonna get an error.
It's going to get an error and
it's not gonna find it cuz it has to actually match.
So, it has to, firstly it has to match but secondly it has to be a song.
That was added to that album, so in other words, if you put let's go here,
and you're trying to reference element zero, which is the album for
Deep Purple, you'll actually get an error.
So, that's the first one.
Let's just add a couple more, so we'll just do a copy and paste here.
Two more.
So, next one we can do is Holy Man.
Type incorrect there, and the next one is create or type in one that doesn't exist.
The one that's not in that first album.
So, if I type in say, Speed king.
Does not exist, just to remember or to remind us that, that's the case.
So, that should work.
So, that's one way of doing it.
So, the second way would be to type albums.
And remember we overloaded the addToPlayList method.
So, we can type albums.get(0), so again it's the Deep Purple album,
.addToPlayList this time we use the trackNumber, so
trackNumber Our nine, which should be Soldier of fortune.
So, it's one, two, three, four, five, six, seven, eight, nine.
Should be the last one there.
There, that's track nine.
So, if we just type now nine cuz that was the second overload method.
And I'll just do that again so you can see it.
Using this one here, which is the first one.
So track 9, and it'll still be linked, a playList actually that's actually sent to.
So, that's just another way of doing it.
So, doing the same now to add some songs from another album,
let's add a couple more.
We'll add another four, but we'll actually use the other album,
so album one which was the AC/DC album.
So, let's go back there now and first one we'll do is song eight.
Which does exist, song three which does exist, song two which does exist,
and song six.
Actually what we'll do for the last one is to higher number we know doesn't exist.
So, that obviously doesn't exist.
There's not 24 tracks in the AC/DC album, so that should fire all of a sudden.
There is no track 24, like so.
At this point we can actually now try actually playing this.
Play playlist.
So, but of course, we haven't actually written the playlist method yet, so
we need to actually quickly write that, so
what I'm going to do is just write a really quick version of that,
and then we'll actually enhance it in the next video.
So, I'm just going to make a bit more space here.
Okay, so, first one's gonna be private.
static void play.
And the parameter I'm gonna be passing is a LinkedList, LinkedList of Song objects,
which of course is our playList.
So, we're gonna start off with something basic.
ListIterator, which will be Song.
listIterator cuz that's that top of object = playList .listIterator.
And, we'll put if listIterator.size.
Actually, not listIterator, sorry.
playList.size, but that's equal to 0.
We're going to return a message.
No songs in playlist.
Return, else, what we're going to do is, we're gonna start on parameters for
the first one, we'll put now playing, ListIterator.
Next dot two string.
Now, this is the important part here, the two string.
If you remember in the example for all linked lists,
that before the challenge, we were actually dealing with strings.
It was easy using dot next, because we new it was actually a type.
Of string so automatically worked some dot out.
But in this case we're using .net of course we're using a song object.
So, what we've implemented is this two string method of which I've talked about
in the previous video, two string.
If you go back and actually have a look at the actual song object
Remember that the two string, its function is just to print out for
a particular object, the title, the colon, the space, and then the duration.
So, that's why we've implemented two string.
When I run it I'll show you what output looks like without two string and
with two string as well.
Okay, so going back to the playlist, what we'll do for
now is we'll just literally just play the first one.
We're just going to play that first song just to confirm it actually is working and
we haven't actually got an error message so we'll do that.
We'll actually go back to main and we'll actually just run that.
I mean, I could get some message, the song speaking is not in the album.
Which is correct.
Because remember that up here, the song doesn't exist.
We actually purposely typed in a song that didn't exist in the album.
And I'll just put that on the screen so you can see it.
So, that's good.
This album does not have a track 24.
Remember we used a number that we knew there wouldn't be.
Track four from the second album.
Now playing You Can't Do it Right.
And that was the first entry that we actually added.
So, that's actually working correctly which is great.
And notice that this is the actual two string message there.
The name of the song, a colon and then the actual duration, so
that's working as well.
That's using the song object.
And just to show you what happens if I actually comment that out.
Sorry.
Should get rid of the override as well to get rid of that message.
And we get back to mine again and rerun this.
You'll actually see that the code will still work.
But we now get now playing conduct timber tracker.song@ 3d1 blah blah blah.
That number will probably vary if you are running in on your computer.
And that is because if you go back to our code again For Song,
we overrode a function if you recall, toString was actually part of the Object
that is a class that is extended by all other Objects you create in Java.
So, this is the default implementation of the toString method.
That's what Java does, gives you the name of the package the name of the class.
And a number which is meant to represent the memory location of where that
particular object is in memory.
But what we've done, of course, is we've overridden that functionality, so
it's actually printing out something useful, so
that's what toString is used for, and we could just run that again and see.
That looks like.
And we actually get the proper representation showing.
So, what we need to do now is actually continue on and
actually extend this player method.
Cuz obviously that is showing the first song.
But we would like to have a mechanism to add here so
that we can actually top in and go forward in our playlist.
Go back in our playlist.
And actually have access to the other songs that actually are part of that
playlist.
So, let's go ahead and do that now.
Let's actually extend out this play function, play method.
So, we've actually got that.
So, we need to add a scanner.
Because we're going to need that for input.
Scanner, equals new scanner.
System done, of course.
And we want a couple other variables, quit, which we saw in the previous video,
quit equals false.
And, we want to maintain or find out which direction we're going in.
Maintain the actual variable to tell us which direction we're never getting,
forward or back.
For reasons explained in the previous video.
So, I'm going to set that forward initially to true because normally,
when you start a playlist, you have to go forward.
Because you're at the first element, you can't physically go backwards.
And I'll just accept that import.
Java to the scanner okay we've got a Sonia Derader.
So, what we're going to do now is down here is add some more code while not quit
because of course with the playlist you can continue until you actually exit
out and we're going to put into action equals scanner dot next hit.
Then we're going to do scanner.nextLine.
Again, clear.
Now, the actions we actually want this,
we're going to start with something pretty simple.
We're actually going to start off with obviously the ability to
exit out of the loop.
And we're gonna add some other cases and continue to add that as we go through.
So, I'm going to start with switch on action, case zero will be our quit.
So, we're just going to type system out playlist complete.
And, quit = true and break.
And, we also want a case 1.
1 break case 2 break case 3 break case 4.
We're going to do print the lists.
Playlist.
And break, and I'll explain that shortly.
And case 5, we're going to print the menu.
And we're also going to add a break after that.
And I'll put that on the next line.
Doesn't really matter, that would still work.
It's still valid, that looks nice, we're being consistent there.
Likewise for this one.
So, that's our basic menu at this point.
So, the actual options we're going to be adding.
So, we've got the first one which is the being able to actually exit.
The next option, option one, we're gonna, is going to be a forward.
So, we're actually gonna go forward.
So, we're going to do that.
If not forward, you remember this code from the previous video.
If listIterator.hasnext, listIterator.forward,
sorry .next, I should say, not .forward.
And then forward Equals true.
So we're checking to see with we're not going forward we need to actually change
directions to set it to going forward so that the next line actually works.
And of course the next line is if this iterator, if this iterator dot has next.
I'm actually gonna go to now playing.
+
ListIterator.next.toString
else we have reached the end of the playlist.
And we set forward to false, cuz we cannot literally go forward anymore.
So, that's the first one, and what we'll do quickly before we end this video,
we'll do the second case, which is going backwards, going back in the previous
direction, or going to the previous entries, if there are any in the playlist.
So, of course, the code we need to do there is to say,
if we're currently going forward.
if (forward), if(listIterator.hasPrevious())
then we need to change that direction, so it goes to the previous entry, previous.
Again this was all explained in detail in the previous video so
I won't do it again now.
Then we set forward to false.
And then we just add our test, if(listIterator .hasPrevious.
But, you can go back, in other words.
We're going to put Now playing listIterator.previous.toString.
And of course, the else will be if we've reached the start.
We are at the start of the playlist.
And of course, we're just gonna hit forward to true at that point.
So let's, now that we've done that, let's actually run this code and
I'll comment these out briefly and we'll do those in the next video.
So, that it will actually compile.
So, we'll run this just to make sure that forward and previous, or next and previous
in other words, going forward into the list or going backwards actually works.
Now, what I haven't done at this stage is I haven't actually got it showing
the menus, so it's not showing any options.
So, we can actually type these, we know that zero will exit so
we'll test if that works.
Zero actually Exits, that's good.
So, if we run this again, and this time if we type 1, it should go to the next one.
And we'll just go up here and have a look, the next one was Holy Man, so
that's correct.
Then the next one, now playing Soldier of Fortune, and Soldier of Fortune,
we can check that that's actually working by looking at that third entry.
Speaking was an invalid.
So, of course, that hasn't been added, which is correct.
And track nine was Soldier of Fortune, from this album.
And you can see that that's working, so we'll do another one.
Now playing Breaking the Rules.
Breaking the Rules was Track eight for this one and
you can see that's breaking the rules which is track eight which is correct.
And just quickly going backwards we should be able to go back to Soldier of Fortune,
go back to Holy Man, and go back one more time To You Can't Do It Right.
And now plying, you can't do it right.
And press two again, we should get an error.
We're at the start of the list, so we can't do any more.
So, that's good, we have a lot of our code working.
So, I'm going to stop the video here.
In the next video, we'll continue on.
And hopefully we'll get close to finish this code.
I think that we're just about done, so we'll do that in the next video.