1 00:00:00,720 --> 00:00:03,300 We are finally to the point of writing our objects, 2 00:00:03,300 --> 00:00:07,440 arguably most important method ever, dispense. 3 00:00:07,440 --> 00:00:09,960 Like I was saying earlier, if we wanted to go overboard, 4 00:00:09,960 --> 00:00:12,017 we could actually make one of these Pez objects. 5 00:00:12,017 --> 00:00:13,900 Nothing's stopping us, right? 6 00:00:13,900 --> 00:00:15,140 You could totally imagine it. 7 00:00:15,140 --> 00:00:18,590 Our new Pez class would define some state, like flavor. 8 00:00:18,590 --> 00:00:22,709 Maybe it could even store the time that it was inserted into the dispenser. 9 00:00:22,709 --> 00:00:24,736 We could determine if it was stale. 10 00:00:24,736 --> 00:00:27,592 When we call the dispense method we're about to write, 11 00:00:27,592 --> 00:00:30,220 we could actually return one of these Pez objects. 12 00:00:30,220 --> 00:00:33,560 Or that's going a little overboard, let's not do that just yet. 13 00:00:34,670 --> 00:00:36,670 Now, I was being a little silly there. 14 00:00:36,670 --> 00:00:40,300 But this is actually a pretty hard thing that you're going to grapple with. 15 00:00:40,300 --> 00:00:43,510 How do you know when to stop building objects, 16 00:00:43,510 --> 00:00:47,190 because like we talked about before, everything can be modeled as an object. 17 00:00:48,190 --> 00:00:51,900 I struggle with this all the time. 18 00:00:51,900 --> 00:00:55,930 A common acronym that you'll probably start hearing a lot is YAGNI. 19 00:00:55,930 --> 00:00:57,570 You ain't gonna need it. 20 00:00:57,570 --> 00:01:00,070 In fact, I keep a sticky on my laptop and 21 00:01:00,070 --> 00:01:04,540 it helps me to remember not to overengineer before you actually need it. 22 00:01:04,540 --> 00:01:07,290 Now there's another one that's pretty common sense, but boy, 23 00:01:07,290 --> 00:01:09,240 do we forget it the more we start programming. 24 00:01:09,240 --> 00:01:12,170 And that's this, KISS. 25 00:01:12,170 --> 00:01:15,840 Keep it simple, smarty pants or something more rude than that. 26 00:01:16,950 --> 00:01:22,031 So, with that said, let's keep it simple and assume that there is only one flavor. 27 00:01:22,031 --> 00:01:26,557 What we'll do is, as long as there is a Pez to be dispensed, we will return true. 28 00:01:26,557 --> 00:01:29,655 Of course, if it is in fact empty, we'll return false. 29 00:01:29,655 --> 00:01:30,750 Sound good? 30 00:01:30,750 --> 00:01:31,310 Let's do this. 31 00:01:32,710 --> 00:01:34,600 Okay, so I have JShell open again and 32 00:01:34,600 --> 00:01:38,390 I wanna show off a couple of cool tricks that we can do with integers. 33 00:01:38,390 --> 00:01:41,169 Okay, so first off we know that we can create one, right? 34 00:01:41,169 --> 00:01:47,157 So we're gonna say, int example = 1. 35 00:01:47,157 --> 00:01:52,670 And if we wanted to just add one to it, we could use the variable itself. 36 00:01:52,670 --> 00:01:57,038 So we can say something like, example = example + 1, right? 37 00:01:57,038 --> 00:01:59,870 So that's gonna return 2. 38 00:01:59,870 --> 00:02:03,620 So, we've set, now example is 2, right? 39 00:02:03,620 --> 00:02:07,587 So, since that's so common, there is a special shorthand for it, and 40 00:02:07,587 --> 00:02:08,760 it looks like this. 41 00:02:08,760 --> 00:02:14,631 So you say example += 1 and basically it's this. 42 00:02:14,631 --> 00:02:17,042 It does exactly this, it's just shorthand for it. 43 00:02:18,878 --> 00:02:25,850 Cool, so that, return 3 and also updated example to be 3. 44 00:02:25,850 --> 00:02:31,550 Okay, finally, because we end up adding one a lot, 45 00:02:31,550 --> 00:02:35,100 there's an even shorter form and it's called incrementing. 46 00:02:35,100 --> 00:02:37,050 And it looks like this. 47 00:02:37,050 --> 00:02:39,533 So we say example++. 48 00:02:39,533 --> 00:02:41,710 Now this one's a little bit dangerous. 49 00:02:41,710 --> 00:02:45,450 It actually returns first and then changes the value. 50 00:02:45,450 --> 00:02:47,870 Because this is what is known as post incrementing. 51 00:02:47,870 --> 00:02:52,040 So if we do this, it's gonna go ahead and it's stored 3 in 5. 52 00:02:52,040 --> 00:02:53,850 Now remember, example was 3. 53 00:02:53,850 --> 00:02:58,120 But if I look at example now, it's 4. 54 00:02:58,120 --> 00:03:00,490 So it actually did example plus one. 55 00:03:01,500 --> 00:03:05,551 But it returned, the expression itself returned what the value used to be, 56 00:03:05,551 --> 00:03:08,610 much like that swap head that we were looking at earlier. 57 00:03:08,610 --> 00:03:12,000 But you can actually do the other side too, you can do pre increment. 58 00:03:12,000 --> 00:03:16,246 So you can say example, ++example. 59 00:03:16,246 --> 00:03:19,873 And that returns 5 and example is 5. 60 00:03:22,744 --> 00:03:24,872 And that does the reverse, right? 61 00:03:24,872 --> 00:03:27,380 It increments and then it returns the value. 62 00:03:27,380 --> 00:03:30,830 Now the dangerous thing about this is that you can use these in a very 63 00:03:30,830 --> 00:03:32,730 complex statement. 64 00:03:32,730 --> 00:03:36,270 Now if you can't do the incrementing on a line by itself, 65 00:03:36,270 --> 00:03:39,420 I highly suggest you don't use these at all. 66 00:03:39,420 --> 00:03:43,430 In other words, I highly recommend that you don't use the value returned from 67 00:03:43,430 --> 00:03:45,680 either the pre or post increment. 68 00:03:45,680 --> 00:03:50,180 As you can probably guess by now, the same works with subtracting. 69 00:03:50,180 --> 00:03:54,308 So I'm gonna clear here and we have, example's 5 so 70 00:03:54,308 --> 00:03:57,730 we say example = example- 1. 71 00:03:57,730 --> 00:04:02,576 We'll see that that example is 4 now, and 72 00:04:02,576 --> 00:04:08,644 you can also do example -=1 and example's now 3. 73 00:04:08,644 --> 00:04:12,969 And then, finally, as the other one is called incrementing, with the pluses, 74 00:04:12,969 --> 00:04:14,630 this is called decrementing. 75 00:04:14,630 --> 00:04:19,511 So we can say example-- and we did it on the other side, so 76 00:04:19,511 --> 00:04:23,210 it's post but now example's actually 2. 77 00:04:24,640 --> 00:04:27,910 Well, decrementing sounds like exactly what we need to do 78 00:04:27,910 --> 00:04:29,350 when we want to dispense. 79 00:04:29,350 --> 00:04:32,739 Again, so the dispense method that we're gonna write should work like this. 80 00:04:32,739 --> 00:04:36,534 Every time that we dispense, we should decrement the Pez count, 81 00:04:36,534 --> 00:04:38,820 as long as there's some left. 82 00:04:38,820 --> 00:04:42,994 And we should let the caller know that we dispensed something, cool. 83 00:04:44,713 --> 00:04:48,770 So, in PezDispenser, let's move this down here. 84 00:04:50,190 --> 00:04:52,440 Let's go ahead and we'll make a new method. 85 00:04:54,620 --> 00:04:57,530 It's going to return a true or false, right? 86 00:04:57,530 --> 00:05:01,330 And it will have it be called dispense, and it won't take anything. 87 00:05:01,330 --> 00:05:06,443 So, we'll store the default value here, we'll say boolean 88 00:05:06,443 --> 00:05:11,375 wasDispensed, and we'll set the default value to false. 89 00:05:11,375 --> 00:05:16,587 Okay, so we know that we can say if it's not empty, right? 90 00:05:19,237 --> 00:05:20,860 So, let's look at this code block. 91 00:05:20,860 --> 00:05:23,900 So if it's not empty we want to decrement the Pez count. 92 00:05:23,900 --> 00:05:27,449 So we do that as such, we say pezCount--, so 93 00:05:27,449 --> 00:05:30,636 that whatever is in there moves down one. 94 00:05:30,636 --> 00:05:35,008 And we're gonna say wasDispensed, we're gonna set that equal to true, 95 00:05:35,008 --> 00:05:36,570 because it was actually. 96 00:05:36,570 --> 00:05:40,070 And then finally, we're going to return what was dispensed. 97 00:05:41,610 --> 00:05:45,578 So it will start out as false, if there's anything in there it will go through. 98 00:05:45,578 --> 00:05:48,166 If it's not it will never come in here and it will just say false and 99 00:05:48,166 --> 00:05:50,030 the number won't go down. 100 00:05:50,030 --> 00:05:51,170 Makes sense? 101 00:05:51,170 --> 00:05:55,620 Okay, so let's flip over to our Example.java. 102 00:05:55,620 --> 00:06:00,850 And I'm going to drop out of the JShell here if we do Ctrl+B. 103 00:06:00,850 --> 00:06:03,938 And I will say clear, all right. 104 00:06:03,938 --> 00:06:08,560 So in Example.java here, let's just go ahead and 105 00:06:08,560 --> 00:06:11,570 empty the whole thing in one sitting. 106 00:06:11,570 --> 00:06:14,640 We're probably all guilty of, I know I am. 107 00:06:14,640 --> 00:06:16,280 So, this should be fairly easy. 108 00:06:16,280 --> 00:06:20,500 All we need to do is just call the dispense method 12 times, right? 109 00:06:20,500 --> 00:06:24,260 Well, that worked, but this sounds like a job for a loop. 110 00:06:24,260 --> 00:06:28,401 We want to keep dispensing as long as we get something back. 111 00:06:28,401 --> 00:06:31,600 So, while there's something dispensed, eat it. 112 00:06:31,600 --> 00:06:32,540 Sound good? 113 00:06:32,540 --> 00:06:34,509 Sounds like a while loop right? 114 00:06:34,509 --> 00:06:40,470 So, just a quick reminder, so we'll say while dispenser.dispense(). 115 00:06:42,100 --> 00:06:45,428 Well that's happening, let's just write out chomp I guess, right? 116 00:06:51,057 --> 00:06:54,396 And then finally, if it is actually empty, 117 00:06:54,396 --> 00:06:58,291 which it should be, cuz we're gonna eat them all. 118 00:07:00,994 --> 00:07:10,750 We'll say system.out.println(Ate all the PEZ). 119 00:07:15,978 --> 00:07:18,620 Something's not lining up, look, I forgot a quote here. 120 00:07:20,570 --> 00:07:22,177 There we go. 121 00:07:22,177 --> 00:07:27,900 All right, so this is gonna return true or false. 122 00:07:27,900 --> 00:07:31,463 And as long as it's returning true, it's gonna keep on chomping. 123 00:07:31,463 --> 00:07:33,854 Sound good, let's take a look. 124 00:07:37,044 --> 00:07:46,664 Clear && javac Example.java && java Example. 125 00:07:46,664 --> 00:07:50,060 Here we go, chomp, chomp, chomp, chomp, chomp. 126 00:07:50,060 --> 00:07:51,410 We ate all the Pez, cool. 127 00:07:51,410 --> 00:07:54,560 So we should have 12 there, chomp, chomp, chomp, chomp, chomp, chomp, chomp, 128 00:07:54,560 --> 00:07:55,190 chomp, chomp. 129 00:07:55,190 --> 00:07:57,662 Ate all the Pez, awesome. 130 00:07:57,662 --> 00:08:00,820 Good job, we can now load and dispense our Pez. 131 00:08:00,820 --> 00:08:03,988 We are getting close to completing this model. 132 00:08:03,988 --> 00:08:07,881 Now that we're removing Pez, we should probably get more specific around 133 00:08:07,881 --> 00:08:10,420 the handling of what it means to fill. 134 00:08:10,420 --> 00:08:13,320 In the real world, if we try to load a dispenser that 135 00:08:13,320 --> 00:08:17,340 already has some Pez in there, we'd add Pez until it was is in fact full. 136 00:08:17,340 --> 00:08:20,440 We wouldn't just add the maximum amount available, because that could be 137 00:08:20,440 --> 00:08:24,260 potentially overfilling and wasting the remainder, we don't wanna waste. 138 00:08:26,610 --> 00:08:28,220 Let's practice incrementing and decrementing.