YouSub
Untitled

en English

Untitled English subtitles

0 downloads · 72 lines · added Dec 11, 2015

Report a problem
What's wrong?
Advertisement

English transcript

We've already walked through the first two steps in the last video.
Now I'm going to show you a couple of things you could have experimented with.
I would like to change the initial quantity value so
that when the app launches it shows up as zero instead of two.
The way I can do that is to modify this global quantity variable
to be initialized at zero.
Then the increment and decrement method should work just fine.
When the increment method is called,
it will use the latest quantity value and just add one.
When the decrement method is called, it will just decrease the quantity by one.
Let's go ahead and try this out now.
That looks weird.
I wanted it to show up at zero.
If I click the plus button, it's weird, it goes to one,
which seems to have decreased it, but now it starts to increase.
So it seems like these buttons are working, but for
some reason the initial value was wrong.
Oh, we forgot to change the activity main layout file.
In this layout we have a text view that says the initial text
quantity value is two.
That should fix our problem with the two showing up when the app launches.
Cool. Now it shows up
as initial quantity of zero.
Then if I hit increase, it updates properly.
If I hit decrease, it also updates properly.
You can see that it's a little tricky that we have the initial quantity
value in two places.
Having two places in the code that represent the same thing
can be a source of bugs in the future, like we just saw now.
An advanced way to solve this problem is to have the main activity actually
display the quantity of zero immediately when the app launches,
instead of relying on the layout to have an initial value.
That way the single source of truth would be this global variable.
But don't worry about that now.
That's more of an advanced thing that you can do after Lesson 3.
The other way we can experiment with the code is to have the plus button
double the number of coffees ordered each time and
have the minus button half the number of coffees each time.
Let's start with an initial quantity of two, so
that if we hit the minus button first, we can at least cut this in half once.
When the increment method is called,
we want to double the number of coffees ordered.
We should take the current quantity and then multiply it by two.
That gets updated to be the new quantity value and
then we display that to the screen.
On the other hand, we have the decrement method.
For the decrement method, we want to cut the number of coffees ordered in half.
So we can take the current quantity and then divide it by two.
That will be the new quantity value.
And we display that to the screen.
Let's try it out.
You can see how it can be a source of error every time you want
to change the initial quantity value.
You have to change it in both places.
Let's run the app now.
Okay.
It starts off at an initial quantity of two.
If I hit plus, it should double the quantity each time.
Cool. And it does.
If I hit the minus button,
it should cut the number of coffees ordered in half each time.
And it works.
If I run it in the debugger, you can see how it updates the variables each time.
The quantity value starts off at two, and then it updates to four.
And then the quantity starts off at four, and
then it updates to eight and so on.
I'm sure you came up with much more creative ways to play around with this
quantity picker.
Hopefully that helped you better understand the use of global variables.
But if I hit the order button, it doesn't show me the price for
that number of coffees ordered.
Let's fix that in the next video.

Need subtitles from another video?