Okay so we're going to finish off the
Kotlin Tutorial section with a challenge.
We're going to alter the way the
overridden dropLoot function works so
switch over now to the Player class.
Let's go ahead and do that. And we
created, as you can see, an overridden
dropLoot method on the screen there,
line 41, and that lets us specify the
name of the loot rather than providing a
reference to the piece of loot.
Now our overridden method will remove
all items with a matching name, which may
not be what we want. So let's see that
working first, to understand why we
might want different behavior. So go back
to Main. I'm going to delete all this
Dracula code, then we're going to
uncomment the conan code. I'm gonna come
up to the top here, delete all
that. We're just going to uncomment
the conan code. Alright we'll just make sure
that the program still works, we'll run
it. Okay so that's working nicely. So you
can see conan's got nine pieces of
loot, total score of 240, the total value
of 240. Now we need some duplicates so
I'm going to use Android Studio to
duplicate the gold and silver rings. the
shortcut's command D on my Mac and
control D on a PC. so if we go to the two
rings. Be on that line and Ctrl D and
silver w'll do the same. Actually, let's do
a couple let's give Conan three rings
three gold rings
so I've duplicates that line twice, and two
silver rings, as you can see. If you
run the program we should get a value
hopefully of 270 and you can see we've
got a value of 270 and we can see the
multiple items now showing in the
inventory as well. Alright so we've got a
dropLoot function that we can use to
drop items of loop using the name and
we've already tested it previously but
what we didn't do was test it when
there's more than one item with the same
name. so let's actually go back to Main
and actually try testing it in that scenario.
So we'll do a conan.dropLoot and
parentheses double quote and a gold ring
making sure you're typing it exactly as
you see on screen
on line 20 and then we'll do
the showInventory after that as well.
conan.showInventory. Let's run
that again. So you can see what's
happened in that scenario, the total
value's dropped to 234, and we've actually
got rid of all of Conan's gold rings. Now
that may not be what you expected, it
certainly wasn't what conan expected. We
don't want him knocking at our door
demanding his gold rings back. Now
unless you fancy a brawl with an irate
barbarian, we better fix that code or
rather you better fix that code. Now
before I set the challenge, though, let's
review the second dropLoot function in
the Player class. So in the case of
droppLoot with an item, passing an
item, that's using the remove item in the
case of this option here the second one
the one that's got a string argument
we're using the removeIf function. so
basically the scenario with this
particular function you can see that
it's doing a return or removeIf, it's
using the removeIf function, which
removes all items if the condition
returns true. So if an item has the name
we specified, it'll be removed from the
list. And we saw that in the case of
Conan's gold ring, all three gold rings
were actually removed. So what we want to
do here is break out of the loop as soon
as an item has been removed. Now a return
statement will break out of a for loop,
if it's inside a function, so you don't
necessarily have to use break. So what
this means is we can't use removeIf
like we're using on line 43, we have to
use a basic for loop instead. Now I
should mention that you can't create a
third dropLoot function. We're going to
have to use exactly the same parameters,
a string here, so we can't override
dropLoot again because there's already a
function declaration with that string
argument. And when you overwrite a
function you have to give the new
version of the function different
parameters. Okay so one with the
challenge
All right so that's
the challenge. Pause the video now and
see how you go with that challenge, and
I'll see you when you get back with the
solution.
Alright, so as usual your code may look different to the code that I'm
about to present the important thing
though is that your code works and you
get the correct results I'm going to
type this code in and then we'll go
through it then we're obviously updating
this drop loop function on line 41 so
what I'm going to do is delete this code
start from scratch so I'm going to do a
four parenthesis item in inventory open
the code block then we're going to put
if parentheses item dot name is equal to
name name being the argument passed to
this function and open a code block and
do inventory dot remove not remove if
item then we're going to return true
otherwise if it gets down to here we're
going to do a return false so that's
actually it so here we've used a simple
for loop to iterate over the items in
the inventory and if we get to a
matching item then it's removed from the
list and the function returns true now
when a function returns no further code
in the functions executed so that's why
there's no need for a break here
execution of the code terminates and
control goes back to the calling code
now if we get all the way through the
loop without finding an item to remove
and here on line 48 that code actually
gets called and you can see here I put
some semicolons in because I'm used to
typing in Java and it's actually come up
and flagged and said that that's
actually redundant seem redundant
semicolon so I'll just remove those as well,
so this is one way to write the code and
again if we get all the way through the
loop without finding can ask them to
remove the function returns false and
you can see it doing them online 48 now
that's one way to write the code but you
may have done something slightly
different so I want to show you an
alternate method as well so I'm going to
comment out this version of the function
so we'll go ahead and run a second
version so we'll start with fun drop
loot parentheses name colon string colon
boolean and lift and write curly braces
and then we can type var result equals
false for parentheses item in inventory
open a code block if parentheses item
name is equal to name moving into
inventory don't remove item results it was true
break they're doing semicolons again
with a semicolons get out of that habit
otherwise and down the bottom here with
what return result so this time we're
storing the result that we'll be
returning in a variable called result
then we start out setting the value of
that variable to false on line 52 and if
we don't find anything to remove that's
what it ultimately it'll contain and
that's what will return down here on
line 60 now inside the loop we're
checking the name of each item as before
but for matches found we remove the item
and that's the same as the previous
version of this function does but then
we also set result to true on the next
line and then we break out of the loop
because there's no point going around
again and then the return statement
online 60 that's going to be executed
when the loop terminates or when we
break out of it so either way we're
going to return the value of result all
right so the tester let's go back to
main and try dropping the silver ring
Conan dot drop loot double quotes this
time to a silver ring
and Conan show inventory. Alright so
let's run that and you can see up here
if we scroll up the score was 258 when
we dropped one of the other gold rings
then the second time the show injury was
caught after he dropped the silver ring
the valley drops down to 252 so we've
got one silver ring left finally and two
gold rings which is correct so that's
working fine all right so how about the
scenario that we're trying to drop
something that he doesn't have on him
there's a couple of ways we could write
that we can't just call the drop loot
function because we need to verify that
returns false so there's three ways you
may have done it let's actually have a
look at that so I will start with the
first one they're all dropped is equal
to conan dot drop loot and we'll type in
something that we know isn't present so
something not present then we can just
print out the value have dropped which
will be true or false depending on
whether it may need to drop it you may
also have done something like this print
'ln parentheses in conan dot drop loot
double quotes something else which will
also return or print out the value of
what was returned from drop loot which
would be false again because it doesn't exist
the third way might have been if
parentheses conan dot drop loot
parentheses in double quotes a bit of
junk against something that doesn't
exist in his inventory at present over
the code block then we'll print line
junk dropped else
print 'ln you don't have any junk so the
first way here on line 26 the result of
conan drop Lutz assigned to the
variable dropped we print that on the
next line to check that we did get false
and that can be combined into a single
line and you can see an example of that
on line 29 which should also print false
and we'll run this shortly
and finally we could test the return
value as a condition in an if statement
as you can see me doing on line 31
you print sent the message you don't
have any junk well it should when we run
it so that's just to confirm that that
actually works
we should get three folders showing up
well we got two folders I should say in
the third time we got you don't have any
junk so that's actually working fine
alright so that's the end of this Kotlin
tutorial in this course we hope you've
enjoyed it and got a lot out of it now
if you call the purpose of these videos
was to give you a quick understanding of
the basics of Kotlin there's a lot more
to the language and you'll see different
ways of writing code as you work through
the rest of this course now I haven't
been able to cover everything about
Kotlin in this tutorial section because
that would need an entire course in
itself but hopefully you now understand
the basics and should be able to
understand the code we use in the
Android apps so if you do think that you
could do with some more tutorials for
Kotlin then Sarah Etheridge has got a
fantastic Kotlin course that you should
check out and there's a link to that in
the last lecture in this course so check
that out if you want to know more about
Kotlin because she goes into great
detail and will help really take you to
the next level with Kotlin development
alright so the rest of this course
though is going to focus on Android and
I'm gonna be explaining the various
classes in the android framework as we
use them and it's worth watching the
videos in this section by the way a few
times if you're new to programming to
make sure you fully understand the
concepts we've covered once you do
you'll be really ready for the rest of
this course okay with that said I'll see
you in the next section.