Welcome back.
And this video, we're going to add on operator functionality using containers and starts with on strings,
so strings have different kinds of methods.
So for example, let's create a string here so far.
My name will be Dennis, for example, or let's make it even a sentence.
So sentence will be.
Dennis is a coder, and I can check if sentence, for example, contains a certain word, for example,
so I could check if it contains the word Dennis.
What this method will then do is it will return true or it will return false.
So what I can now do is if sentence contains Venice, then I can return.
Well, I can just assign some text to my.
TV input, I could just say something like Dennis.
OK, let's just test as real quick and it will make a little clearer.
What's up?
So we see eight Dennis, nine Dennis and so forth.
So what happens here is we have this sentence and we check if the sentence contains something, even
if it contains something.
So the sentence has this something that we are checking with in this case, Dennis.
Then this if statement is true and the code that comes inside of those brackets will be executed.
And in this case, I use some stupid dummy cult, which just appends Dennis to it.
Super stupid stuff.
But this will just be important for you to further understand what we are going to use in a bit.
OK, and now we will need a new method that will check if an operator is at it.
So is operator.
Add it and I will need to pass a string to it.
And this method should return a Boolean.
OK, so we pass a string and it returns.
If Boolean, which means we need to return something now, we need to use this return keyword.
And now what I'm going to do is I'm going to use a new feature that we haven't used yet, and I'm just
going to use this if statement to then return true or false directly.
So this is pretty neat because you don't need to create all of the code before that.
You can just use it after the return statement.
So what I'm going to check is if the string that is given to this is operator added method that we are
creating here.
If it starts with a certain letter, so starts with and you see, there's this method that exists for
strings, which checks if a certain character is given at the beginning of that string that is passed.
So value is a string.
So if the string would be something like minus five, for example, now, then this statement where
it starts with a minus would be true.
And then whatever code comes after those brackets would be executed.
OK.
And in my case?
I'm just going to say false.
So what I do with this is I'm basically ignoring a minus here.
So what I want to do with this is once I press the minus button and let's rerun this real quick without
these lines, these lines here.
So.
What I want to check is if the mind is that the is at the beginning, I want to ignore it.
This basically means I want to be able to work with results that have negative values and so forth.
That's why I'm using this prefix minus.
So I'm just ignoring the miners at the beginning.
That's what I'm saying with the statement here.
And then else, I need to check a couple of things.
I need to check if the value contains, for example, a slash which would be division or if the value
contains.
A multiplication or a value contains.
A.
Plus and so forth.
OK, so we need to do that for all operations.
Let me do it for the final one as well here value that contains the minus sign.
So what will happen here is it will check if either of those statements is true.
So if there is a stash signed for division or multiplication sign or staff for multiplication, a plus
for addition and a minus for subtraction, if any of that is given, then it will return true.
And if none of those is given, it will also return false.
So that's what this is.
Operator added Method should basically do.
It will just tell us if in the text there is a minus a plus a multiplication or a slash sign, unless
it's at the beginning of the value that is given OK.
And the value that is given will be the text that we get from the text view.
So let's now use this method and therefore I'm going to use the on operator method that we will call
once we click on any of the operators.
So here first, I'm going to check if the last entry was a numeric and I'm going to check if the operator
so is Operator added.
Is not true.
So if this statement here will return false.
So here we can just check the TextView inputs.
The text to string now, unfortunately, we can just do it because text your input is another bill.
So what we need to do instead, then, is to use the LED operation.
So here TV input.
Question mark.
Text.
Question mark.
But let and only in that case, we can execute this code here.
So we need to use the safe call symbol to get the value before passing the parameter.
So we're checking does TV input text exist and if it does, then execute this court?
So that's what this TV input question mark, the text question mark that it does for us.
So here, if last numeric and is operator, add it with the text input that we have exists, then do
something with it.
So unfortunately, because we are using this alert here, we need to use it differently.
We cannot just use TV import text here, but the actual text is given to us with it.
So this it's here you can see it's great out it char sequence.
That's how this led thing works, right?
It gives us whatever is existing if this statement or both of these statements are true.
So if TV input is not empty and text is not empty, then it will give us the char sequence, which is
the text itself.
So now we can just go ahead and use it.
You see here it exists now.
And now what we can do with it is to use it inside of here so we can make a string out of it because
it is a chance sequence.
But we need you to be a string and then we can actually run some operations.
I'll go over this in a second once more because I think it's a little complicated what's up here?
But let's, first of all, make sure that we now append.
To our TextView inputs.
So we need to append whatever was inside the button.
OK, and then from that to text, so we get the view, which is this view that this passed to the on
operator method, which will be called once we click on any of the operators.
So, for example, this operator?
So here, instead of using on digit, I want to use on operator.
Same goes here instead of using on digit for the multiplication I want to use on operator for the minus
sign to same story on operator and for the plus sign.
This same story again and here for the plus sign as well.
I want to execute on operator.
So all those four buttons here will now call the on operator method.
This one here, which will check if the text you input is empty or not.
If it's not empty, it will do whatever we want to do in here.
So what we're saying is append whatever the button has a text to the existing text to input text and
then make sure that the last numeric is set to false and last dot is set to false as well.
So this it here that I'm using this, it is bold here is this char sequence, which is the value we
receive from the lambda.
So this year is a lambda that we have, which is automatically created for us when we are using let
and let is something you use for novels as we have them here.
This is one Nullarbor and another mobile, so you can have Nullarbor chaining here.
So just once again, really quickly, what we're checking is once we click on any of the operators here
we are checking is text and put empty.
Well, that's just something we have to do for safety.
Otherwise, we could crash our application if it was empty, but it's not in the way.
So we're done checking.
It's the last numeric.
So was the last valued that the user entered.
And no, because if it wasn't a no, we can't do the operation.
We don't want to do any operations because let's say we had this statement here.
Nine divided.
So what is this?
But there is no operation to be done.
So the last value that has been entered has to be a number, so it has to be numeric.
That's why we're checking is it numeric and also is an operator not added?
To the string already.
So that means do we already have an operator inside of this statement or not?
In this case, there is an operator already, so I cannot add another operator.
That's what I'm double checking here because if there is an operator already, I don't want to do anything.
But if there is no operator, add it yet, then I want to add the existing operator that is on the button
that I'm clicking right now.
So that's what this code here in the operator method will do, and that's why we needed this helper
method.
This is operator added for our purposes here.
Now let's run this, and I know this was a rather complex lecture, but now let's run this and see if
we can add more operators and you can see we are limited.
We can only add one operator, even though the mine is at the beginning, is being ignored, as you
see.
So we can have a minus at the beginning, but we cannot have a minus after that.
Now this is, of course, limiting us if we wanted to subtract or multiply negative numbers.
So here.
Let's say we want to multiply a negative number that wouldn't work, so that's something we cannot do
with our current solution that we have here.
But that's fine for now.
And in the next video, we are going to go ahead and see how we can add subtraction to our application.