1 00:00:04,730 --> 00:00:06,710 Okay so we're going to finish off the 2 00:00:06,710 --> 00:00:09,110 Kotlin Tutorial section with a challenge. 3 00:00:09,110 --> 00:00:11,299 We're going to alter the way the 4 00:00:11,299 --> 00:00:14,210 overridden dropLoot function works so 5 00:00:14,210 --> 00:00:15,889 switch over now to the Player class. 6 00:00:15,889 --> 00:00:19,310 Let's go ahead and do that. And we 7 00:00:19,310 --> 00:00:21,050 created, as you can see, an overridden 8 00:00:21,050 --> 00:00:23,720 dropLoot method on the screen there, 9 00:00:23,720 --> 00:00:26,180 line 41, and that lets us specify the 10 00:00:26,180 --> 00:00:28,640 name of the loot rather than providing a 11 00:00:28,640 --> 00:00:30,739 reference to the piece of loot. 12 00:00:30,739 --> 00:00:32,719 Now our overridden method will remove 13 00:00:32,719 --> 00:00:35,930 all items with a matching name, which may 14 00:00:35,930 --> 00:00:38,090 not be what we want. So let's see that 15 00:00:38,090 --> 00:00:39,470 working first, to understand why we 16 00:00:39,470 --> 00:00:41,960 might want different behavior. So go back 17 00:00:41,960 --> 00:00:44,390 to Main. I'm going to delete all this 18 00:00:44,390 --> 00:00:46,130 Dracula code, then we're going to 19 00:00:46,130 --> 00:00:48,440 uncomment the conan code. I'm gonna come 20 00:00:48,440 --> 00:00:51,050 up to the top here, delete all 21 00:00:51,050 --> 00:00:54,800 that. We're just going to uncomment 22 00:00:54,800 --> 00:01:01,489 the conan code. Alright we'll just make sure 23 00:01:01,489 --> 00:01:03,290 that the program still works, we'll run 24 00:01:03,290 --> 00:01:09,020 it. Okay so that's working nicely. So you 25 00:01:09,020 --> 00:01:10,549 can see conan's got nine pieces of 26 00:01:10,549 --> 00:01:14,240 loot, total score of 240, the total value 27 00:01:14,240 --> 00:01:17,659 of 240. Now we need some duplicates so 28 00:01:17,659 --> 00:01:19,460 I'm going to use Android Studio to 29 00:01:19,460 --> 00:01:21,859 duplicate the gold and silver rings. the 30 00:01:21,859 --> 00:01:23,420 shortcut's command D on my Mac and 31 00:01:23,420 --> 00:01:26,810 control D on a PC. so if we go to the two 32 00:01:26,810 --> 00:01:31,039 rings. Be on that line and Ctrl D and 33 00:01:31,039 --> 00:01:34,549 silver w'll do the same. Actually, let's do 34 00:01:34,549 --> 00:01:36,319 a couple let's give Conan three rings 35 00:01:36,319 --> 00:01:37,779 three gold rings 36 00:01:37,779 --> 00:01:40,369 so I've duplicates that line twice, and two 37 00:01:40,369 --> 00:01:41,990 silver rings, as you can see. If you 38 00:01:41,990 --> 00:01:44,479 run the program we should get a value 39 00:01:44,479 --> 00:01:48,020 hopefully of 270 and you can see we've 40 00:01:48,020 --> 00:01:49,490 got a value of 270 and we can see the 41 00:01:49,490 --> 00:01:51,560 multiple items now showing in the 42 00:01:51,560 --> 00:01:54,319 inventory as well. Alright so we've got a 43 00:01:54,319 --> 00:01:58,099 dropLoot function that we can use to 44 00:01:58,099 --> 00:02:00,109 drop items of loop using the name and 45 00:02:00,109 --> 00:02:01,459 we've already tested it previously but 46 00:02:01,459 --> 00:02:03,590 what we didn't do was test it when 47 00:02:03,590 --> 00:02:04,969 there's more than one item with the same 48 00:02:04,969 --> 00:02:07,249 name. so let's actually go back to Main 49 00:02:07,249 --> 00:02:09,169 and actually try testing it in that scenario. 50 00:02:09,169 --> 00:02:13,510 So we'll do a conan.dropLoot and 51 00:02:13,510 --> 00:02:16,010 parentheses double quote and a gold ring 52 00:02:16,010 --> 00:02:17,840 making sure you're typing it exactly as 53 00:02:17,840 --> 00:02:18,720 you see on screen 54 00:02:18,720 --> 00:02:21,670 on line 20 and then we'll do 55 00:02:21,670 --> 00:02:23,200 the showInventory after that as well. 56 00:02:23,200 --> 00:02:26,080 conan.showInventory. Let's run 57 00:02:26,080 --> 00:02:31,000 that again. So you can see what's 58 00:02:31,000 --> 00:02:32,410 happened in that scenario, the total 59 00:02:32,410 --> 00:02:34,810 value's dropped to 234, and we've actually 60 00:02:34,810 --> 00:02:38,020 got rid of all of Conan's gold rings. Now 61 00:02:38,020 --> 00:02:39,670 that may not be what you expected, it 62 00:02:39,670 --> 00:02:41,920 certainly wasn't what conan expected. We 63 00:02:41,920 --> 00:02:43,270 don't want him knocking at our door 64 00:02:43,270 --> 00:02:45,640 demanding his gold rings back. Now 65 00:02:45,640 --> 00:02:47,290 unless you fancy a brawl with an irate 66 00:02:47,290 --> 00:02:50,200 barbarian, we better fix that code or 67 00:02:50,200 --> 00:02:52,510 rather you better fix that code. Now 68 00:02:52,510 --> 00:02:53,860 before I set the challenge, though, let's 69 00:02:53,860 --> 00:02:55,870 review the second dropLoot function in 70 00:02:55,870 --> 00:03:00,430 the Player class. So in the case of 71 00:03:00,430 --> 00:03:03,010 droppLoot with an item, passing an 72 00:03:03,010 --> 00:03:05,080 item, that's using the remove item in the 73 00:03:05,080 --> 00:03:07,330 case of this option here the second one 74 00:03:07,330 --> 00:03:09,160 the one that's got a string argument 75 00:03:09,160 --> 00:03:11,860 we're using the removeIf function. so 76 00:03:11,860 --> 00:03:13,120 basically the scenario with this 77 00:03:13,120 --> 00:03:15,070 particular function you can see that 78 00:03:15,070 --> 00:03:17,530 it's doing a return or removeIf, it's 79 00:03:17,530 --> 00:03:19,690 using the removeIf function, which 80 00:03:19,690 --> 00:03:21,910 removes all items if the condition 81 00:03:21,910 --> 00:03:24,250 returns true. So if an item has the name 82 00:03:24,250 --> 00:03:25,840 we specified, it'll be removed from the 83 00:03:25,840 --> 00:03:27,100 list. And we saw that in the case of 84 00:03:27,100 --> 00:03:29,920 Conan's gold ring, all three gold rings 85 00:03:29,920 --> 00:03:31,930 were actually removed. So what we want to 86 00:03:31,930 --> 00:03:34,750 do here is break out of the loop as soon 87 00:03:34,750 --> 00:03:37,570 as an item has been removed. Now a return 88 00:03:37,570 --> 00:03:39,430 statement will break out of a for loop, 89 00:03:39,430 --> 00:03:41,950 if it's inside a function, so you don't 90 00:03:41,950 --> 00:03:44,470 necessarily have to use break. So what 91 00:03:44,470 --> 00:03:46,299 this means is we can't use removeIf 92 00:03:46,299 --> 00:03:48,760 like we're using on line 43, we have to 93 00:03:48,760 --> 00:03:51,430 use a basic for loop instead. Now I 94 00:03:51,430 --> 00:03:53,049 should mention that you can't create a 95 00:03:53,049 --> 00:03:55,510 third dropLoot function. We're going to 96 00:03:55,510 --> 00:03:57,070 have to use exactly the same parameters, 97 00:03:57,070 --> 00:03:59,620 a string here, so we can't override 98 00:03:59,620 --> 00:04:01,030 dropLoot again because there's already a 99 00:04:01,030 --> 00:04:03,730 function declaration with that string 100 00:04:03,730 --> 00:04:06,010 argument. And when you overwrite a 101 00:04:06,010 --> 00:04:07,600 function you have to give the new 102 00:04:07,600 --> 00:04:09,100 version of the function different 103 00:04:09,100 --> 00:04:11,079 parameters. Okay so one with the 104 00:04:11,079 --> 00:04:14,170 challenge 105 00:04:57,280 --> 00:04:58,260 All right so that's 106 00:04:59,260 --> 00:05:00,670 the challenge. Pause the video now and 107 00:05:00,670 --> 00:05:02,080 see how you go with that challenge, and 108 00:05:02,080 --> 00:05:03,970 I'll see you when you get back with the 109 00:05:03,970 --> 00:05:04,290 solution. 110 00:05:07,290 --> 00:05:09,610 Alright, so as usual your code may look different to the code that I'm 111 00:05:09,610 --> 00:05:11,950 about to present the important thing 112 00:05:11,950 --> 00:05:13,450 though is that your code works and you 113 00:05:13,450 --> 00:05:15,610 get the correct results I'm going to 114 00:05:15,610 --> 00:05:16,960 type this code in and then we'll go 115 00:05:16,960 --> 00:05:19,450 through it then we're obviously updating 116 00:05:19,450 --> 00:05:22,390 this drop loop function on line 41 so 117 00:05:22,390 --> 00:05:25,680 what I'm going to do is delete this code 118 00:05:25,680 --> 00:05:28,330 start from scratch so I'm going to do a 119 00:05:28,330 --> 00:05:35,140 four parenthesis item in inventory open 120 00:05:35,140 --> 00:05:36,910 the code block then we're going to put 121 00:05:36,910 --> 00:05:41,700 if parentheses item dot name is equal to 122 00:05:41,700 --> 00:05:44,920 name name being the argument passed to 123 00:05:44,920 --> 00:05:47,830 this function and open a code block and 124 00:05:47,830 --> 00:05:51,900 do inventory dot remove not remove if 125 00:05:51,900 --> 00:05:56,130 item then we're going to return true 126 00:05:56,130 --> 00:05:59,050 otherwise if it gets down to here we're 127 00:05:59,050 --> 00:06:02,050 going to do a return false so that's 128 00:06:02,050 --> 00:06:04,510 actually it so here we've used a simple 129 00:06:04,510 --> 00:06:06,910 for loop to iterate over the items in 130 00:06:06,910 --> 00:06:08,919 the inventory and if we get to a 131 00:06:08,919 --> 00:06:10,990 matching item then it's removed from the 132 00:06:10,990 --> 00:06:13,780 list and the function returns true now 133 00:06:13,780 --> 00:06:15,850 when a function returns no further code 134 00:06:15,850 --> 00:06:18,310 in the functions executed so that's why 135 00:06:18,310 --> 00:06:19,770 there's no need for a break here 136 00:06:19,770 --> 00:06:22,169 execution of the code terminates and 137 00:06:22,169 --> 00:06:24,430 control goes back to the calling code 138 00:06:24,430 --> 00:06:26,110 now if we get all the way through the 139 00:06:26,110 --> 00:06:27,760 loop without finding an item to remove 140 00:06:27,760 --> 00:06:30,730 and here on line 48 that code actually 141 00:06:30,730 --> 00:06:33,340 gets called and you can see here I put 142 00:06:33,340 --> 00:06:34,630 some semicolons in because I'm used to 143 00:06:34,630 --> 00:06:36,490 typing in Java and it's actually come up 144 00:06:36,490 --> 00:06:38,020 and flagged and said that that's 145 00:06:38,020 --> 00:06:41,860 actually redundant seem redundant 146 00:06:41,860 --> 00:06:44,740 semicolon so I'll just remove those as well, 147 00:06:44,740 --> 00:06:47,810 so this is one way to write the code and 148 00:06:47,810 --> 00:06:50,150 again if we get all the way through the 149 00:06:50,150 --> 00:06:51,410 loop without finding can ask them to 150 00:06:51,410 --> 00:06:53,330 remove the function returns false and 151 00:06:53,330 --> 00:06:55,789 you can see it doing them online 48 now 152 00:06:55,789 --> 00:06:57,530 that's one way to write the code but you 153 00:06:57,530 --> 00:06:58,610 may have done something slightly 154 00:06:58,610 --> 00:07:00,289 different so I want to show you an 155 00:07:00,289 --> 00:07:01,580 alternate method as well so I'm going to 156 00:07:01,580 --> 00:07:03,139 comment out this version of the function 157 00:07:03,139 --> 00:07:09,139 so we'll go ahead and run a second 158 00:07:09,139 --> 00:07:11,690 version so we'll start with fun drop 159 00:07:11,690 --> 00:07:17,569 loot parentheses name colon string colon 160 00:07:17,569 --> 00:07:21,889 boolean and lift and write curly braces 161 00:07:21,889 --> 00:07:25,280 and then we can type var result equals 162 00:07:25,280 --> 00:07:32,080 false for parentheses item in inventory 163 00:07:32,080 --> 00:07:35,750 open a code block if parentheses item 164 00:07:35,750 --> 00:07:40,310 name is equal to name moving into 165 00:07:40,310 --> 00:07:46,930 inventory don't remove item results it was true 166 00:07:46,930 --> 00:07:50,000 break they're doing semicolons again 167 00:07:50,000 --> 00:07:52,750 with a semicolons get out of that habit 168 00:07:52,750 --> 00:07:56,000 otherwise and down the bottom here with 169 00:07:56,000 --> 00:07:59,300 what return result so this time we're 170 00:07:59,300 --> 00:08:00,830 storing the result that we'll be 171 00:08:00,830 --> 00:08:02,979 returning in a variable called result 172 00:08:02,979 --> 00:08:05,330 then we start out setting the value of 173 00:08:05,330 --> 00:08:09,050 that variable to false on line 52 and if 174 00:08:09,050 --> 00:08:10,759 we don't find anything to remove that's 175 00:08:10,759 --> 00:08:12,320 what it ultimately it'll contain and 176 00:08:12,320 --> 00:08:13,969 that's what will return down here on 177 00:08:13,969 --> 00:08:16,460 line 60 now inside the loop we're 178 00:08:16,460 --> 00:08:18,110 checking the name of each item as before 179 00:08:18,110 --> 00:08:20,810 but for matches found we remove the item 180 00:08:20,810 --> 00:08:22,240 and that's the same as the previous 181 00:08:22,240 --> 00:08:25,250 version of this function does but then 182 00:08:25,250 --> 00:08:27,770 we also set result to true on the next 183 00:08:27,770 --> 00:08:29,710 line and then we break out of the loop 184 00:08:29,710 --> 00:08:31,610 because there's no point going around 185 00:08:31,610 --> 00:08:33,409 again and then the return statement 186 00:08:33,409 --> 00:08:35,750 online 60 that's going to be executed 187 00:08:35,750 --> 00:08:37,130 when the loop terminates or when we 188 00:08:37,130 --> 00:08:38,929 break out of it so either way we're 189 00:08:38,929 --> 00:08:41,240 going to return the value of result all 190 00:08:41,240 --> 00:08:42,860 right so the tester let's go back to 191 00:08:42,860 --> 00:08:46,510 main and try dropping the silver ring 192 00:08:46,510 --> 00:08:50,630 Conan dot drop loot double quotes this 193 00:08:50,630 --> 00:08:53,200 time to a silver ring 194 00:08:53,200 --> 00:08:57,530 and Conan show inventory. Alright so 195 00:08:57,530 --> 00:09:02,870 let's run that and you can see up here 196 00:09:02,870 --> 00:09:06,290 if we scroll up the score was 258 when 197 00:09:06,290 --> 00:09:07,790 we dropped one of the other gold rings 198 00:09:07,790 --> 00:09:10,640 then the second time the show injury was 199 00:09:10,640 --> 00:09:11,990 caught after he dropped the silver ring 200 00:09:11,990 --> 00:09:15,080 the valley drops down to 252 so we've 201 00:09:15,080 --> 00:09:16,820 got one silver ring left finally and two 202 00:09:16,820 --> 00:09:19,040 gold rings which is correct so that's 203 00:09:19,040 --> 00:09:22,490 working fine all right so how about the 204 00:09:22,490 --> 00:09:24,620 scenario that we're trying to drop 205 00:09:24,620 --> 00:09:26,090 something that he doesn't have on him 206 00:09:26,090 --> 00:09:27,890 there's a couple of ways we could write 207 00:09:27,890 --> 00:09:29,690 that we can't just call the drop loot 208 00:09:29,690 --> 00:09:31,550 function because we need to verify that 209 00:09:31,550 --> 00:09:35,000 returns false so there's three ways you 210 00:09:35,000 --> 00:09:36,530 may have done it let's actually have a 211 00:09:36,530 --> 00:09:39,170 look at that so I will start with the 212 00:09:39,170 --> 00:09:44,000 first one they're all dropped is equal 213 00:09:44,000 --> 00:09:49,460 to conan dot drop loot and we'll type in 214 00:09:49,460 --> 00:09:50,750 something that we know isn't present so 215 00:09:50,750 --> 00:09:55,310 something not present then we can just 216 00:09:55,310 --> 00:09:58,490 print out the value have dropped which 217 00:09:58,490 --> 00:09:59,690 will be true or false depending on 218 00:09:59,690 --> 00:10:01,430 whether it may need to drop it you may 219 00:10:01,430 --> 00:10:02,990 also have done something like this print 220 00:10:02,990 --> 00:10:09,440 'ln parentheses in conan dot drop loot 221 00:10:09,440 --> 00:10:13,700 double quotes something else which will 222 00:10:13,700 --> 00:10:16,460 also return or print out the value of 223 00:10:16,460 --> 00:10:18,980 what was returned from drop loot which 224 00:10:18,980 --> 00:10:21,080 would be false again because it doesn't exist 225 00:10:21,080 --> 00:10:23,050 the third way might have been if 226 00:10:23,050 --> 00:10:26,680 parentheses conan dot drop loot 227 00:10:26,680 --> 00:10:28,940 parentheses in double quotes a bit of 228 00:10:28,940 --> 00:10:30,950 junk against something that doesn't 229 00:10:30,950 --> 00:10:34,310 exist in his inventory at present over 230 00:10:34,310 --> 00:10:37,300 the code block then we'll print line 231 00:10:37,300 --> 00:10:41,810 junk dropped else 232 00:10:41,810 --> 00:10:49,520 print 'ln you don't have any junk so the 233 00:10:49,520 --> 00:10:52,400 first way here on line 26 the result of 234 00:10:52,400 --> 00:10:54,170 conan drop Lutz assigned to the 235 00:10:54,170 --> 00:10:56,029 variable dropped we print that on the 236 00:10:56,029 --> 00:10:57,620 next line to check that we did get false 237 00:10:57,620 --> 00:11:00,230 and that can be combined into a single 238 00:11:00,230 --> 00:11:02,210 line and you can see an example of that 239 00:11:02,210 --> 00:11:04,940 on line 29 which should also print false 240 00:11:04,940 --> 00:11:06,020 and we'll run this shortly 241 00:11:06,020 --> 00:11:07,730 and finally we could test the return 242 00:11:07,730 --> 00:11:09,620 value as a condition in an if statement 243 00:11:09,620 --> 00:11:11,450 as you can see me doing on line 31 244 00:11:11,450 --> 00:11:13,040 you print sent the message you don't 245 00:11:13,040 --> 00:11:14,900 have any junk well it should when we run 246 00:11:14,900 --> 00:11:16,400 it so that's just to confirm that that 247 00:11:16,400 --> 00:11:17,240 actually works 248 00:11:17,240 --> 00:11:20,350 we should get three folders showing up 249 00:11:20,350 --> 00:11:22,550 well we got two folders I should say in 250 00:11:22,550 --> 00:11:24,350 the third time we got you don't have any 251 00:11:24,350 --> 00:11:26,410 junk so that's actually working fine 252 00:11:26,410 --> 00:11:28,700 alright so that's the end of this Kotlin 253 00:11:28,700 --> 00:11:30,860 tutorial in this course we hope you've 254 00:11:30,860 --> 00:11:32,990 enjoyed it and got a lot out of it now 255 00:11:32,990 --> 00:11:34,700 if you call the purpose of these videos 256 00:11:34,700 --> 00:11:36,380 was to give you a quick understanding of 257 00:11:36,380 --> 00:11:39,290 the basics of Kotlin there's a lot more 258 00:11:39,290 --> 00:11:40,850 to the language and you'll see different 259 00:11:40,850 --> 00:11:42,410 ways of writing code as you work through 260 00:11:42,410 --> 00:11:44,570 the rest of this course now I haven't 261 00:11:44,570 --> 00:11:45,800 been able to cover everything about 262 00:11:45,800 --> 00:11:47,570 Kotlin in this tutorial section because 263 00:11:47,570 --> 00:11:49,160 that would need an entire course in 264 00:11:49,160 --> 00:11:51,740 itself but hopefully you now understand 265 00:11:51,740 --> 00:11:52,940 the basics and should be able to 266 00:11:52,940 --> 00:11:54,920 understand the code we use in the 267 00:11:54,920 --> 00:11:57,200 Android apps so if you do think that you 268 00:11:57,200 --> 00:11:59,810 could do with some more tutorials for 269 00:11:59,810 --> 00:12:02,210 Kotlin then Sarah Etheridge has got a 270 00:12:02,210 --> 00:12:03,980 fantastic Kotlin course that you should 271 00:12:03,980 --> 00:12:05,900 check out and there's a link to that in 272 00:12:05,900 --> 00:12:08,000 the last lecture in this course so check 273 00:12:08,000 --> 00:12:08,990 that out if you want to know more about 274 00:12:08,990 --> 00:12:10,310 Kotlin because she goes into great 275 00:12:10,310 --> 00:12:12,830 detail and will help really take you to 276 00:12:12,830 --> 00:12:14,270 the next level with Kotlin development 277 00:12:14,270 --> 00:12:16,040 alright so the rest of this course 278 00:12:16,040 --> 00:12:17,839 though is going to focus on Android and 279 00:12:17,839 --> 00:12:19,790 I'm gonna be explaining the various 280 00:12:19,790 --> 00:12:21,950 classes in the android framework as we 281 00:12:21,950 --> 00:12:24,709 use them and it's worth watching the 282 00:12:24,709 --> 00:12:26,240 videos in this section by the way a few 283 00:12:26,240 --> 00:12:28,220 times if you're new to programming to 284 00:12:28,220 --> 00:12:29,630 make sure you fully understand the 285 00:12:29,630 --> 00:12:31,459 concepts we've covered once you do 286 00:12:31,459 --> 00:12:32,660 you'll be really ready for the rest of 287 00:12:32,660 --> 00:12:35,300 this course okay with that said I'll see 288 00:12:35,300 --> 00:12:38,080 you in the next section.