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
    
      
  In this video we will determine what our UI, or User Interface, will look like. It's a console-based application, so we'll explore some ways to display dynamic menus.
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
                      Okie dokie, so let's build up that user
interface.
                      0:00
                    
                    
                      Okay, we only have one UI component,
                      0:04
                    
                    
                      so I'm not going to build
a brand new package just for it.
                      0:06
                    
                    
                      So let's just go ahead and drop this
at the Team Treehouse package level.
                      0:09
                    
                    
                      Okay,
so come in here to Team Treehouse level,
                      0:14
                    
                    
                      and I'm going to add a new file
                      0:17
                    
                    
                      right here, and I'm going to call it
karaokemachine.java.
                      0:18
                    
                    
                      This will be the UI portion.
                      0:22
                    
                    
                      Okay, so let's put it in a package
                      0:28
                    
                    
                      com.teamtreehouse. Cool.
                      0:32
                    
                    
                      So let's make the new class. Let's say
public
                      0:35
                    
                    
                      class karaoke machine
                      0:38
                    
                    
                      You'll get very good at spelling that
by the end of this. Alright, let's go ahead
                      0:40
                    
                    
                      and say on the creation of one of these
karaoke machines, let's give it a songbook.
                      0:45
                    
                    
                      Now we
                      0:55
                    
                    
                      need to import the songbook
because we're not in that package anymore.
                      0:56
                    
                    
                      So import
com.teamtreehouse.model.songbook.
                      0:59
                    
                    
                      And let's make that a private variable
in the karaoke machine.
                      1:08
                    
                    
                      So we'll do private
songbook and msongbook.
                      1:12
                    
                    
                      And then let's assign it in
the constructor.
                      1:18
                    
                    
                      mSongBook member variable
is equal to SongBook that got passed in.
                      1:20
                    
                    
                      Great.
                      1:25
                    
                    
                      Okay, so I'm going to show you
a different approach to what we were doing
                      1:27
                    
                    
                      with the console, and this probably
would have seemed insane before,
                      1:30
                    
                    
                      but now it should seem,
I don't know, less insane.
                      1:34
                    
                    
                      So let's use a reader
for our user's input.
                      1:38
                    
                    
                      Just as we use System.out to print output,
                      1:40
                    
                    
                      we can use System.in to read
input from the console.
                      1:43
                    
                    
                      To make it easier to work with,
we can use the input stream reader class,
                      1:48
                    
                    
                      which converts the byte-based input stream
into a character-based stream.
                      1:52
                    
                    
                      However, reading input one character
at a time isn't very efficient.
                      1:56
                    
                    
                      That's why it's common practice
                      2:01
                    
                    
                      to wrap the input stream
reader in a buffered reader.
                      2:02
                    
                    
                      The buffered reader buffers the input,
allowing us to read entire lines at once
                      2:06
                    
                    
                      and improving performance
                      2:10
                    
                    
                      by reducing the number of conversions
from bytes to characters.
                      2:11
                    
                    
                      So let's
go ahead and create a new buffered reader
                      2:16
                    
                    
                      to handle our console
input more efficiently.
                      2:18
                    
                    
                      It takes an input stream,
so let's do that.
                      2:21
                    
                    
                      Let's actually save this
                      2:26
                    
                    
                      to a member variable up top as well.
                      2:27
                    
                    
                      Private buffered reader,
                      2:31
                    
                    
                      and let's call it mReader.
                      2:35
                    
                    
                      Then we can assign it
                      2:41
                    
                    
                      this buffered reader down here
as its value.
                      2:42
                    
                    
                      Awesome.
                      2:47
                    
                    
                      So we got some imports to do.
                      2:48
                    
                    
                      It's all in the Java IO package.
                      2:50
                    
                    
                      So let's say import
java.io.bufferedreader
                      2:52
                    
                    
                      and import 
java.io.inputstreamreader
                      3:00
                    
                    
                      And the method
                      3:04
                    
                    
                      that we're going to use throws
a thing called an IO exception.
                      3:05
                    
                    
                      So I'm just going to go ahead and import
that while we here.
                      3:08
                    
                    
                      Import java.io.ioexception.
                      3:11
                    
                    
                      Okay, now
maps are pretty good for building a menu.
                      3:15
                    
                    
                      So if you want options,
you can have the choice
                      3:20
                    
                    
                      and then what the choice does.
                      3:22
                    
                    
                      It's really good to do that in a map.
                      3:24
                    
                    
                      And of course, that looks like
a map of string to string.
                      3:26
                    
                    
                      So let's let's create
a new thing called M menu.
                      3:30
                    
                    
                      And it will be a hash map.
                      3:35
                    
                    
                      And it will be of string to string.
                      3:37
                    
                    
                      Let's declare our variable,
we'll say that it's a private map.
                      3:45
                    
                    
                      Again, we're using the interface, string
                      3:49
                    
                    
                      string and M menu.
                      3:52
                    
                    
                      While we're up here
let's go ahead and import that too.
                      3:55
                    
                    
                      Import Java.util.hashmap
                      3:58
                    
                    
                      import Java.util.map, great!
                      4:06
                    
                    
                      So we have this map, let's go ahead
and put some menu options in there.
                      4:10
                    
                    
                      So let's make an option to add.
                      4:14
                    
                    
                      We'll say add a new song to the songbook.
                      4:21
                    
                    
                      Cool.
                      4:26
                    
                    
                      And we can put as many as we want in here,
but for now, let's just put in one more.
                      4:27
                    
                    
                      Let's put in quit.
                      4:31
                    
                    
                      And we'll say give up, exit the program.
                      4:34
                    
                    
                      Nice.
                      4:41
                    
                    
                      Let's go ahead and start using this menu
that we just created.
                      4:43
                    
                    
                      So let's make a private method.
                      4:46
                    
                    
                      There's really
no need to expose this outside of what
                      4:48
                    
                    
                      we're going to be working on. This is going
to be private to this karaoke machine.
                      4:50
                    
                    
                      So we'll say private string prompt action
                      4:55
                    
                    
                      Okay let's write this out.
                      5:02
                    
                    
                      There are percent D
                      5:07
                    
                    
                      songs available
                      5:09
                    
                    
                      your options are
                      5:13
                    
                    
                      and we'll do a new line
                      5:14
                    
                    
                      and let's put there
mSongBook.getSongCount,
                      5:18
                    
                    
                      just we did before.
                      5:22
                    
                    
                      Now, let's go ahead
and let's loop through that menu.
                      5:28
                    
                    
                      So remember, when you want to loop
through a map, it returns that
                      5:30
                    
                    
                      inner class, the map.entry.
                      5:33
                    
                    
                      And this one will be string, string,
                      5:36
                    
                    
                      and we'll call that option.
                      5:39
                    
                    
                      For each one of those options
in the menu.entry set,
                      5:42
                    
                    
                      And we'll say
                      5:52
                    
                    
                      system.out.printf and we want to say,
                      5:53
                    
                    
                      we'll say whatever the value was in the
key is whatever the description is, right?
                      5:57
                    
                    
                      And then we'll make a new line.
                      6:02
                    
                    
                      And we do option.getKey
                      6:04
                    
                    
                      and option.getValue.
                      6:08
                    
                    
                      So we'll just loop through each one of those
and print those out. Cool.
                      6:10
                    
                    
                      So now
                      6:15
                    
                    
                      let's prompt
the user for what they want to do.
                      6:15
                    
                    
                      We'll say system.out.print.
                      6:19
                    
                    
                      I'm using print and not the print line
because I want it to be on the same line.
                      6:22
                    
                    
                      We'll say, what do you want to do?
                      6:27
                    
                    
                      Okay, so now we can use that reader object
that we created.
                      6:31
                    
                    
                      Let's say mReader.readLine.
                      6:34
                    
                    
                      and we'll store it in a variable here.
                      6:40
                    
                    
                      Let's say string choice equals
                      6:42
                    
                    
                      mReader.readLine.
                      6:45
                    
                    
                      Now remember when I said
that this can throw an exception?
                      6:48
                    
                    
                      Now I don't really want to handle that
in this method.
                      6:51
                    
                    
                      I'd rather make it
so that whoever called this prompt action,
                      6:54
                    
                    
                      if that exception happened,
they handle it.
                      6:57
                    
                    
                      So I'm kind of saying
this is not my responsibility.
                      7:00
                    
                    
                      So what you can do
is you can bubble up exceptions.
                      7:03
                    
                    
                      So because that throws an exception,
it would expect me
                      7:06
                    
                    
                      to wrap that in a try catch,
but I don't want to do that.
                      7:09
                    
                    
                      So I'm just going to come up here and I'm
going to say this is not my problem
                      7:12
                    
                    
                      and say throws IO exception.
                      7:16
                    
                    
                      So now I don't need to wrap that
in a try catch
                      7:19
                    
                    
                      because it knows that it throws
an IO exception.
                      7:21
                    
                    
                      Whoever calls prompt action will either
need to catch that or also bubble that up.
                      7:24
                    
                    
                      So we'll catch it,
but I'll show you here in a moment.
                      7:29
                    
                    
                      So now we're going to return the choice,
but let's format this a little bit.
                      7:32
                    
                    
                      So let's do a quick validation with trim,
which takes off
                      7:36
                    
                    
                      the empty space at the beginning
and the end of the string.
                      7:39
                    
                    
                      And then let's do it to lowercase.
                      7:42
                    
                    
                      And that way,
                      7:44
                    
                    
                      if somebody, you know, has caps
lock on or something that, we'll catch it.
                      7:45
                    
                    
                      Nice.
                      7:48
                    
                    
                      Okay, so let's build the run loop here.
                      7:50
                    
                    
                      It won't return anything.
                      7:52
                    
                    
                      And basically,
                      7:54
                    
                    
                      what we're going to do
                      7:55
                    
                    
                      is just loop until we get the value
that we want out of there.
                      7:55
                    
                    
                      So let's initialize
another string variable
                      7:59
                    
                    
                      called choice so that we can use it
in our do while loop.
                      8:01
                    
                    
                      We want this prompt to happen
at least once no
                      8:05
                    
                    
                      matter what, so I think a do
while loop is a great choice here.
                      8:07
                    
                    
                      So remember, we're just going to say do,
                      8:10
                    
                    
                      add our curly braces,
                      8:13
                    
                    
                      and we'll say while not
                      8:17
                    
                    
                      with that logical
not operator there, choice.equals quit.
                      8:19
                    
                    
                      So we want this to go until quit
is the user's choice.
                      8:24
                    
                    
                      All right, nice.
                      8:28
                    
                    
                      And don't
forget your semicolon on this one.
                      8:29
                    
                    
                      So inside the do block, let's say
that choice is equal to prompt action.
                      8:32
                    
                    
                      So it will hold the value returned
by the method we just created up there.
                      8:38
                    
                    
                      Now remember, prompt action
now throws an IO exception.
                      8:42
                    
                    
                      So let's wrap this in a try catch.
                      8:46
                    
                    
                      So we'll say try,
                      8:48
                    
                    
                      catch,
                      8:55
                    
                    
                      and that
prompt action throws an IO exception.
                      8:58
                    
                    
                      So let's name that IOE and we'll say
                      9:00
                    
                    
                      system.out.println problem
                      9:04
                    
                    
                      with input.
                      9:08
                    
                    
                      Then we can say IOE
                      9:12
                    
                    
                      and use that nice print stack trace.
                      9:14
                    
                    
                      Cool. Okay so now we have a choice
from the menu that they typed there
                      9:18
                    
                    
                      and we could do a bunch of if statements,
but I'd to show you a new construct
                      9:23
                    
                    
                      and it's normally used for situations
exactly like this.
                      9:27
                    
                    
                      So instead of saying if the choice block
is equal to add, then do this.
                      9:31
                    
                    
                      If the choice is equal to quit,
then do this.
                      9:35
                    
                    
                      There's a keyword called switch.
                      9:38
                    
                    
                      So we'll say switch.
                      9:40
                    
                    
                      Tfhen you tell it
what you want to switch on.
                      9:44
                    
                    
                      So we want to switch on choice.
                      9:46
                    
                    
                      So in here, you define every possibility
that you want to catch.
                      9:49
                    
                    
                      And each thing that you want to catch
is referred to as a case.
                      9:53
                    
                    
                      So if the case happens
where choice is add,
                      9:56
                    
                    
                      then you do a colon,
and this will be the code that runs.
                      9:59
                    
                    
                      So in here,
we'll add a to do for now, add a new song.
                      10:03
                    
                    
                      Now this
                      10:08
                    
                    
                      is really important here,
you need to break the case.
                      10:08
                    
                    
                      Otherwise, the code will fall
through to the following case.
                      10:11
                    
                    
                      The fall through allows you to write code
that runs and meets multiple cases,
                      10:15
                    
                    
                      but in this situation,
it's not what we want.
                      10:18
                    
                    
                      So we want to call the keyword break,
which will break us out of the switch
                      10:21
                    
                    
                      clause.
                      10:24
                    
                    
                      We'll walk through this once
more here in a bit.
                      10:26
                    
                    
                      So let's add quit as well.
                      10:28
                    
                    
                      So now we have another case.
                      10:33
                    
                    
                      So if it matches on quit,
let's go ahead and say
                      10:35
                    
                    
                      thanks for playing
                      10:39
                    
                    
                      and then again
we'll break out of that statement.
                      10:42
                    
                    
                      There's also an optional case
if none of the cases are found
                      10:46
                    
                    
                      and that's called default. In that case
we want to say
                      10:49
                    
                    
                      hey I don't know what you're talking about.
So we'll say
                      10:53
                    
                    
                      system dot out dot print f
                      10:56
                    
                    
                      unknown choice
and then we'll put that in some quotes
                      10:59
                    
                    
                      here to show them that we don't know
and then we'll say try again.
                      11:02
                    
                    
                      And let's give us a couple new lines.
                      11:09
                    
                    
                      And we should pass in choice
                      11:14
                    
                    
                      so that we can show them what they typed
and that we don't know, all right.
                      11:15
                    
                    
                      Okay so let's walk that one more time here.
So when our run method is called
                      11:21
                    
                    
                      we're going to initialize choice
as an empty string.
                      11:24
                    
                    
                      We're going to start our loop here.
                      11:28
                    
                    
                      This will run
as long as choice is not equal to quit,
                      11:30
                    
                    
                      but it's going to go through one time no
matter what because it's a do while loop.
                      11:33
                    
                    
                      So it's going to start from the top.
                      11:37
                    
                    
                      It's going to come in here
and it's going to try this prompt action,
                      11:39
                    
                    
                      which throws an IO exception because
we're awesome and we told it to do that.
                      11:42
                    
                    
                      So it's going to throw out an IO exception
if there's a problem
                      11:46
                    
                    
                      and it's going to get caught here.
                      11:50
                    
                    
                      If that happens,
choice will still not have changed.
                      11:52
                    
                    
                      So it would go back
and would continue to loop here.
                      11:55
                    
                    
                      Okay, so we'll come in
and we'll get a choice back
                      11:58
                    
                    
                      and let's assume that it's add
this time it comes in and it'll say add
                      12:01
                    
                    
                      and it'll run the add code it'll hit break
and it will drop out of this method.
                      12:06
                    
                    
                      It'll come down here and it's not quit
so it'll go again.
                      12:11
                    
                    
                      So you can keep on adding songs
eventually it'll come here and say quit.
                      12:14
                    
                    
                      It will quit and it'll break out
and when it comes here it'll drop
                      12:19
                    
                    
                      out of the loop. Make sense? 
Awesome. Nice job everyone.
                      12:22
                    
              
        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