The Model6:23 with Craig Dennis
Determining how we will keep track of our important data is important. Let's start building the model.
We are using the m prefixed field naming convention as defined here in the Android coding style guide.
Your settings should be set to do this because we set things up like this in the Local Development Environments course. If you wanted to set this you can go to File > Other Settings > Editor > Code Style > Java > Code Generation > Naming > Field and then there in the name prefix add an m
Let's work on the m part of the MVC pattern, the model. 0:00 So what do we know about our model? 0:05 Well from what I can tell from the user stories, is it looks like we have 0:07 a timed attempt to complete either a focus time, or a break time. 0:11 The attempt, whether it is a focus or a break, is pretty much the same thing, 0:17 as far as functionality is concerned. 0:21 It just runs for a different length of time. 0:22 There are a couple of ways of solving this issue where we have very similar models. 0:26 Now, one is to use inheritance and override certain properties and 0:30 methods, but that seems like a little overkill for this problem, though. 0:34 As well as I wanna show you a new type that you haven't seen yet. 0:38 Enum types or Enoom types are one of those pronunciation arguments that 0:43 developers get into from time to time. 0:47 It's the age-old [SOUND] is it Super, Mar-ee-oh, Brothers or 0:50 is it [SOUND] Super, Mair-ee-oh, Brothers dilemma. 0:53 Now enum is short for enumerated, and it's a grouping of known constants. 0:56 The most common way I've seen this explained is using the days of the week. 1:02 We know there is seven and that they're pretty constant. 1:05 Right? 1:07 So we have an enum of day and it's got all the days of the week there. 1:08 As you can imagine, you can store this as an integer and use zero to six. 1:12 But this makes for much clearer code. 1:16 You can reference it and use enum as a type. 1:19 So you can do something like this. 1:22 Like here's a function that sings That One Song and you pass it a day. 1:24 And you could say if day = Friday, then print out It's Friday, 1:27 Friday, gotta get down, it's Friday. 1:31 You know that song that you never wanted to hear again? 1:33 Sorry. 1:36 So not only does it help with clarity, but it also introduces some additional power. 1:37 When you know all types expected, you can loop through them. 1:42 The order matters, too, so Saturday is greater that Friday. 1:46 Java adds some additional power to enums that other programming languages don't, so 1:50 I want you to understand some of these features. 1:55 Let's go build our model. 1:57 Okay, so let's start on our attempt model first. 2:00 So let's make a new package here under com.teamtreehouse.pomidoro called 2:04 model and then let's add a new class and call it attempt. 2:07 Now I could right click here and choose new package. 2:10 But I want to show you a different way. 2:14 Let's say new JavaClass, and 2:15 I'm just going to name the package, even though it doesn't exist, model.attempt. 2:16 And you'll see what it does is it goes ahead and it builds that model package 2:20 from where I started at, and then it creates a new attempt. 2:23 That's pretty cool. 2:26 So we know that we want to track the message that they added about, 2:28 what they were doing down in the text area. 2:33 So we have private String_mMessage. 2:35 And we also know that we want to track how many remaining 2:39 seconds they have on this specific attempt right, so 2:44 let's do private int_m remaining seconds. 2:49 Great. Okay. So now, 2:55 this is where we wanna know what type of attempt is happening. 2:56 But what do we call that? 2:59 I mean, we don't wanna call it type, right, because that's confusing. 3:01 We have different types already in Java. 3:03 And that's a naming conflict. 3:06 And then we could call it style, like, what style of attempt is this? 3:07 But, no, that's also a naming conflict from like CSS styles. 3:11 What if we called it kind? 3:17 How about an AttemptKind? 3:19 So let's say private, we'll just name it AttemptKind, and we'll call it nKind. 3:20 All right, so it doesn't know what it is, obviously, 3:30 because we just made that word up. 3:32 And if we come over here and we say, Create enum AttemptKind. 3:34 And it's gonna ask if it should go in the same package, and indeed it should. 3:38 Okay. 3:42 So let's choose to add FOCUS and BREAK. 3:44 And we'll put them in all caps, 3:47 because that lets people know that they're constants. 3:48 That's a good standard, it's been around for a while. 3:50 Okay, so now we can refer to the constants of FOCUS and BREAK, but 3:53 they also kind of have constants themselves, right. 3:56 I mean it'd be nice to find the FOCUS time is 25 minutes and 3:59 BREAK time is five minutes. 4:01 Well, the good news is that you can do that in Java. 4:02 So much like a class definition, you can add fields and 4:05 even methods to each item in enum. 4:08 So let's do that. 4:10 So we'll say private int mTotalSeconds. 4:12 Right? 4:16 Cuz that's what we're looking at, 4:17 extra info that we're looking for each one of these. 4:18 Cuz that's kind of what makes them different, 4:20 on top of being named differently. 4:22 Right? 4:24 So we'll use code generation to generate a constructor for that, 4:25 cause it works just like the same way, and we'll cool. 4:28 Perfect. 4:32 And we'll also do a getter for it. 4:33 Let's do a getter. 4:36 You'll see, up here,that it is asking for, 4:36 it's saying hey, I don't know what you're talking about, so 4:40 what you do is you actually pass it in what the constructor expects. 4:44 Right? So it expects TotalSeconds. 4:49 So, what is TotalSeconds? 4:51 So we have 25 minutes, we know that. 4:53 So times 60 right, that's the math for that. 4:55 And then BREAK we want five minutes, and 4:58 there are 60 seconds in a minute so, five times 60, bam. 5:00 Okay and if flip back over to our attempt model here, we can go ahead now and 5:05 build ourselves a constructor. 5:10 So let's build a constructor and let's just pass in kind and message. 5:14 Let's do that. 5:20 Just kind and message. 5:22 Remaining seconds, what we'll do is we'll set the remaining seconds from the kind. 5:24 GetTotalSeconds, sorry. 5:32 So the remaining seconds that set on the attempt when it's first created, 5:34 will just set it to be the expected time. 5:37 Cool, right? 5:39 Now let's go ahead and add getters for everything. 5:41 So all three of those, 5:45 we want to be able to get the kind out the message in the remaining seconds. 5:48 And then let's go ahead and let's add a setter, but let's only add a setter for 5:51 the message, right? 5:55 I don't think we should be changing the kind ever. 5:57 And the remaining seconds, we'll change that but 5:59 let's change it through a different method. 6:02 Awesome I think we're ready to use this model. 6:05 I'm glad we got a chance to explore enums or enums, I don't wanna offend anybody. 6:08 They're certainly powerful for grouping constants. 6:12 We'll use a little more of their super powers as we wire this app together. 6:15 Speaking of wiring it together, let's go do that. 6:19 Right after this. 6:21
You need to sign up for Treehouse in order to download course files.Sign up