Return to Video

Fix the Order Button - Solution

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

more » « less
Video Language:
English
Team:
Udacity
Project:
UD837 - Android for Beginners
Duration:
03:39

English subtitles

Revisions Compare revisions