So in the previous video, we actually finished off our linked list demos, so
now it's time for a challenge.
So I'm gonna type out the challenge for you,
to really help understand linked lists.
Okay, so here is our challenge.
So the idea is to create a program that implements a playlist for songs.
So you want to create a song class, which has got title and duration for the song.
And the program will have an album class containing a list of songs.
Now, the actual albums will be stored in an array list.
Songs from different albums can be added to the playlist.
And the playlist is actually a linked list.
And the songs that we can add will appear in the list in the order they're added.
So that's the linked list functionality.
Once the songs have been added to the playlist,
you wanna create a menu of options, to add this functionality.
Quit out of the menu, skip forward to the next song, skip backwards to a previous
song, replay the current song, list the songs in the playlist as well.
Now a song must exist in an album before it can be added to the playlist.
Remember that the songs are stored in albums in an array list, but
the playlist is the link list.
So, in other words, here making sure that the song exists in an album before it can
be added to the playlist, so
you can only play songs that you wanted that you've already got in your catalog.
And the hint for this is to replay a song.
Consider what happens when we went back and forward, or
what happened when we went back and forth from a city in the previous video,
before we started tracking the direction.
So you'll probably wanna do something like that.
And as an optional extra,
provide another menu option to remove the current song from the playlist.
And the hint for that is to look at list iterative.remove.
So that's it.
That's the challenge.
Go away and try that out, and once you've tried it out and got a working solution or
you've had enough and wanna see my solution, come back and play the video.
And I'll go through and show you how to do it.
Pause the video now, and I'll see you when you get back.
Okay, so how did you get on?
Hopefully you figured it out and you came up with a solution.
But let's go guys through what my solution is for this.
And bearing in mind, as I've said often in the past, this is just one solution.
There is always more than one way to develop almost anything when you're
programming.
So let's start off, create a subclass.
I'm gonna do that first.
I'm going to create a class called song obviously.
And we're going to have two fields, private
string will be the title name of the song.
And private double duration.
And we're using a double, so we can actually have decimal point,
cuz all the songs don't play for exactly three minutes or exactly four minutes.
Okay, add a constructor and
the constructor's gonna be both parameters here.
We're also going to add a getter for the title.
And also, we're going to return a two string.
And we'll see this in use later on.
This is just a quick way to actually output the contents of an actual object.
Two string is part of the Java infrastructure for all objects.
And I can show you that.
We can actually go and go to override it.
Override.
Notice how we're in object, and of course, if you recall from our object oriented
programming section, all classes are inherited from the object class.
As you can see there's a two string there.
This is optional, so you're not gonna lose points if you haven't done this.
But I just want to show you how to use two string as well.
So traditionally what you do in a two string,
you do something like this.title + this.duration.
So you'll normally print out the actual contents of the class or
the various fields, and it's just a good way to actually quickly print out
an overview of what that particular object for that class is actually set to.
So you'll see that in use later on anyway.
So let's now create an album class.
That's the next thing we need to do.
Album and just to go back as a recap.
The program will have an album class containing a list of songs, so that
the albums will be stored in an in array list, so we need to go back and do that.
So what we wanna do then is we wanna have the album name,
which is going to be private string name, private string artist.
And now we need to create an array, that's an array list of songs.
That's gonna be the songs that are of course, part of this array.
So, the constructor we need to create will have the name and
the artist as parameters.
But then, we also wanna initialize array list.
So, this.songs = new array list to make sure that it's initialized so
that we can actually use it without getting any errors.
So next, we get to the stage of some methods to add some functionality.
So one of the first things we need to do, is we need to be able to add a song, so
that the song actually goes into our array list.
So to do that, we're gonna create a method called add song, and
I'm gonna use boolean, you wouldn't necessarily have to use boolean, but
I'm going to do that just to make sure it's validated.
So, it's add song.
String title.
String, oh sorry, double.
Duration.
Then we're gonna do if find, song,
title = null, this.songs.add,
new song, title, duration.
Return true, otherwise, return false.
So in other words, what we're doing here with this validation is,
we're checking to see, or to make sure, that we're not duplicating songs.
Songs can only be added once, so
we're doing the comparison test on the actual title.
And we'll write that method next, the find song.
We can make that a private cuz it's only gonna be used internally.
Private song, find song, string title.
We put four song, check song.
This.songs.
Now what I'm introducing here, I'll just go through and write the code.
And I'll explain it.
Checksong.getitle.=title.
Return.
Check song.
Return null.
Now, what this line here is,
this is an alternative way to actually go through a list of entries.
It's the for, each command in Java.
So what it's actually doing is, instead of us having to put for
I = zero, I list, and the actual size of the array list in i++.
What this does, it actually goes through and creates a variable
called checked song for every entry that's actually in our array list.
So that's the equivalent of doing our normal while iterations when we go
through and increment a variable called I, I = I + 1, and
do a test for .geti and so on and so forth.
This is a shortcut.
So again, what happens is Java with this code here, probably does the same code
internally that we were doing, but this is far easier to read.
So again confirmation for every entry, as we go through this for
loop, every entry that's in our array list,
Java will automatically create this variable called checked song.
And from there, we can actually use that what we'd normally use.
We can do with checked song and get title.=title.
So what we're doing of course, is just making sure that's there's no duplicates,
but this for
each statement is actually an abbreviation to make things a little bit easier,
when you wanna navigate through an array list or any type of list for that matter.
So that's what that is. So if we actually do find the title,
then we return the actual title itself, otherwise, we return null.
And of course, the test here it adds song, it's like you're looking for null.
If we don't get null, it means the song already existed.
And we return false.
But if we do get null, then it's going to add the song and then return true.
So that's it, that's our add song and find song.
But we also need some way to add songs to a playlist.
So what we're gonna do is, we're gonna add to the playlist method.
Cuz of course we've got songs, but at the moment we've got no concept of a playlist.
Here's where we get a little bit tricky because what we're going to do,
is we're gonna create this playlist method and we're gonna actually add
the extra songs that are part of this album to that playlist.
And we're gonna actually pass that playlist as a parameter,
which is our link list, so the code we are gonna create is public boolean,
add to play list, int track number, Then linked list.
There's gonna be a linked list of songs which we're gonna call our playlist.
And I'm gonna type some of this code then we'll go through it,
int index = track number.
Take 1, if index is greater
than 0 and index is less than or
equal to this.songs.size and
do play list.add.
This.songs.getindex.
I need another bracket there, I think.
And at that point we're gonna do a return true.
Otherwise, we'll print out a message here,
which will say this album does not have
a track + track number return false.
Okay so, let's just review this code cuz it might be a little bit confusing.
So first what we're doing,
is we're actually passing the actual track number that we wanna add.
Now, normally, we would hide the index from users.
From the users of our classes, but
songs in an album actually do have a track number.
So if you actually buy a CD, the song has actually got a track number.
So that's what we're doing, is we're using that track number as a way to reference
the actual entries for this album.
So what that means, is when we're adding the songs up here,
we need to add them in the same order so that the first time we add an entry,
that's actually track number one, track number two, track number three, and so on.
So, by doing that, what we're now doing is, we're actually doing a test here.
So, we're passing the track number that we want to get added to our playlist.
And, we're also passing our playlist.
And then, we're doing a test here.
The int = track number take 1 because of course,
track numbers start from 1 in terms of how humans, how we read them.
We don't have a track 0, but, internally, in our array list,
our counting starts from 0.
So, that's why we're putting a take 1.
Then, we're just making sure that there actually is a song in that entry.
So, if there is a song.
In other words, there's a song in that index position for this array list.
Then, we'll actually add the song object to the playlist that was passed to us
automatically.
And of course, as you're gonna be seeing later is we'll actually look at changing
the code for that in a minute.
So consequently, we're actually getting, we're passing the track number we want.
And we're passing the playlist.
It does some validation.
And if the actual entry for our song is there, so
our track number is the song is being added in the right position.
Then we're actually gonna then add that to our playlist.
Otherwise, we get return a message that there's actually an error.
So now it's not actually a requirement to
overload the method to successfully meet this challenge.
What I'm going to do is also add a second option,
which is the ability to be able to add a playlist.
But in this time past the title instead of just having a track number.
So a track number equates to the index position in the actual array list.
Now that's also at an overload disk method to add another option or
we can actually add it, but use title instead of the track number.
So, we'll do something like uni-space it.
Public, boolean, add to playlist.
And this time, we're gonna pass the title to get the title we passed to us.
But then, linked list song playlist.
We'll do something a little bit different.
We'll do song, check song = find song, title.
And of course, this time we wanna check that it's not null,
which means that it was found on file.
So put if check song not equal to null playlist.add.
Check song.
Return true, because we successfully found it.
Otherwise, the song +
title is not in this album.
Return false.
And as you can see, there's two ways now for us to add a song to a playlist,
either by the track number, which again equates to the index position,
the order that they're actually added.
And we use the add sum method or the actual title itself.
Now the other thing you could have done here,
which we won't go into a lot of detail about, but
we could have added the actual add to play list in the actual song object itself.
alright so this is future
Tim recording this in the future and I'm
actually adding this bit of audio here
now just to let you know that there's
actually a problem with this bit of code
here this add to playlist method that
we've been working on now the next video
I'm going to show you how to make the
change in full but you can see what I've
got on the screen there now is a small
little change basically the if statement
is incorrect and that's because the
index always starts from 0 so if we
enter track number one index value will
be zero since one take one is zero now
consequently what will happen is the if
statement will be false so the code will
print this album does not have a track
one so basically as you can see on the
screen the changes we need to actually
change the index greater than zero to be
index greater than equal to zero but
well go through and make that change in
the next video all right so let's go
back now to where I was talking
previously just finishing off the
explanation of this method
But what I chose to do was actually, add it in the album because we're actually
looking at the album when we're actually creating the playlist.
But it would be equally valid if you decided to add the mechanism to
add a song to the playlist, if you had that code
in the actual song class that would be perfectly fine to do it that way as well.
Okay, so time is ticking on with this video, so I'm gonna actually end the video
here now, but in the next video we'll continue on we'll actually
start creating albums and adding songs and testing some of this code out.
So I'll see you in the next video.