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
    
      
  Here we will use switches and maps to prompt a menu.
This video doesn't have any notes.
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 go ahead and add
                      0:01
                    
                    
                      the method to add a new song.
                      0:03
                    
                    
                      So private, it will return a song,
                      0:09
                    
                    
                      and we'll call it prompt new song.
                      0:12
                    
                    
                      Awesome.
                      0:18
                    
                    
                      And here we go.
                      0:19
                    
                    
                      So let's say system.out.print.
                      0:20
                    
                    
                      Enter the artist's name.
                      0:25
                    
                    
                      And again, we'll say string artist
                      0:30
                    
                    
                      equals mreader.readline.
                      0:34
                    
                    
                      We'll do the same
                      0:42
                    
                    
                      for the title and video URL.
                      0:42
                    
                    
                      So I'll just copy those two lines.
                      0:45
                    
                    
                      Change this to title.
                      0:54
                    
                    
                      and this is the title.
                      1:04
                    
                    
                      One more time.
                      1:07
                    
                    
                      Change this to video URL.
                      1:08
                    
                    
                      And change this to video URL.
                      1:17
                    
                    
                      Cool. So now let's make a new song
and return it.
                      1:23
                    
                    
                      So we return new song
                      1:27
                    
                    
                      artist, title, video URL.
                      1:32
                    
                    
                      Awesome.
                      1:38
                    
                    
                      So now that we're working with songs,
let's
                      1:40
                    
                    
                      go up and check our imports and make sure
that we imported that originally.
                      1:42
                    
                    
                      And we did not.
                      1:46
                    
                    
                      So let's make sure that we import song.
                      1:47
                    
                    
                      There we go.
                      1:53
                    
                    
                      Okay, so let's come back down into our run
method here
                      1:55
                    
                    
                      and let's get rid of this to-do
that we had added here.
                      1:57
                    
                    
                      Let's instead make a new song
                      2:02
                    
                    
                      that's equal to prompt new song.
                      2:04
                    
                    
                      So we'll ask for the new song there.
                      2:09
                    
                    
                      And then we will use our private songbook
                      2:13
                    
                    
                      to add the song.
                      2:15
                    
                    
                      Finally, let's provide a little feedback
                      2:19
                    
                    
                      to the user with System.out.printf
                      2:21
                    
                    
                      %s added.
                      2:25
                    
                    
                      And we'll add a couple new lines
there too.
                      2:32
                    
                    
                      And we'll use song,
which will show the toString
                      2:36
                    
                    
                      of what we built last time,
so it should look pretty there.
                      2:38
                    
                    
                      We can switch back to this karaoke here.
                      2:42
                    
                    
                      Okay let's switch this file
here to use our karaoke machine.
                      2:45
                    
                    
                      We need to import it so
com.teamtreehouse.karaokemachine. Here
                      2:48
                    
                    
                      let's come in and we can get rid
                      2:54
                    
                    
                      of all of this except for the line
where we create our songbook.
                      2:59
                    
                    
                      Now let's say, karaoke machine,
                      3:06
                    
                    
                      named machine, equals
                      3:10
                    
                    
                      new karaoke machine,
and that takes in a songbook,
                      3:13
                    
                    
                      so we'll pass in our new songbook
that was created there.
                      3:16
                    
                    
                      Then, we can just call machine.run,
                      3:20
                    
                    
                      and that will kick off the loop
that we created.
                      3:22
                    
                    
                      Nice.
                      3:26
                    
                    
                      Woof, we got three errors.
                      3:40
                    
                    
                      Let's see here.
                      3:42
                    
                    
                      These are coming from Karaoke Machine.
                      3:44
                    
                    
                      Let's go in there.
                      3:46
                    
                    
                      I forgot to throw on our new song
method here.
                      3:54
                    
                    
                      So it's an unreported exception,
IO exception.
                      3:57
                    
                    
                      So this is what happens
if you do not put the throws clause on.
                      4:00
                    
                    
                      So let's go ahead and say throws IO
exception.
                      4:04
                    
                    
                      I'm actually glad that I made this
mistake, as it's one I make often,
                      4:07
                    
                    
                      and now you know what to look for if
or when you see this error pop up.
                      4:11
                    
                    
                      So just like our prompt action method above,
we're calling that inside of the try block
                      4:15
                    
                    
                      and we're taking in user input in that method.
So we're saying this method can throw
                      4:20
                    
                    
                      an IO exception
                      4:24
                    
                    
                      because what the user types is unknown.
It might want to break things.
                      4:25
                    
                    
                      Since we're calling that inside
the try block here, we can catch it here.
                      4:30
                    
                    
                      Same thing goes for our prompt new song.
                      4:33
                    
                    
                      We're calling it inside of our try block.
                      4:36
                    
                    
                      So we can catch the exception there.
                      4:38
                    
                    
                      But we need to make sure
that our method states
                      4:40
                    
                    
                      it can throw an IO exception
in order to work.
                      4:42
                    
                    
                      Nice, right?
                      4:45
                    
                    
                      Okay, let's try that one more time.
                      4:47
                    
                    
                      Fingers crossed.
                      4:48
                    
                    
                      All right, so there's zero songs
available.
                      4:52
                    
                    
                      Your options are add,
add a new song to the songbook,
                      4:54
                    
                    
                      or quit, give up, and exit the program.
                      4:58
                    
                    
                      So let's say add, but I'm going to do it
in all capitals to make sure it works too.
                      5:00
                    
                    
                      Sweet.
                      5:07
                    
                    
                      Let's just do
Michael Jackson's Beat It again.
                      5:08
                    
                    
                      and I'm going to just put in example.com
here,
                      5:14
                    
                    
                      since this isn't going to save or
anything.
                      5:16
                    
                    
                      There we go. Awesome.
                      5:19
                    
                    
                      The song Beat It by Michael
Jackson is added now, so there's one
                      5:21
                    
                    
                      song available, so we could do add again,
                      5:24
                    
                    
                      or we could do quit.
                      5:27
                    
                    
                      There we go.
                      5:31
                    
                    
                      So this says,
thanks for playing. Excellent.
                      5:32
                    
                    
                      So we can definitely add
now, so let's go close that ticket.
                      5:35
                    
                    
                      Put this in done, and the menu seems like
                      5:39
                    
                    
                      we should be able to expand on it
pretty quickly should we need to.
                      5:42
                    
                    
                      Awesome job, everyone.
                      5:45
                    
              
        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