Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Java Data Structures!
      
    
You have completed Java Data Structures!
Preview
    
      
  Let's develop a way for users to browse artists and songs and choose them easily.
Resources
- The Diamond - (halfway down the page)
 - Cache invalidation is hard
 
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      So let's continue on
with our user interface and provide a way
                      0:00
                    
                    
                      for our singers to browse artists
and then browse those artists' songs.
                      0:04
                    
                    
                      So I'm going to move this card here
over to Doing.
                      0:08
                    
                    
                      As a singer,
I should be able to browse the songs
                      0:11
                    
                    
                      by artists
so that I know what's available.
                      0:13
                    
                    
                      So if this were on the web, I'd suggest
we just produce a list of links.
                      0:16
                    
                    
                      But since we're console-based, we'll need
to create our own little interface.
                      0:20
                    
                    
                      How about we make a way to display a list
of numbered items,
                      0:24
                    
                    
                      and then have someone choose an item
from that list?
                      0:27
                    
                    
                      So, in Karaoke Machine,
                      0:31
                    
                    
                      we'll make a reusable helper function,
but we don't need to expose it, right?
                      0:33
                    
                    
                      So let's make it private.
                      0:37
                    
                    
                      We'll do private, and we'll return
an index of the list that we passed in.
                      0:39
                    
                    
                      So we'll say prompt for index.
                      0:44
                    
                    
                      And we'll require a list of strings that
can be the options to present to our user.
                      0:47
                    
                    
                      So we know that our list
                      0:58
                    
                    
                      is zero-based, but that doesn't mean
we need to expose that.
                      0:59
                    
                    
                      So let's make a new variable called
counter here and initialize it at one.
                      1:02
                    
                    
                      Then let's loop through whatever
these options were that were passed in.
                      1:08
                    
                    
                      So we'll say four, and we know that
                      1:11
                    
                    
                      they're a string, right,
because it's a list of strings.
                      1:14
                    
                    
                      Option in options.
                      1:17
                    
                    
                      Let's print them out.
                      1:22
                    
                    
                      We'll do that style, right,
                      1:27
                    
                    
                      where it's a period and then an open
paren that, so it looks nice.
                      1:28
                    
                    
                      And then we'll display
whatever that option was.
                      1:33
                    
                    
                      And then, of course, the new line.
                      1:35
                    
                    
                      And so what we'll do is that first %d
                      1:37
                    
                    
                      will be the counter
that's going to loop through each time.
                      1:40
                    
                    
                      And then we'll put our option here. Okay?
                      1:43
                    
                    
                      And then let's not forget
to increment our counter.
                      1:46
                    
                    
                      So each time through the loop,
that will go, you know, one, two, three,
                      1:49
                    
                    
                      however many options
there are in there. Cool.
                      1:52
                    
                    
                      Okay, so system.out.print, your choice.
                      2:00
                    
                    
                      So one thing to remember is that
when we use readline, it returns a string.
                      2:07
                    
                    
                      So let's do string, option as string,
                      2:11
                    
                    
                      and we'll readline.
                      2:17
                    
                    
                      Oh, what does that read line do?
                      2:19
                    
                    
                      That throws an exception,
so we need to make sure that we're going
                      2:23
                    
                    
                      to bubble that up.
                      2:25
                    
                    
                      Throws IO exception.
                      2:28
                    
                    
                      You can get the int out of a string
                      2:35
                    
                    
                      from saying int choice
                      2:37
                    
                    
                      equals integer dot parse int.
                      2:40
                    
                    
                      We'll say option
                      2:45
                    
                    
                      as string
and let's do a little bit more validation.
                      2:46
                    
                    
                      We'll trim the white space around it
in case they, you know, get excited
                      2:49
                    
                    
                      and press the space
a whole bunch of times.
                      2:52
                    
                    
                      Okay, we're going to return choice
                      2:56
                    
                    
                      minus one because we know that it's zero
based, right?
                      2:57
                    
                    
                      But we started at one,
so we need to take one off of that.
                      3:01
                    
                    
                      Cool.
                      3:04
                    
                    
                      So now let's
                      3:06
                    
                    
                      go get some artists
and song list to browse.
                      3:06
                    
                    
                      So we know that we'll need to first
browse artists and then there's songs.
                      3:09
                    
                    
                      So to me, that sounds
we need to make a map of artists to songs.
                      3:13
                    
                    
                      So let's save this and pop over here
to songbook.
                      3:17
                    
                    
                      Let's keep this internal for a minute.
                      3:21
                    
                    
                      I don't think we really need
to expose this map just yet.
                      3:22
                    
                    
                      We can change it later
if this doesn't work out.
                      3:25
                    
                    
                      So we'll make a method, private,
                      3:32
                    
                    
                      returns a map of string,
and it'll be a list of songs, right?
                      3:34
                    
                    
                      so the string there being the artist
and the songs are there
                      3:40
                    
                    
                      we can name it by artist.
                      3:44
                    
                    
                      We're in the song book, so song book
dot by artist kind of makes sense, right?
                      3:47
                    
                    
                      So let's create the new map. So string
                      3:52
                    
                    
                      and it a list of songs
                      3:56
                    
                    
                      and we'll just call it by artists in here
                      4:01
                    
                    
                      and say new hash map.
                      4:03
                    
                    
                      Something I wanted to point out
is that when you are initializing
                      4:06
                    
                    
                      a variable that's already been declared,
since Java 7, you can actually put
                      4:09
                    
                    
                      in empty angle brackets and it will assume
whatever you declared there.
                      4:13
                    
                    
                      I just want to show you
that it's a nice shorthand for what
                      4:17
                    
                    
                      we're trying to do here.
                      4:19
                    
                    
                      So we don't need to define that again.
                      4:21
                    
                    
                      We just defined it on the left.
                      4:22
                    
                    
                      We don't need to define it
on the right. Okay.
                      4:24
                    
                    
                      So for each of the songs
that are currently
                      4:27
                    
                    
                      in the songs list, let's
see if there's something to find again.
                      4:29
                    
                    
                      So we have this
as a map of artist songs here, right?
                      4:33
                    
                    
                      So we're going to say, by artist, get
                      4:41
                    
                    
                      song.getArtist
                      4:46
                    
                    
                      Okay, so we're going to loop through here.
                      4:49
                    
                    
                      We're going to see
if that song has already been defined,
                      4:53
                    
                    
                      and if it has, we'll get a list back here.
                      4:56
                    
                    
                      If it hasn't, we'll make a new one.
                      4:59
                    
                    
                      And again,
I'm going to use that diamond syntax.
                      5:05
                    
                    
                      We don't need to redefine
that it's a list of songs.
                      5:08
                    
                    
                      We defined it here, and here
I'm just leaving the blank diamond
                      5:11
                    
                    
                      because it works in our environment.
                      5:14
                    
                    
                      And I'm going to put that in the map.
                      5:17
                    
                    
                      It's an empty object
reference to that list.
                      5:20
                    
                    
                      And then
                      5:28
                    
                    
                      finally,
we definitely now have an artist's songs.
                      5:29
                    
                    
                      Either it was in there, or we created
a new one, so we can add the song to that.
                      5:32
                    
                    
                      I think that closes the for loop, yeah?
                      5:37
                    
                    
                      And then finally, we want to return
that map that we created.
                      5:39
                    
                    
                      All right,
                      5:45
                    
                    
                      so one more time, we're going to create
a map of artists to songs,
                      5:46
                    
                    
                      and we're going to come in here and create
a brand new map when the method starts,
                      5:50
                    
                    
                      and we're going to loop
through each of the songs.
                      5:54
                    
                    
                      We're going to see
if it's in the by artist map.
                      5:57
                    
                    
                      If it is, it's going to be here.
                      6:00
                    
                    
                      If it's null, meaning we haven't yet
come across a song by that artist,
                      6:01
                    
                    
                      we're going to make a new one, and
then we're going to put it into the map,
                      6:05
                    
                    
                      the artist name,
and that new list that we made.
                      6:08
                    
                    
                      And then, to that new or existing list,
we're going to add the song.
                      6:12
                    
                    
                      Then we're going to return them by artist.
                      6:16
                    
                    
                      Okay, so now let's expose our artists.
                      6:19
                    
                    
                      And this is really easy
now that we have that map.
                      6:21
                    
                    
                      So we know that we can return
a set of keys, right?
                      6:24
                    
                    
                      And that's what's in that map.
                      6:27
                    
                    
                      It will return by artist, we'll call that.
                      6:33
                    
                    
                      That will return the map,
and will return the key
                      6:35
                    
                    
                      set from that map.
                      6:38
                    
                    
                      Cool.
                      6:41
                    
                    
                      And then also,
                      6:45
                    
                    
                      very easily,
we can return the songs, right?
                      6:45
                    
                    
                      So we can say public list of songs,
                      6:48
                    
                    
                      because we want to expose these too,
                      6:52
                    
                    
                      the ones
we want to expose off of the songbook.
                      6:54
                    
                    
                      So we'll say get songs for artists.
                      6:57
                    
                    
                      And it doesn't know how we're storing it,
but it does know
                      7:00
                    
                    
                      that you can pass on the artist name here.
                      7:02
                    
                    
                      And let's just return.
                      7:06
                    
                    
                      We'll do the map,
and we'll say get artist name,
                      7:07
                    
                    
                      and if it exists,
it will return a list of songs.
                      7:10
                    
                    
                      Now, if you're paying attention,
you'll see that every time we call
                      7:17
                    
                    
                      either of these two methods here, it goes
and builds a brand new map each time.
                      7:20
                    
                    
                      Now, that seems like it's running more code
than it needs to, and it definitely is.
                      7:24
                    
                    
                      Now, there's ways around doing this,
and we could build the map once
                      7:29
                    
                    
                      and store it, but then what happens
if someone adds a song?
                      7:32
                    
                    
                      The map wouldn't contain the new song,
So the trick here is to perform
                      7:36
                    
                    
                      something called caching.
                      7:39
                    
                    
                      It's out of the scope of this course
but I did want you to notice
                      7:41
                    
                    
                      that we are doing
some somewhat unnecessary computation.
                      7:44
                    
                    
                      And we'll just let it slide
for this prototype. We could add
                      7:47
                    
                    
                      a fix me here, let's do that.
Let's add a fix me - This should be cached.
                      7:50
                    
                    
                      Okay, so I'm
gonna make sure that we have our imports.
                      7:57
                    
                    
                      We used set.
                      8:00
                    
                    
                      We used the map.
                      8:05
                    
                    
                      We already have arraylist and list,
                      8:11
                    
                    
                      and we added a hash map too.
                      8:14
                    
                    
                      Okay, so let's go
                      8:21
                    
                    
                      use these new data structures
in our karaoke machine.
                      8:22
                    
                    
                      So let's not expose
the method of prompt for artist over here.
                      8:26
                    
                    
                      Let's call it private,
and we'll have it return the artist name,
                      8:30
                    
                    
                      and we'll have it say prompt artist.
                      8:35
                    
                    
                      All right,
and we'll print out the heading,
                      8:41
                    
                    
                      available artists.
                      8:44
                    
                    
                      So our prompt for index
                      8:50
                    
                    
                      method takes a list,
but we're returning a set.
                      8:51
                    
                    
                      So we can actually take care
of that easily by saying
                      8:54
                    
                    
                      it's a new list of strings, artists.
                      8:57
                    
                    
                      It's a new array list.
                      9:01
                    
                    
                      Again, I'm
going to use that diamond structure there.
                      9:03
                    
                    
                      We'll say mSongBook.getArtists.
                      9:06
                    
                    
                      So that will return a set.
                      9:10
                    
                    
                      But if you pass it into an array list,
if you pass it in any collection,
                      9:12
                    
                    
                      it will make a new list of that. Cool.
                      9:16
                    
                    
                      So now we can say
int index equals prompt for index.
                      9:19
                    
                    
                      And we'll pass in that new list
that we just made.
                      9:26
                    
                    
                      Oh, and remember, the prompt for index
throws an IO exception.
                      9:29
                    
                    
                      So we need to make sure
that we also bubble that out.
                      9:32
                    
                    
                      So we'll throw an IO exception.
                      9:35
                    
                    
                      That will
                      9:38
                    
                    
                      again, make that top loop
be responsible to handle that.
                      9:39
                    
                    
                      And then so we have the index from this
so we can say use that to get the name out.
                      9:43
                    
                    
                      So we'll say artist dot get index.
                      9:48
                    
                    
                      So the prompt for index is returning one
less than the number that they chose,
                      9:52
                    
                    
                      which is the index into this list. Cool.
                      9:56
                    
                    
                      Okay so let's go
and add the menu option for choose.
                      9:59
                    
                    
                      So if you come back up to our menu,
let's go ahead and we'll say m menu
                      10:03
                    
                    
                      dot put choose.
                      10:08
                    
                    
                      And we'll say choose a song to sing.
                      10:11
                    
                    
                      So let's go down into the switch
                      10:20
                    
                    
                      and add the new case here.
                      10:21
                    
                    
                      So case choose
                      10:25
                    
                    
                      and we will say string
                      10:30
                    
                    
                      artist equals prompt artist.
                      10:32
                    
                    
                      Cool.
                      10:37
                    
                    
                      So that will show the list to them,
and we want to make sure that we break.
                      10:38
                    
                    
                      Awesome.
                      10:42
                    
                    
                      Okay,
so that's going to return the artist,
                      10:43
                    
                    
                      so now let's show
the songs while we're there.
                      10:45
                    
                    
                      All right, so let's
go down, and after this prompt for artist,
                      10:48
                    
                    
                      we'll say, again, keep it private, return
                      10:51
                    
                    
                      a song, prompt song for artist.
                      10:55
                    
                    
                      And we'll make it take a string artist.
                      11:00
                    
                    
                      Okay, so we'll go get the songs.
                      11:04
                    
                    
                      So it's a list of songs.
                      11:07
                    
                    
                      From the songbook, get songs for artists,
                      11:10
                    
                    
                      and we'll pass in our artist
we just received.
                      11:16
                    
                    
                      Great.
                      11:19
                    
                    
                      Now the prompt for index that we wrote
takes a list of strings,
                      11:20
                    
                    
                      and we currently have a list of songs,
                      11:24
                    
                    
                      so let's make a new list
that pulls out the titles.
                      11:25
                    
                    
                      So we'll make a new list of strings
                      11:29
                    
                    
                      called song titles
                      11:34
                    
                    
                      and it will be a new array list.
                      11:36
                    
                    
                      And I'll say for song in songs
                      11:43
                    
                    
                      songTitles song.
                      11:46
                    
                    
                      So what we're doing here
is we're building a new list.
                      11:58
                    
                    
                      We're looping through the songs by this
artist, and we're basically making a list
                      12:00
                    
                    
                      that's exactly this one, but it's only the
song title strings, right?
                      12:05
                    
                    
                      So we'll
get the index from our prompt for index,
                      12:10
                    
                    
                      and we'll pass our new song titles.
                      12:13
                    
                    
                      Then we'll return from the original list
                      12:21
                    
                    
                      songs.get index.
                      12:23
                    
                    
                      Alright, so that will return the song
from our first list,
                      12:27
                    
                    
                      not just the title in the second list.
                      12:30
                    
                    
                      Prompt for index is in here,
and that throws an IO exception,
                      12:33
                    
                    
                      so let's be sure to add that up here.
                      12:36
                    
                    
                      Okay so let's return to our choose case
                      12:40
                    
                    
                      up here
let's go ahead and say song artist song.
                      12:42
                    
                    
                      We already defined a song in the scope
up here so let's just name it artist song.
                      12:47
                    
                    
                      So song artist song equals prompt song
                      12:51
                    
                    
                      for artist
                      12:54
                    
                    
                      artist
                      12:58
                    
                    
                      So now we'll get the song back.
This is where we could probably
                      13:00
                    
                    
                      add it to the queue, so let's go ahead
and let's put a little to do here.
                      13:03
                    
                    
                      Add to a song queue.
                      13:09
                    
                    
                      And then we'll say system.out.printf
                      13:14
                    
                    
                      and we'll just do a little message
to make sure that we're working here.
                      13:18
                    
                    
                      You chose %s %n artist
                      13:21
                    
                    
                      song, cool.
Alright so let's go give that a go.
                      13:25
                    
                    
                      I'm sure I messed something up somewhere
in all of that craziness.
                      13:31
                    
                    
                      Okay, so
                      13:39
                    
                    
                      in karaoke machine
it doesn't know about ArrayList.
                      13:39
                    
                    
                      I forgot to do the import there.
                      13:43
                    
                    
                      I introduced a new ArrayList
and I didn't import it.
                      13:45
                    
                    
                      Looks like I also need to do that for list.
                      13:58
                    
                    
                      Okay, let's try that again.
                      14:05
                    
                    
                      Cool.
                      14:10
                    
                    
                      There are zero songs available.
                      14:11
                    
                    
                      Your options are to add, so let's add.
                      14:13
                    
                    
                      I'm going to add Michael Jackson.
                      14:17
                    
                    
                      We really need to make this so we can save
so we don't need to do this every time.
                      14:19
                    
                    
                      And then we'll do PYT,
and I'll just do example.com again.
                      14:23
                    
                    
                      Cool.
                      14:29
                    
                    
                      There's one song available,
                      14:30
                    
                    
                      and then I'm just going to go ahead
and I'm going to add another one.
                      14:32
                    
                    
                      I'm going to add another Michael Jackson.
                      14:34
                    
                    
                      Let's add Thriller.
                      14:36
                    
                    
                      There we go.
                      14:42
                    
                    
                      Okay, so there's two songs available.
                      14:43
                    
                    
                      So now let's try our new choose method.
                      14:45
                    
                    
                      Choose.
                      14:48
                    
                    
                      So there, there's only one
available artist.
                      14:50
                    
                    
                      So we'll choose Michael Jackson.
                      14:52
                    
                    
                      Your choices are PYT or Thriller.
                      14:54
                    
                    
                      So let's choose PYT.
                      14:57
                    
                    
                      There it goes.
                      14:59
                    
                    
                      It goes, you chose PYT by Michael Jackson.
                      15:00
                    
                    
                      So we'll clean this up,
your choice bit at the end there,
                      15:04
                    
                    
                      but this is looking great.
                      15:07
                    
                    
                      Real quick, we busted out a lot of code
just now, I realize that.
                      15:09
                    
                    
                      If you're starting to get lost
or confused, I recommend
                      15:13
                    
                    
                      following the chain of events,
starting at the top of our run method.
                      15:16
                    
                    
                      Follow the code down, and whenever you see
a method call, jump to that method.
                      15:19
                    
                    
                      See what it's doing,
and see what it's returning, etc.
                      15:24
                    
                    
                      Okay, let's go over and close that ticket.
                      15:28
                    
                    
                      That'll feel good.
                      15:30
                    
                    
                      So we can now browse properly.
                      15:32
                    
                    
                      We just need to do a little bit of cleanup.
And we started working on this too.
                      15:33
                    
                    
                      So here we're almost ready
to put the song in the queue.
                      15:38
                    
                    
                      Great. We're getting really
close to finishing this up!
                      15:41
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up