Welcome back.
In this video, we are going to look at the on operator functionality, which we need to implement because
as you can see, we can have multiple operators just after another so we can add more and more.
Well, in this case, we cannot add more than that.
But as you can see, we can add plenty.
And we should always only be able to do one operation at a time because we're not using Brackett's or
anything.
And I want to keep it super simple.
So this calculator, as I said at the beginning, is not going to be a fully fledged and super amazing
calculator.
It's really going to be very basic.
But in order to achieve this so that we can only add one of those.
Operators to Artex, the ones we can use, something called contains, so we can use the contains functionality
of a TextView, so let's say somebody presses on to our digit.
So what I can check.
Is the TV, the DOT or TV input that contains.
And then what I want to specifically check now here, I, of course, need to add the text property
because I'm using the text here.
So I'm looking at the text of the TV input.
And dear, I can use this contains method.
So this contains method is not available in TV and it only is available in text or in the text of the
TV input.
All right.
So now I can check if it contains one.
So this will return true or false.
So I can check.
If this is true then I want to.
Let's say write something onto TV input.
Something useless, like, ha ha.
OK, so let's try this.
So here, let's say I entered this one, as you can see, says ha ha, because the text contains I want
now if I press any other button, it's not going to be a problem.
But once I press the one, it's going to say, ha ha, because the whole text that we have here, this
text view, it contains this text.
Being a one, so this contains checks if a value is available and returns true or false.
All right.
So that was just to show you the functionality, because we are going to use it now for a new function
that we created.
And I will need to check if the operator is edit and therefore I'm going to create a function here,
which I'm going to make private because it's not necessary to make it public or available for anyone
else.
It will only be required in this main activity, not an activity maniac's email and so forth.
So I can just go ahead and create it here.
So I'm going to call this one is Operator added, and I need to get a value which is of type string,
so whenever we pass this function, I value it should be a type string and it's going to return a Bulliet.
So it's going to say true or false as a return value.
Now, of course, it needs to return something.
And the cool thing is we can use a functionality that we have not seen yet, which is return.
If so, we can say return if a value starts with a certain value.
So here I'm using starts with again and you function this is a function that is available in strings
or for strings and value is of type string, as we have defined here.
So now we can use it and we can check if the value starts with a minus.
Why would I even use that?
Well, I need to use that because let's say we do a calculation where the value becomes a negative value,
then we will have a minus, so to speak, an operator at the beginning of the text.
So we need to check that.
For example, let's say I do a calculation here and I get this minus sixty five as a result, then this
minus should not be the operator that limits me from adding another operator.
OK, it's just.
Indicating that this value is negative, so I'm saying return, if value starts with minus, then it
should just say false.
And ELT's.
It should check if the value contains.
One of the operators, so, for example, it already contains a slash or.
It contains.
A multiplication science, so an Asterix contains.
Star.
Then.
Value that contains.
Plus.
Poor value it contains.
Hey, minus.
And this will now allow us to only had one of those operators, but if it's a minus at the beginning,
it's still going to work.
So now we can use that.
We can use that for our own operator function.
So I'm going to create a new function here.
Fun on operator.
And again, we need to use a view here and now I'm going to check if last numeric and the is operator
edit.
Of our TV input text.
That to string.
Is not true.
So I'm checking Latinoamerica, that's our variable that we created early on, which is this boolean
and then I'm checking if it is operator error with a text that our TextView currently has as a string,
because as we see here is operator, it requires a string, it doesn't require of type text, it requires
of type string.
So we need to convert it into a string.
And then if all of that is true.
So this whole statement is true, then we can go ahead and append the element to our TextView.
So whatever the operator is to our text you.
And we may not forget to reset last numeric because now last number is clearly going to be false and
last thought is also going to be false because now we had an operator, right?
So it's neither a numeric nor a dot value so that we can go ahead and use an operator to all of our
operators.
So here on Operator.
Then here on Operator the minus.
On operator, and finally the plus as well.
All right, and now we can restart our application and see if all of that works.
All right, so that we are.
Let's do a calculation times six.
Now, let's add another operator, as you can see, doesn't accept any further operators of these buttons,
they just don't do anything because we already have an operator there.
And of course, the equal sign is not working, so we cannot test any thing else.
Now, as you can see, I cannot add the minus at the beginning, but having a minus at the beginning
of a result will work later on.
But of course, we cannot check results yet.
So we need to implement this equal button here, which is going to give us the result.
And that is going to be the toughest challenge because it's a long code that we need to add there.
We need to do a bunch of checks and everything in order to make sure that everything is working.
So I'd say that's something that we keep for the next video.
So I'll see you there.