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 map out the classes that we will use to represent data in our application
Copy/Paste
// Video URL
"https://www.youtube.com/watch?v=SaEC9i9QOvk"
Resources
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
                      I find the easiest way to start on
a new project is to start with the model.
                      0:01
                    
                    
                      By building the model out
in accordance to a user story,
                      0:05
                    
                    
                      it helps make us make sure that the scope
is at the right level.
                      0:08
                    
                    
                      Earlier when we learned about packages,
we learned about how it helped
                      0:12
                    
                    
                      prevent naming conflicts.
                      0:15
                    
                    
                      But there are many more benefits
to packages.
                      0:17
                    
                    
                      They help group files
                      0:20
                    
                    
                      together, much like we use folders
and subfolders in our operating systems.
                      0:21
                    
                    
                      systems.
                      0:25
                    
                    
                      So let's go take a whack at that
first story and start by building
                      0:25
                    
                    
                      the models package.
                      0:28
                    
                    
                      Okay, so we want to build our model.
                      0:32
                    
                    
                      Our model will not contain
any user interface.
                      0:34
                    
                    
                      It will just be a way for us
to structure our data in a logical way
                      0:36
                    
                    
                      that other parts
of the application can use.
                      0:40
                    
                    
                      Now the way I see it,
                      0:43
                    
                    
                      there are a couple of things
that we should attempt to model.
                      0:44
                    
                    
                      Well, first things first, let's review
the first story here.
                      0:47
                    
                    
                      It says, as an admin, I can add a new song
                      0:51
                    
                    
                      so that songs that are wanted by
the singers are available.
                      0:54
                    
                    
                      Okay,
so we definitely need to model a song.
                      0:57
                    
                    
                      Well, let's look through the rest here.
                      1:00
                    
                    
                      I see songs by artist, so we know there's
some sort of list of songs. But
                      1:02
                    
                    
                      importantly, we know that the song needs
to know the artist associated with it.
                      1:07
                    
                    
                      So the last story down here
talks about the video should be available.
                      1:12
                    
                    
                      So we should probably add a URL
to get the YouTube video.
                      1:16
                    
                    
                      So it looks like
our model will be song and list of songs.
                      1:20
                    
                    
                      Again,
because we're trying to limit our scope,
                      1:24
                    
                    
                      or features,
being developed to these stories only,
                      1:26
                    
                    
                      we want to make sure
that we don't build outside of it.
                      1:29
                    
                    
                      For instance,
we might be inclined to model a singer,
                      1:32
                    
                    
                      but you'll notice
there's no stories that require that.
                      1:35
                    
                    
                      At least now. In the future
                      1:38
                    
                    
                      it might, but that's the future
and it's not our concern right now.
                      1:39
                    
                    
                      Minimum viable product.
                      1:42
                    
                    
                      So let's get started on this first story.
                      1:45
                    
                    
                      Let's build a model package.
                      1:50
                    
                    
                      So let's go over here and create
a new folder and we'll call this com.
                      1:52
                    
                    
                      Under that new folder we'll add a team
treehouse
                      1:56
                    
                    
                      folder.
                      1:59
                    
                    
                      Then we're going to add a new package
                      2:05
                    
                    
                      called the model package.
                      2:06
                    
                    
                      Great, and in here we'll add a new file.
                      2:12
                    
                    
                      That file will be song.java.
                      2:15
                    
                    
                      Great, so
                      2:18
                    
                    
                      first things first, in our code
we'll say package
                      2:22
                    
                    
                      com.teamtreehouse.model
                      2:25
                    
                    
                      Sweet!
                      2:28
                    
                    
                      Let's build our new class.
                      2:32
                    
                    
                      So we know it's a public class
                      2:35
                    
                    
                      and it's a song.
                      2:37
                    
                    
                      And let's
go ahead and make these songs immutable.
                      2:41
                    
                    
                      We want to expose the ability
to change them to the users of our class.
                      2:44
                    
                    
                      So we know that there was an artist.
                      2:48
                    
                    
                      So we'll just start
with the string version of the artist
                      2:50
                    
                    
                      because we didn't
see any need to build those other classes.
                      2:52
                    
                    
                      There's also a title of the song,
                      2:56
                    
                    
                      and there's also the video URL that we saw
that was pretty important.
                      2:58
                    
                    
                      So let's go ahead and build all those
private variables that are not exposed.
                      3:02
                    
                    
                      And they're also not initialized,
                      3:08
                    
                    
                      so what we'll do
is we'll make a constructor
                      3:09
                    
                    
                      so that when you create one of these new
songs, you need to give it those values.
                      3:11
                    
                    
                      So again, it's
the same name as the class there
                      3:15
                    
                    
                      and we'll be expecting a string for artist,
                      3:18
                    
                    
                      title and video URL.
                      3:22
                    
                    
                      Okay.
                      3:30
                    
                    
                      And before we just declared them, let's
go ahead
                      3:31
                    
                    
                      and initialize them to
whatever was passed in.
                      3:33
                    
                    
                      And because it starts with an M,
we can tell the difference
                      3:36
                    
                    
                      between the two.
                      3:39
                    
                    
                      We know that M is our member variable.
                      3:40
                    
                    
                      So the member variable title
is getting set to title.
                      3:43
                    
                    
                      The member variable video
URL is being set to video URL.
                      3:46
                    
                    
                      Great.
                      3:51
                    
                    
                      Okay, next we'll expose what we want
the users of our class to see.
                      3:52
                    
                    
                      So we'll put the getters out there.
                      3:55
                    
                    
                      I'd like to
offer this to you as a little challenge.
                      3:58
                    
                    
                      Go ahead and create the public getter
methods that will each return
                      4:01
                    
                    
                      one of our private variables here. Ready?
                      4:04
                    
                    
                      Go ahead and pause me.
                      4:07
                    
                    
                      How'd you do?
                      4:11
                    
                    
                      If you got stuck,
don't worry, let's go through it.
                      4:12
                    
                    
                      Again, the IDEs you'll be using soon
will generate this kind of stuff for you.
                      4:15
                    
                    
                      It's absolutely worth knowing how to do it
and how it works though first.
                      4:19
                    
                    
                      So we'll do public string get artist
                      4:24
                    
                    
                      and it will return our private M artist
                      4:28
                    
                    
                      I'm actually just going to copy this one,
                      4:35
                    
                    
                      paste it here, I'll format it correctly,
                      4:38
                    
                    
                      paste it again and I'll just adjust these.
                      4:42
                    
                    
                      And we'll do get title again
this is going to simply return the title.
                      4:49
                    
                    
                      Finally, we'll do get video URL
                      4:57
                    
                    
                      and return m video URL.
                      5:00
                    
                    
                      At this point of our learning,
this might seem a lot of overkill.
                      5:06
                    
                    
                      The usage of these getters,
and sometimes when we build setters,
                      5:09
                    
                    
                      will be much more obvious
as we start building more in depth API's.
                      5:12
                    
                    
                      But for now,
                      5:16
                    
                    
                      just know that we're building things out
this way to make the instance immutable.
                      5:17
                    
                    
                      We can only get things out.
                      5:21
                    
                    
                      We can't set stuff, right?
                      5:22
                    
                    
                      So let's add some stuff here,
just a couple lines.
                      5:26
                    
                    
                      So we want to make the instance look good,
if it's ever used as a string.
                      5:29
                    
                    
                      So let's override the base
object's toString method.
                      5:34
                    
                    
                      And my personal favorite way to do
this is to use that string format trick
                      5:38
                    
                    
                      that we learned earlier.
                      5:41
                    
                    
                      So we're going to say that this is a song.
                      5:46
                    
                    
                      We'll say %s by %s, %n for a new line.
                      5:48
                    
                    
                      line.
                      5:53
                    
                    
                      So we want the title of the song
by the artist
                      5:54
                    
                    
                      so that if it ever was printed out,
it would look really nice.
                      5:57
                    
                    
                      Great.
                      6:00
                    
                    
                      Okay, now let's build that list of songs.
                      6:01
                    
                    
                      Now this is one of those places
where you might want
                      6:03
                    
                    
                      to just let the users of your code
                      6:05
                    
                    
                      build their own list using that collection
framework themselves.
                      6:07
                    
                    
                      But looking at the rest of the stories
that we had, it looks
                      6:11
                    
                    
                      there might be some convenience methods
that we might want to expose.
                      6:14
                    
                    
                      So let's actually build a songbook class
that will help provide
                      6:17
                    
                    
                      those helper methods.
                      6:20
                    
                    
                      So let's add the songbook class
to this model package here.
                      6:22
                    
                    
                      songbook.java
                      6:26
                    
                    
                      Cool and let's add it to the package.
                      6:29
                    
                    
                      So we'll say package com.teamtreehouse.model.
                      6:34
                    
                    
                      And we'll make a
                      6:41
                    
                    
                      public class songbook.
                      6:42
                    
                    
                      Alright so we have a list of songs
so we should definitely use a list.
                      6:46
                    
                    
                      And since we're declaring it,
we should use the interface only,
                      6:52
                    
                    
                      just like we do
with normal variable declarations.
                      6:55
                    
                    
                      So let's keep things private, just in case
we want to change the implementation
                      6:58
                    
                    
                      of the whole songbook.
                      7:01
                    
                    
                      So we'll say private list
                      7:03
                    
                    
                      the interface list of song msongs.
                      7:05
                    
                    
                      Now you'll note
I'm going to have to import lists here.
                      7:10
                    
                    
                      So say import java.util.list.
                      7:13
                    
                    
                      But you'll notice
that I didn't have to import song,
                      7:20
                    
                    
                      and that's because song is assumed
to be in the same package, which it is.
                      7:22
                    
                    
                      We're both on the com.teamtreehouse.model
package, so it's there.
                      7:26
                    
                    
                      That's another benefit of using packages.
                      7:31
                    
                    
                      You don't need to import things
from that same package.
                      7:33
                    
                    
                      Okay, so we'll say public songbook.
                      7:39
                    
                    
                      We won't make the constructor
take anything in, but what we'll do
                      7:42
                    
                    
                      is we'll initialize that list of songs.
                      7:45
                    
                    
                      And we should, of course,
use the array list
                      7:48
                    
                    
                      we learned.
                      7:51
                    
                    
                      And we'll need to import that too,
so let's add that up top,
                      7:54
                    
                    
                      keeping these in alphabetical order
like the good programmers
                      7:58
                    
                    
                      that we are.
                      8:00
                    
                    
                      Okay so let's provide a way
for the song book
                      8:06
                    
                    
                      to accept new songs to be added
just as the story requested.
                      8:08
                    
                    
                      So we'll say public
                      8:14
                    
                    
                      and it doesn't need
to return anything, add song
                      8:16
                    
                    
                      and it takes a song.
                      8:19
                    
                    
                      Then we just say mSongs.add song.
                      8:23
                    
                    
                      Cool,
so now we have a way to add the songs.
                      8:27
                    
                    
                      And if we want to, we can expose
different things off of that list class.
                      8:30
                    
                    
                      But we don't have to expose everything.
                      8:34
                    
                    
                      It'll be our choice of what comes out.
                      8:36
                    
                    
                      So let's go ahead and let's
just show off that we can do that.
                      8:38
                    
                    
                      Let's go ahead
and let's get the song count.
                      8:41
                    
                    
                      We'll return the
number of how many songs are available.
                      8:47
                    
                    
                      And we know that
that's going to use our internal list,
                      8:50
                    
                    
                      which isn't exposed.
                      8:52
                    
                    
                      There.
                      8:58
                    
                    
                      I think that should wrap
                      8:59
                    
                    
                      the Songbook class up for the model
size of this first story.
                      9:00
                    
                    
                      We've encapsulated the way
that we're actually storing the data
                      9:03
                    
                    
                      so that we can change
the underlying implementation at any time,
                      9:06
                    
                    
                      and the users of our class
would never know.
                      9:10
                    
                    
                      Now, since we don't have any unit tests
to exercise our code, though
                      9:13
                    
                    
                      we should, let's go write
the main code that we'll be using
                      9:17
                    
                    
                      and just make sure things are working
we expected.
                      9:20
                    
                    
                      So let's close this folder up.
                      9:23
                    
                    
                      Let's go here and make a new file
called karaoke.java
                      9:25
                    
                    
                      right here in the root.
                      9:30
                    
                    
                      Okay, now we're not in the package
                      9:34
                    
                    
                      so we're going to have to import the stuff
that we created, the models.
                      9:36
                    
                    
                      import com.teamTreehouse.model.song
                      9:39
                    
                    
                      and songbook.
                      9:49
                    
                    
                      Okay,
so we're going to create the main class.
                      9:51
                    
                    
                      Again, it's the same name as the file
so we say public
                      9:53
                    
                    
                      class karaoke
                      9:58
                    
                    
                      The static
                      10:02
                    
                    
                      method named main
takes a string array of arguments
                      10:03
                    
                    
                      and returns nothing.
                      10:06
                    
                    
                      Right, so in here we'll make a new song.
                      10:15
                    
                    
                      I'm thinking
                      10:22
                    
                    
                      this is going to get kind of long,
                      10:22
                    
                    
                      so I'm just going to write this
on multiple lines.
                      10:23
                    
                    
                      So one of my favorite songs is Michael
Jackson's Beat It.
                      10:26
                    
                    
                      So let's go ahead and put that in there.
                      10:30
                    
                    
                      Michael Jackson, so it's artist title
and then we have the video
                      10:32
                    
                    
                      URL I've gone ahead
and I searched YouTube already.
                      10:37
                    
                    
                      This will be in the teacher's notes
so you can copy and paste it in.
                      10:40
                    
                    
                      The video has been on YouTube for
a long time already, so hopefully it stays.
                      10:44
                    
                    
                      If you're from the future
and this link is broken,
                      10:48
                    
                    
                      you can feel free to search
and find one that works.
                      10:50
                    
                    
                      Okay, so now that we have a new song
                      10:56
                    
                    
                      let's make a new song book.
                      10:58
                    
                    
                      And we'll do system.out.printf
                      11:08
                    
                    
                      adding
                      11:14
                    
                    
                      %s%n.
                      11:15
                    
                    
                      Because we overrode toString, it's
                      11:18
                    
                    
                      going to look pretty.
                      11:20
                    
                    
                      So we'll do
                      11:24
                    
                    
                      songbook.addSong,
the method that we exposed here.
                      11:25
                    
                    
                      You can kind of see that the user doesn't
know it's a list underneath.
                      11:29
                    
                    
                      It's just a songbook that we're adding to.
                      11:32
                    
                    
                      And let's print out the amount.
                      11:34
                    
                    
                      There are %d, for numbers, songs.
                      11:40
                    
                    
                      And of course, it's
going to look a little bit weird,
                      11:45
                    
                    
                      because if there's one, it's going to say
there are one songs.
                      11:47
                    
                    
                      That is a common problem.
                      11:51
                    
                    
                      So we're going to say, get song count.
                      11:52
                    
                    
                      And again, a user of this class
doesn't really know that it's
                      11:55
                    
                    
                      a list underneath, and they could have
just called songs.size.
                      11:58
                    
                    
                      Makes sense.
Or any other method for that matter.
                      12:01
                    
                    
                      Okay, let's go ahead and close this out,
                      12:05
                    
                    
                      and I think we're ready.
                      12:07
                    
                    
                      Let's go ahead and see.
                      12:12
                    
                    
                      So make sure all your files are saved,
                      12:13
                    
                    
                      and we'll say clear, and javac,
karaoke.java,
                      12:16
                    
                    
                      and java karaoke.
                      12:22
                    
                    
                      oh, what now?
                      12:26
                    
                    
                      Songbook.
                      12:30
                    
                    
                      Oh, I didn't camel case
my usage of the variable.
                      12:31
                    
                    
                      Sorry about that.
                      12:36
                    
                    
                      Let's try that again.
                      12:47
                    
                    
                      Awesome.
                      12:55
                    
                    
                      Adding song Beat It by Michael Jackson.
                      12:56
                    
                    
                      So that's using the string representation
that we did, right?
                      12:59
                    
                    
                      So remember we did the %s by %s?
                      13:02
                    
                    
                      So Beat It by Michael Jackson,
and there are one songs.
                      13:05
                    
                    
                      There's that hilarious bug
I was talking about.
                      13:10
                    
                    
                      So I don't think we can quite close
that first story, but it's super close.
                      13:12
                    
                    
                      We'll need to build some way for the user
interface to call that add song method.
                      13:16
                    
                    
                      Let's take a quick break and get right
back to building that user interface.
                      13:21
                    
              
        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