YouSub
Untitled

en English

Untitled English subtitles

0 downloads · 71 lines · added Dec 12, 2015

Report a problem
What's wrong?
Advertisement

English transcript

There isn't just one correct way to answer this question.
It really depends on what's most useful for you to identify the difference
between the incorrect app behavior and the correct app behavior.
Here are some of the thing that I noticed.
In the incorrect app behavior, when you click on the order button
the quantity changes to the same value each time and it shouldn't change.
Next I also notice that the price was correct for
this quantity value, but really the price should be for
the last quantity that was selected in the quantity picker.
Again, this is related to the fact that quantity shouldn't change.
Let's look at the existing code for why this is happening.
In the activity_main layout file, We have the Order button declared here.
When the Order button is clicked,
we're going to call a method in the main activity called submitOrder.
In the MainActivity.java file, here's the submitOrder method.
This is old code from a long time ago that we haven't changed yet.
But we create an integer quantity variable and initialize it to 5.
We display that to the screen.
We also display price as the number of cups of coffee times $5 each.
Already this code don't make a lot of sense, because as we're trying to submit
the order we're trying to update the quantity.
And another problem is that we're creating an integer quantity variable
when we already have a global quantity variable that's supposed to keep track
of the number of cups of coffee.
In the MainActivity we already have a global quantity variable,
so any method inside here can refer to this quantity variable.
However, in the submitOrder method we're creating a new local quantity variable.
Technically you can have a local variable called the same name
as a global variable.
This just means that in the context of this method,
after this variable has been declared, whenever you refer to quantity,
it actually refers to this local quantity variable with value 5,
not the global quantity variable of value 2.
So display(quantity) and
displayPrice(quantity *5) both refer to this quantity value.
This is the scope of this local variable.
Outside of this scope,
whenever you refer to quantity it means this global variable quantity.
If you think about it,
though, it doesn't really make sense that when you submit your order,
you would create a new quantity and then display that on the screen.
What you really want to do is just use the quantity that we've been keeping
track of as we've been increasing and decreasing the number of cups of coffee.
So that's what we should calculate our price off of.
To fix our code, we should not declare a local quantity variable.
Instead we should just use this global quantity variable.
We should also stop trying to update the quantity on the screen.
The plus and minus buttons already handle updating the screen
with the proper quantity value.
We do, however, want the submitOrder button to still update the price.
But this time we're going to calculate the price as quantity * 5
using the global quantity variable.
I'm going to update the code so that it reflects what we just talked about.
Notice that the quantity variable has turned into a purple font color,
which means that we're using the global quantity variable in the MainActivity.
When the button is pressed it will trigger the submitOrder method.
This will display the price on the screen,
where the price is calculated as the global quantity variable times 5.
This looks correct, so let's run it on our device.
Okay, now for the ultimate test.
I'm going to change the quantity to 6 and then click on the Order button, and
yay, it's $30.
Let me try other values as well so I know it's not a fluke.
That looks correct. That looks correct.
Cool, it works!
You just fixed your first bug.
You did really well on that task.
The functionality of our app is working really well.
But I have to admit, the UI is a little bit ugly.
In particular, the quantity picker could use some UI love, so
let's work on that next.

Need subtitles from another video?