1 00:00:00,460 --> 00:00:04,090 Let's work on the m part of the MVC pattern, the model. 2 00:00:05,300 --> 00:00:07,580 So what do we know about our model? 3 00:00:07,580 --> 00:00:11,200 Well from what I can tell from the user stories, is it looks like we have 4 00:00:11,200 --> 00:00:15,620 a timed attempt to complete either a focus time, or a break time. 5 00:00:17,010 --> 00:00:21,050 The attempt, whether it is a focus or a break, is pretty much the same thing, 6 00:00:21,050 --> 00:00:22,920 as far as functionality is concerned. 7 00:00:22,920 --> 00:00:24,440 It just runs for a different length of time. 8 00:00:26,130 --> 00:00:30,330 There are a couple of ways of solving this issue where we have very similar models. 9 00:00:30,330 --> 00:00:34,230 Now, one is to use inheritance and override certain properties and 10 00:00:34,230 --> 00:00:38,150 methods, but that seems like a little overkill for this problem, though. 11 00:00:38,150 --> 00:00:41,250 As well as I wanna show you a new type that you haven't seen yet. 12 00:00:43,180 --> 00:00:47,717 Enum types or Enoom types are one of those pronunciation arguments that 13 00:00:47,717 --> 00:00:50,226 developers get into from time to time. 14 00:00:50,226 --> 00:00:53,559 It's the age-old [SOUND] is it Super, Mar-ee-oh, Brothers or 15 00:00:53,559 --> 00:00:56,420 is it [SOUND] Super, Mair-ee-oh, Brothers dilemma. 16 00:00:56,420 --> 00:01:02,210 Now enum is short for enumerated, and it's a grouping of known constants. 17 00:01:02,210 --> 00:01:05,650 The most common way I've seen this explained is using the days of the week. 18 00:01:05,650 --> 00:01:07,880 We know there is seven and that they're pretty constant. 19 00:01:07,880 --> 00:01:08,440 Right? 20 00:01:08,440 --> 00:01:11,040 So we have an enum of day and it's got all the days of the week there. 21 00:01:12,140 --> 00:01:16,620 As you can imagine, you can store this as an integer and use zero to six. 22 00:01:16,620 --> 00:01:19,460 But this makes for much clearer code. 23 00:01:19,460 --> 00:01:22,550 You can reference it and use enum as a type. 24 00:01:22,550 --> 00:01:24,060 So you can do something like this. 25 00:01:24,060 --> 00:01:27,080 Like here's a function that sings That One Song and you pass it a day. 26 00:01:27,080 --> 00:01:31,270 And you could say if day = Friday, then print out It's Friday, 27 00:01:31,270 --> 00:01:33,310 Friday, gotta get down, it's Friday. 28 00:01:33,310 --> 00:01:36,300 You know that song that you never wanted to hear again? 29 00:01:36,300 --> 00:01:37,550 Sorry. 30 00:01:37,550 --> 00:01:42,650 So not only does it help with clarity, but it also introduces some additional power. 31 00:01:42,650 --> 00:01:46,190 When you know all types expected, you can loop through them. 32 00:01:46,190 --> 00:01:48,990 The order matters, too, so Saturday is greater that Friday. 33 00:01:50,280 --> 00:01:55,250 Java adds some additional power to enums that other programming languages don't, so 34 00:01:55,250 --> 00:01:57,880 I want you to understand some of these features. 35 00:01:57,880 --> 00:01:58,670 Let's go build our model. 36 00:02:00,550 --> 00:02:04,060 Okay, so let's start on our attempt model first. 37 00:02:04,060 --> 00:02:07,810 So let's make a new package here under com.teamtreehouse.pomidoro called 38 00:02:07,810 --> 00:02:10,760 model and then let's add a new class and call it attempt. 39 00:02:10,760 --> 00:02:13,510 Now I could right click here and choose new package. 40 00:02:14,600 --> 00:02:15,610 But I want to show you a different way. 41 00:02:15,610 --> 00:02:16,830 Let's say new JavaClass, and 42 00:02:16,830 --> 00:02:20,190 I'm just going to name the package, even though it doesn't exist, model.attempt. 43 00:02:20,190 --> 00:02:23,800 And you'll see what it does is it goes ahead and it builds that model package 44 00:02:23,800 --> 00:02:26,790 from where I started at, and then it creates a new attempt. 45 00:02:26,790 --> 00:02:27,350 That's pretty cool. 46 00:02:28,660 --> 00:02:33,220 So we know that we want to track the message that they added about, 47 00:02:33,220 --> 00:02:35,030 what they were doing down in the text area. 48 00:02:35,030 --> 00:02:38,455 So we have private String_mMessage. 49 00:02:39,860 --> 00:02:44,816 And we also know that we want to track how many remaining 50 00:02:44,816 --> 00:02:49,666 seconds they have on this specific attempt right, so 51 00:02:49,666 --> 00:02:53,892 let's do private int_m remaining seconds. 52 00:02:55,370 --> 00:02:56,430 Great. Okay. So now, 53 00:02:56,430 --> 00:02:59,730 this is where we wanna know what type of attempt is happening. 54 00:02:59,730 --> 00:03:01,210 But what do we call that? 55 00:03:01,210 --> 00:03:03,460 I mean, we don't wanna call it type, right, because that's confusing. 56 00:03:03,460 --> 00:03:05,280 We have different types already in Java. 57 00:03:06,350 --> 00:03:07,820 And that's a naming conflict. 58 00:03:07,820 --> 00:03:11,160 And then we could call it style, like, what style of attempt is this? 59 00:03:11,160 --> 00:03:17,360 But, no, that's also a naming conflict from like CSS styles. 60 00:03:17,360 --> 00:03:19,050 What if we called it kind? 61 00:03:19,050 --> 00:03:20,780 How about an AttemptKind? 62 00:03:20,780 --> 00:03:26,340 So let's say private, we'll just name it AttemptKind, and we'll call it nKind. 63 00:03:30,470 --> 00:03:32,845 All right, so it doesn't know what it is, obviously, 64 00:03:32,845 --> 00:03:34,430 because we just made that word up. 65 00:03:34,430 --> 00:03:37,170 And if we come over here and we say, Create enum AttemptKind. 66 00:03:38,240 --> 00:03:40,790 And it's gonna ask if it should go in the same package, and indeed it should. 67 00:03:42,250 --> 00:03:42,750 Okay. 68 00:03:44,070 --> 00:03:47,440 So let's choose to add FOCUS and BREAK. 69 00:03:47,440 --> 00:03:48,560 And we'll put them in all caps, 70 00:03:48,560 --> 00:03:50,780 because that lets people know that they're constants. 71 00:03:50,780 --> 00:03:53,570 That's a good standard, it's been around for a while. 72 00:03:53,570 --> 00:03:56,800 Okay, so now we can refer to the constants of FOCUS and BREAK, but 73 00:03:56,800 --> 00:03:59,150 they also kind of have constants themselves, right. 74 00:03:59,150 --> 00:04:01,850 I mean it'd be nice to find the FOCUS time is 25 minutes and 75 00:04:01,850 --> 00:04:02,860 BREAK time is five minutes. 76 00:04:02,860 --> 00:04:05,860 Well, the good news is that you can do that in Java. 77 00:04:05,860 --> 00:04:08,270 So much like a class definition, you can add fields and 78 00:04:08,270 --> 00:04:10,650 even methods to each item in enum. 79 00:04:10,650 --> 00:04:12,330 So let's do that. 80 00:04:12,330 --> 00:04:16,510 So we'll say private int mTotalSeconds. 81 00:04:16,510 --> 00:04:17,050 Right? 82 00:04:17,050 --> 00:04:18,469 Cuz that's what we're looking at, 83 00:04:18,469 --> 00:04:20,565 extra info that we're looking for each one of these. 84 00:04:20,565 --> 00:04:22,910 Cuz that's kind of what makes them different, 85 00:04:22,910 --> 00:04:24,290 on top of being named differently. 86 00:04:24,290 --> 00:04:25,190 Right? 87 00:04:25,190 --> 00:04:28,690 So we'll use code generation to generate a constructor for that, 88 00:04:28,690 --> 00:04:32,300 cause it works just like the same way, and we'll cool. 89 00:04:32,300 --> 00:04:33,620 Perfect. 90 00:04:33,620 --> 00:04:36,020 And we'll also do a getter for it. 91 00:04:36,020 --> 00:04:36,720 Let's do a getter. 92 00:04:36,720 --> 00:04:40,139 You'll see, up here,that it is asking for, 93 00:04:40,139 --> 00:04:44,874 it's saying hey, I don't know what you're talking about, so 94 00:04:44,874 --> 00:04:49,890 what you do is you actually pass it in what the constructor expects. 95 00:04:49,890 --> 00:04:51,190 Right? So it expects TotalSeconds. 96 00:04:51,190 --> 00:04:53,640 So, what is TotalSeconds? 97 00:04:53,640 --> 00:04:55,590 So we have 25 minutes, we know that. 98 00:04:55,590 --> 00:04:58,060 So times 60 right, that's the math for that. 99 00:04:58,060 --> 00:05:00,630 And then BREAK we want five minutes, and 100 00:05:00,630 --> 00:05:03,390 there are 60 seconds in a minute so, five times 60, bam. 101 00:05:05,250 --> 00:05:10,870 Okay and if flip back over to our attempt model here, we can go ahead now and 102 00:05:10,870 --> 00:05:14,550 build ourselves a constructor. 103 00:05:14,550 --> 00:05:20,000 So let's build a constructor and let's just pass in kind and message. 104 00:05:20,000 --> 00:05:22,870 Let's do that. 105 00:05:22,870 --> 00:05:24,090 Just kind and message. 106 00:05:24,090 --> 00:05:28,380 Remaining seconds, what we'll do is we'll set the remaining seconds from the kind. 107 00:05:32,260 --> 00:05:34,070 GetTotalSeconds, sorry. 108 00:05:34,070 --> 00:05:37,460 So the remaining seconds that set on the attempt when it's first created, 109 00:05:37,460 --> 00:05:39,330 will just set it to be the expected time. 110 00:05:39,330 --> 00:05:39,830 Cool, right? 111 00:05:41,220 --> 00:05:43,670 Now let's go ahead and add getters for everything. 112 00:05:45,870 --> 00:05:48,270 So all three of those, 113 00:05:48,270 --> 00:05:51,010 we want to be able to get the kind out the message in the remaining seconds. 114 00:05:51,010 --> 00:05:55,570 And then let's go ahead and let's add a setter, but let's only add a setter for 115 00:05:55,570 --> 00:05:57,430 the message, right? 116 00:05:57,430 --> 00:05:59,632 I don't think we should be changing the kind ever. 117 00:05:59,632 --> 00:06:02,280 And the remaining seconds, we'll change that but 118 00:06:02,280 --> 00:06:03,580 let's change it through a different method. 119 00:06:05,580 --> 00:06:07,390 Awesome I think we're ready to use this model. 120 00:06:08,410 --> 00:06:12,980 I'm glad we got a chance to explore enums or enums, I don't wanna offend anybody. 121 00:06:12,980 --> 00:06:15,630 They're certainly powerful for grouping constants. 122 00:06:15,630 --> 00:06:19,330 We'll use a little more of their super powers as we wire this app together. 123 00:06:19,330 --> 00:06:21,410 Speaking of wiring it together, let's go do that. 124 00:06:21,410 --> 00:06:22,460 Right after this.