Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active February 6, 2023 14:25
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 31 You must be signed in to fork a gist
  • Save udacityandroid/2d4afee7c69adc6df544 to your computer and use it in GitHub Desktop.
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);
}
@YoussefNachit
Copy link

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

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

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
Copy link

eng4flo commented Oct 13, 2020

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

nice code

@sudhir72350999
Copy link

nice this is nice lecture

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