Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active February 6, 2023 14:25
Show Gist options
  • Select an option

  • Save udacityandroid/2d4afee7c69adc6df544 to your computer and use it in GitHub Desktop.

Select an option

Save udacityandroid/2d4afee7c69adc6df544 to your computer and use it in GitHub Desktop.
Android for Beginners : Negative Number of Cups of Coffee Extra Challenge Solution
/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
if (quantity == 100) {
// Show an error message as a toast
Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
// Exit this method early because there's nothing left to do
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
if (quantity == 1) {
// Show an error message as a toast
Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
// Exit this method early because there's nothing left to do
return;
}
quantity = quantity - 1;
displayQuantity(quantity);
}
@Afandy1

Afandy1 commented Dec 8, 2017

Copy link
Copy Markdown

it is better to write
public void decrement (View view){
if (quantity<=1){
// show an error message as a Toast.
Toast.makeText(this,"You cannot have less than 1 coffe",Toast.LENGTH_SHORT).show();
return;
// Exit this method early because there is nothing left to do.
}
quantity=quantity-1;
displayQuantity(quantity);
}
because once first implement , the decrement method get minus and the number of coffees will get less than 0 .

@razakadegoke

Copy link
Copy Markdown

Why the "return;" make the method increment and decrement not go further
` public void increment(View view) {

    if (quantity == 100) {
        // Show an error message as a toast
        Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
        // Exit this method early because there's nothing left to do
        return;
    }
    quantity = quantity + 1;
    displayQuantity(quantity);

}

public void decrement(View view) {

    if (quantity == 1) {
        // Show an error message as a toast
        Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        // Exit this method early because there's nothing left to do
       return;
    }
    quantity = quantity - 1;
    displayQuantity(quantity);
}`

@nineOneOnePawel

Copy link
Copy Markdown

In question no.4 there is mistake as when you click answer quantity==100 it tells you to try again.
Only accepts the answer when you pick quantity<=100...

@JamieBe

JamieBe commented Feb 4, 2018

Copy link
Copy Markdown

Works fine with me.

/**
* This method is called when the plus button is clicked.
* this is the increment method
*/
public void increment(View view) {
if (quantity >= 100){
//show an error message as a toast
Toast.makeText(this, "Mehr als 100 Tassen gibt es nicht", Toast.LENGTH_SHORT).show();
return;
}
quantity = quantity + 1;
display(quantity);
// TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
// quantityTextView.setText("" + quantity);
}

/**
 * This method is called when the minus button is clicked.
 * This is the decrement method
 */
public void decrement(View view) {
    if (quantity < 1){
        //show an error message as a toast
        Toast.makeText(this, "Weniger als 1 Tasse gibt es nicht", Toast.LENGTH_SHORT).show();
       return;
    }
    quantity = quantity - 1;
    display(quantity);
    //TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    //quantityTextView.setText("" + quantity);
}

@Anmar89

Anmar89 commented Mar 2, 2018

Copy link
Copy Markdown

public void increment(View view) {

    if (quantity == 100) {
        Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
        return;
    }
    quantity++;
    display(quantity);

}

/**
 * This method is called when the  minus button is clicked.
 */
public void decrement(View view) {
    if (quantity == 1) {
        Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        return;
    }
    quantity--;
    display(quantity);

@mxacan

mxacan commented Mar 4, 2018

Copy link
Copy Markdown

/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
if(quantity>=0){
quantity = quantity + 1;
displayQuantity(quantity);}
if(quantity>=100){
quantity = 100;
displayQuantity(quantity);
}
}

/**
 * This method is called when the minus button is clicked.
 */
public void decrement(View view) {
    if(quantity<=100){
    quantity = quantity - 1;
    displayQuantity(quantity);}
    if(quantity<=0){
        quantity=0;
        displayQuantity(quantity);
    }
}

@abdullah-alialdin

Copy link
Copy Markdown

This my code and work well

/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {

    //to prevent the user adding more than 100 cups
    if (quantity > 99){
        //to display a toast message
        Context context = getApplicationContext();
        CharSequence text = "you cannot have more than 100 cups";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        return;
    }
    else {quantity = quantity + 1;}
    displayQuantity(quantity);
}

/**
 * This method is called when the minus button is clicked.
 */
public void decrement(View view) {


    //to prevent the user adding less than 1 cup
    if (quantity < 2){
        //to display a toast message
        Context context = getApplicationContext();
        CharSequence text = "you cannot have less than 1 cups";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        return;
    }
    else {quantity = quantity - 1;}
    displayQuantity(quantity);
}

@Team31250

Copy link
Copy Markdown

use it for every solution

/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
if (quantity >= 100) {
// Show an error message as a toast
Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
// Exit this method early because there's nothing left to do
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}

/**
 * This method is called when the minus button is clicked.
 */
public void decrement(View view) {
    if (quantity <= 1) {
        // Show an error message as a toast 
        Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        // Exit this method early because there's nothing left to do
        return;
    }
    quantity = quantity - 1;
    displayQuantity(quantity);
}

this is right code for any problem

@badrddinb

Copy link
Copy Markdown

DONE

@waelhabbal

Copy link
Copy Markdown

Hi ...
@RandomYogi has been noticed that quantity become "0" when rotate the phone ... seems that the class MainActivity re-create... can anyone explain the reason.. to try to avoid that

thanx

@xbarakota

Copy link
Copy Markdown

Done

@louayeldin

Copy link
Copy Markdown

Done

@Rosariobc

Rosariobc commented Mar 31, 2018

Copy link
Copy Markdown

Update: Solved!!! I forgot to import the toast widget!

I did the code correct, but toast is not resolved... how should I correct it? Thanks

@Eduese

Eduese commented May 30, 2018

Copy link
Copy Markdown

Going back to the quiz

@eiadt

eiadt commented Jun 7, 2018

Copy link
Copy Markdown

I used this method and it's OK:
`` public void increment(View view) {
if (quantity<=99) {
quantity = quantity + 1;
}else {
Toast.makeText(this, "You cannot have more than 100 cups", Toast.LENGTH_SHORT).show();
}
displayQuantity(quantity);

}

/**
 * this method is called when the - button is clicked
 */
public void decrement(View view) {


    if (quantity>=2) {
        quantity = quantity - 1;
    }else {
        Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
    }

    displayQuantity(quantity);

}

@tooptooptoop

Copy link
Copy Markdown

thanks

@alothaimeen

alothaimeen commented Jul 14, 2018

Copy link
Copy Markdown

I used this code and it works perfectly.

``
`

/**
 * This method is called when the plus button is clicked.
 */
public void increment(View view) {
    if (quantity < 100) quantity = quantity +1;
    else {
        // Show an error message as a toast
        Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
    }
    displayQuantity(quantity);

}
/**
 * This method is called when the minus button is clicked.
 */
public void decrement (View view) {
    if (quantity > 1) quantity = quantity -1;
    else {
        // Show an error message as a toast
        Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
    }
    displayQuantity(quantity);

}

`

@m7oda11

m7oda11 commented Sep 23, 2018

Copy link
Copy Markdown

anyone can share the full java code ?

@juanromo13

Copy link
Copy Markdown

i can use this solution?

public void decrement(View view) { if (quantity > 1) { quantity--; } else { Toast.makeText(this,"You cannot have less than 1 coffee",Toast.LENGTH_SHORT).show(); } displayQuantity(quantity); }

@TGRBTRFLY

TGRBTRFLY commented Mar 20, 2019

Copy link
Copy Markdown

If someone is dumb enough to need a TOAST message telling them that they can't order less than 1 cup... they shouldn't be ordering hot drinks. 8P Just use: if (quantity > 0) --- see below

int quantity = 0;
/**
* This method is called when the minus button is clicked.
/
public void decrement(View view) {
if (quantity > 0)
quantity = quantity - 1;
displayQuantity(quantity);
}
/
*
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
if (quantity == 100) {
// Show error message as toast
Toast.makeText(this, "You cannot order more than 100 cups", Toast.LENGTH_SHORT).show();
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}

Well, I guess they could order toppings only, but if this were a real app I wouldn't use this layout anyway.

@sslaia

sslaia commented May 29, 2019

Copy link
Copy Markdown

The solution code has flaw if the quantity variable is initialised with 0 value, as some here have found out. Clicking on minus sign would make the number go down to smaller than 1 (even to minus). The solution is to set the initial quantity value to 1:
int quantity = 1;

@NarFooZ

NarFooZ commented Jun 5, 2019

Copy link
Copy Markdown
public void increment(View view) {
    if (quantity > 99){
        Context context = getApplicationContext();
        CharSequence text = "The maximum of Coffee cups is 100!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }else{
        quantity = quantity + 1;
    }


    displayQuantity(quantity);
}

/**
 * This method is called when the minus button is clicked.
 */
public void decrement(View view) {
    if (quantity < 2){
        Context context = getApplicationContext();
        CharSequence text = "The minimum of Coffee cups is 1!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
                }else{
        quantity = quantity - 1;
    }

    displayQuantity(quantity);
}

@yaminebl

Copy link
Copy Markdown

This is mine

     public void increment(View view) {
            if (quantity == 10) {
                Context context = getApplicationContext();
                CharSequence text = "You can't order more than 10";
                int duration = Toast.LENGTH_SHORT;
                Toast toastMessage = Toast.makeText(context, text, duration);
                toastMessage.show();
                return;
            }
            quantity = quantity + 1;
            displayQuantity(quantity);
        }

        public void decrement(View view) {
            if (quantity == 1) {
                Context context = getApplicationContext();
                CharSequence text = "You can't order less than 1";
                int duration = Toast.LENGTH_SHORT;
                Toast toastMessage = Toast.makeText(context, text, duration);
                toastMessage.show();
                return;
            }
            quantity = quantity - 1;
            displayQuantity(quantity);
        }

@alb-noah

alb-noah commented Mar 31, 2020

Copy link
Copy Markdown

to solve this I preferred to use this code...

 if (quantity > 0) {
           quantity = quantity - 1;
        } else {
            Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        }
        displayQuantity(quantity);

this way solves the problem @tompad2, but as Katherine said, there are many ways of solving these issues

is better to give quantity variable =1
and make if statement start with 1
if (quantity >1 )
so you can't get less than 1 cup

@YoussefNachit

Copy link
Copy Markdown

here is another method ( I start with quantity=2)
public void increment (View view) {
if (quantity<100){
quantity++;
display(quantity);
} else {
Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
return;
}
}
public void decrement (View view) {

    if (quantity>=2){
        quantity--;
        display(quantity);
    } else {
        Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        return;
    }
}

@Ghazali-Sufi

Copy link
Copy Markdown

to solve this I preferred to use this code...

 if (quantity > 0) {
           quantity = quantity - 1;
        } else {
            Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        }
        displayQuantity(quantity);

this way solves the problem @tompad2, but as Katherine said, there are many ways of solving these issues

to solve this I preferred to use this code...

 if (quantity > 0) {
           quantity = quantity - 1;
        } else {
            Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        }
        displayQuantity(quantity);

this way solves the problem @tompad2, but as Katherine said, there are many ways of solving these issues

i also solved this way:

if(quantity == 0){
Toast.makeText(MainActivity.this, "You can not have less than 1 cup of coffee", Toast.LENGTH_SHORT).show();
return;
}

instead of 1 i used 0 ===> Happy Coding

@Ghazali-Sufi

Copy link
Copy Markdown

i also solved this way:

if(quantity == 0){
Toast.makeText(MainActivity.this, "You can not have less than 1 cup of coffee", Toast.LENGTH_SHORT).show();
return;
}

instead of 1 i used 0 ===> Happy Coding

@eng4flo

eng4flo commented Oct 13, 2020

Copy link
Copy Markdown

Hi ...
@RandomYogi has been noticed that quantity become "0" when rotate the phone ... seems that the class MainActivity re-create... can anyone explain the reason.. to try to avoid that

thanx

Isnt quantity set to 0?? If so... if you start to click decrement when app is just started it would be doing -1....or?

Hi, I think that will work well
//AS quantity =2
// increase quantity by 1
public void increment(View view) {

    if (quantity == 100) {
        quantity = 100;
        Toast toast = Toast.makeText(context, "It isn't available, Try again", Toast.LENGTH_SHORT);
        toast.show();
             } else {
        quantity = quantity + 1;
    }
    display(quantity);
}

// decrease quantity by 1
public void decrement(View view) {


    if (quantity == 1) {
        quantity = 1;
        Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
    } else {
        quantity -= 1;
    }
    display(quantity);
}

@mixspark

Copy link
Copy Markdown

nice code

@sudhir72350999

Copy link
Copy Markdown

nice this is nice lecture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment