Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active December 22, 2023 08:49
Show Gist options
  • Save udacityandroid/609dbb5370ba0665955a to your computer and use it in GitHub Desktop.
Save udacityandroid/609dbb5370ba0665955a to your computer and use it in GitHub Desktop.
Android for Beginners : Add the Chocolate Topping Checkbox Solution Java
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
// Figure out if the user wants whipped cream topping
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
// Figure out if the user wants chocolate topping
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
boolean hasChocolate = chocolateCheckBox.isChecked();
// Calculate the price
int price = calculatePrice();
// Display the order summary on the screen
String message = createOrderSummary(price, hasWhippedCream, hasChocolate);
displayMessage(message);
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice() {
return quantity * 5;
}
/**
* Create summary of the order.
*
* @param price of the order
* @param addWhippedCream is whether or not to add whipped cream to the coffee
* @param addChocolate is whether or not to add chocolate to the coffee
* @return text summary
*/
private String createOrderSummary(int price, boolean addWhippedCream, boolean addChocolate) {
String priceMessage = "Name: Lyla the Labyrinth";
priceMessage += "\nAdd whipped cream? " + addWhippedCream;
priceMessage += "\nAdd chocolate? " + addChocolate;
priceMessage += "\nQuantity: " + quantity;
priceMessage += "\nTotal: $" + price;
priceMessage += "\nThank you!";
return priceMessage;
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int numberOfCoffees) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + numberOfCoffees);
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
}
}
@orwa-te
Copy link

orwa-te commented Jun 25, 2018

Easy task but motivational

@adejuwonk1
Copy link

This code is a life saviour for a begginner. I have been wondering how i would debug the error in my code after adjusting few lines. but copy and pasting this code did the job

@mohameddss
Copy link

/**

  • IMPORTANT: Make sure you are using the correct package name.
  • This example uses the package name:
  • package com.example.android.justjava
  • If you get an error when copying this code into Android studio, update it to match teh package name found
  • in the project's AndroidManifest.xml file.
    **/

package com.example.android.copyjustjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.android.copyjustjava.R;

import java.text.NumberFormat;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    int quantity = 0;

    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
    • String message = "Total :" + "$" + (quantity * 5);
      displayMessage(message);

    او

    • int Price = quantity * 5;
      String message = "Total :" + "$" + Price;
      displayMessage(message);
      */

    public void submitOrder(View view) {

     CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
     boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
     CheckBox chocolateCheckBox = findViewById(R.id.chocolate_checkbox);
     boolean hasChocolate = chocolateCheckBox.isChecked();
    
    
    
     int Price = quantity * 5;
     String priceMessage = "Name: Mohamed salah gabr";
     priceMessage += "\nAdd whipped cream? "+ hasWhippedCream;
     priceMessage += "\nAdd Chocolate?  " + hasChocolate;
     priceMessage += "\nQuntity: " + quantity;
     priceMessage += "\nTotal: $" + Price;
     priceMessage +=  "\nThank You!";
     displayMessage(priceMessage);
    

    }
    /**

    • This method is called when the plus button is clicked.
      */
      public void increment(View view) {
      quantity = quantity + 1;
      display(quantity);

    }
    /**

    • This method is called when the minus button is clicked.
      */
      public void decrement(View view) {
      quantity = quantity - 1;
      display(quantity);

    }

    /**

    • This method displays the given price on the screen.
      */
      private void displayPrice(int number) {
      TextView priceTextView = (TextView) findViewById(R.id.order_summary_text_view);
      priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    /**

    • This method displays the given text on the screen.
      */
      private void displayMessage(String message) {
      TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
      orderSummaryTextView.setText(message);
      }
      }

@YassineOuardini
Copy link

package com.example.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {
    int quantity = 2;
    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      CheckBox whippercreamcheckbox = (CheckBox) findViewById(R.id.Whipped_Cream_checkbox);
      CheckBox chocolat = (CheckBox) findViewById(R.id.Whipped_Chocolat_checkbox);
      boolean hasWhpperCream = whippercreamcheckbox.isChecked();
      boolean haschocolat = chocolat.isChecked();

      calculatePrice(quantity);
      displayMessage(creatOrderSummary(hasWhpperCream, haschocolat));
      }

      private String creatOrderSummary(Boolean hasWhpperCream, Boolean haschocolat){
          int price = calculatePrice(quantity);
      
          String priceMessage  = "add whepperCream ? "+hasWhpperCream + "\nadd whepperChocolat ? "+haschocolat + "\nQuantity : "+quantity ;
          priceMessage = priceMessage + "\nTotal: $ " + price ;
          priceMessage = priceMessage + "\nThank you!";
      
               return priceMessage;
      }
      

    private int calculatePrice(int quantity){
    int price = quantity *5 ;
    return price ;
    }

    public void increment(View view) {
    quantity =quantity +1;
    display(quantity);
    }
    public void decrement(View view) {
    quantity=quantity -1;
    display(quantity);
    }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    private void displayMessage (String number) {
    int x = calculatePrice(quantity);

     TextView order_summaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
     order_summaryTextView.setText("Name : Yassine Ouardini\n" + number);
     if (x > 50)
         order_summaryTextView.setTextColor(Color.RED);
     else
         order_summaryTextView.setTextColor(Color.BLACK);
     }
    

    }

@YassineOuardini
Copy link

package com.example.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {
    int quantity = 2;
    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      CheckBox whippercreamcheckbox = (CheckBox) findViewById(R.id.Whipped_Cream_checkbox);
      CheckBox chocolat = (CheckBox) findViewById(R.id.Whipped_Chocolat_checkbox);
      boolean hasWhpperCream = whippercreamcheckbox.isChecked();
      boolean haschocolat = chocolat.isChecked();

      calculatePrice(quantity);
      displayMessage(creatOrderSummary(hasWhpperCream, haschocolat));
      }

      private String creatOrderSummary(Boolean hasWhpperCream, Boolean haschocolat){
          int price = calculatePrice(quantity);
      
          String priceMessage  = "add whepperCream ? "+hasWhpperCream + "\nadd whepperChocolat ? "+haschocolat + "\nQuantity : "+quantity ;
          priceMessage = priceMessage + "\nTotal: $ " + price ;
          priceMessage = priceMessage + "\nThank you!";
      
               return priceMessage;
      }
      

    private int calculatePrice(int quantity){
    int price = quantity *5 ;
    return price ;
    }

    public void increment(View view) {
    quantity =quantity +1;
    display(quantity);
    }
    public void decrement(View view) {
    quantity=quantity -1;
    display(quantity);
    }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    private void displayMessage (String number) {
    int x = calculatePrice(quantity);

     TextView order_summaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
     order_summaryTextView.setText("Name : Yassine Ouardini\n" + number);
     if (x > 50)
         order_summaryTextView.setTextColor(Color.RED);
     else
         order_summaryTextView.setTextColor(Color.BLACK);
     }
    

    }

@GianlucaVolpe
Copy link

I had to write this code, otherwise, when clicked on the checkbox I got the message JustJava has stopped. But in the code is not mentioned can someone explain, please?

`public void CheckBox(View view) {
boolean checked = ((CheckBox) view).isChecked();
}

public void CheckBox2(View view) {
    boolean checked = ((CheckBox) view).isChecked();
}`

@omeshkumar65
Copy link

This code also affect the price as per chocolatebox is checked or whippedCreamBox checked

`package com.o.just_java;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import java.text.NumberFormat;

import androidx.appcompat.app.AppCompatActivity;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    int quantity= 0;
    int price = 5;
    int whippedCreamPrice=0;
    int chocolatePrice=0;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */

    public void submitOrder(View view) {
    CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whippedCreamCheckBox);
    boolean hasWhippedCream= whippedCreamCheckBox.isChecked();

     CheckBox chocolateBox = (CheckBox) findViewById(R.id.chocolateBox);
     boolean hasChocolate= chocolateBox.isChecked();
    
     if(hasWhippedCream == true)
         {
             whippedCreamPrice=2;
         }
         else {
         whippedCreamPrice=0;
         }
     if(hasChocolate ==  true) {
         chocolatePrice=3;
     }
     else {
         chocolatePrice=0;
     }
    
     String priceMessage= "Name: "+"Omesh kumar"+
             "\nQuantity: " +quantity+
             "\nwippedCream: " + hasWhippedCream +
             "\nChocolate: " + hasChocolate +
             "\nTotal $ "+ quantity* (price +whippedCreamPrice +chocolatePrice) +
             "\nThankyou";
     displayMessage(priceMessage);
     // displayPrice(quantity*5);
    

    }

    public void Increment(View view)
    {
    quantity= quantity + 1;
    display(quantity);
    }

    public void Decrement(View view)
    {
    quantity= quantity - 1;
    display(quantity);
    }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    /**

    • This method displays the given price on the screen.
      */
      private void displayPrice(int number) {
      TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
      priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
      }
      private void displayMessage(String message) {
      TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
      priceTextView.setText(message);
      }
      }`

@yaz71
Copy link

yaz71 commented Sep 22, 2020

I did it using a different code, still worked :D ! thank you for the code tho !!

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