-
-
Save udacityandroid/609dbb5370ba0665955a to your computer and use it in GitHub Desktop.
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); | |
} | |
} |
Thanks
Done
thanks alot
How come when I click on the whipped cream, the order summary says true for both whipped cream and chocolate and when I click on just chocolate, both are false?
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 priceMessage = createOrderSummary(price, hasWhippedCream, hasChocolate);
displayMessage(priceMessage);
}
/**
-
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 the user wants whipped cream topping
- @param addChocolate is whether or not the user wants chocolate topping
- @return text summary
/
private String createOrderSummary(int price, boolean addWhippedCream, boolean addChocolate) {
String priceMessage = "Name: Cristina";
priceMessage += "\nAdd whipped cream? " + addWhippedCream;
priceMessage += "\nAdd chocolate? " + addWhippedCream;
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);
}
- This method is called when the plus button is clicked.
}
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.justjava.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:paddingBottom="16dp"
android:text="TOPPINGS"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/whipped_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:buttonTint="@android:color/white"
android:paddingLeft="24dp"
android:text="Whipped Cream"
android:textColor="@android:color/white"
android:textSize="24sp" />
<CheckBox
android:id="@+id/choco_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:buttonTint="@android:color/white"
android:paddingLeft="24dp"
android:text="Chocolate"
android:textColor="@android:color/white"
android:textSize="24sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:paddingBottom="16dp"
android:text="QUANTITY"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:background="@android:color/black"
android:onClick="decrement"
android:text="-" />
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="16dp"
android:text="2"
android:textColor="@android:color/white"
android:textSize="16sp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@android:color/Black"
android:onClick="increment"
android:text="+" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:paddingTop="16dp"
android:text="ORDER SUMMARY"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/ordrer_summary__text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="$10"
android:textColor="@android:color/white"
android:textSize="16sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@android:color/Black"
android:onClick="submitOrder"
android:text="ORDER" />
</LinearLayout>
You know when you get it 'wrong' but it still seems to work? I didn't use any 'findviewByID for either check box. The following seems to work using onClick in the XML file to get to java:
boolean hasWhippedCream = false;
boolean hasChocolate = false;
...........
public void clickCheckbox(View view){
hasWhippedCream = ((CheckBox) view).isChecked();
}
public void clickChocCheckbox(View view){
hasChocolate = ((CheckBox) view).isChecked();
}
So has Android Studio been updated so it doesn't need that it doesn't need to find the display object, or is my method simply different?
MAIINACTIVITY
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;
public class MainActivity extends AppCompatActivity {
int quantity = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void calculatePrice(int quantity) {
int price = quantity * 5;
}
private int calculatePrice() {
int price = quantity * 5;
return price;
}
private void displayQuantity(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
public void increment(View view) {
quantity = quantity + 1;
displayQuantity(quantity);
}
public void decrement(View view) {
quantity = quantity - 1;
displayQuantity(quantity);
}
public String createOrderSummary(int price, boolean addwhippedcream, boolean addChocolate) {
String pricemessage = "Name: khathrene";
pricemessage = pricemessage + "\nAdd whippedcream ? " + addwhippedcream;
pricemessage = pricemessage + "\nAdd chocolate? " + addChocolate;
pricemessage = pricemessage + "\nQuantity: " + quantity;
pricemessage = pricemessage + "\ntotal:$ " + price;
pricemessage = pricemessage + "\n Thank you";
return pricemessage;
}
public void submitOrder(View view) {
CheckBox whippedcreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean haswhippedcream = whippedcreamCheckBox.isChecked();
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
boolean haschocolate = chocolateCheckBox.isChecked();
int price = calculatePrice();
String pricemessage = createOrderSummary(price, haswhippedcream, haschocolate);
displayMessage(pricemessage);
}
/**
* 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);
}
}
Couldn't we just create the two CheckBoxes for the whipped cream and chocolate in the createOrderSummary method? That way we wouldn't be adding any extra input parameters to the method createOrderSummary cuz the CheckBox and booleans are initiated within the method.
I found this as an easier way to add the CheckBox without messing in the submitOrder method too and just dealing with the createOrderSummary method only. The method would look like this:
private String createOrderSummary(int price) { boolean hasWhippedCream = ((CheckBox) findViewById(R.id.whipped_cream_checkbox)).isChecked(); boolean hasChocolate = ((CheckBox) findViewById(R.id.chooclate_checkbox)).isChecked(); String priceMessage = "Name: Omar Ashraf"; priceMessage += "\nAdd whipped cream? " + hasWhippedCream; priceMessage += "\n Add chocolate? " + hasChocolate; priceMessage += "\nQuantity: " + quantity; priceMessage += "\nTotal: $" + price; priceMessage += "\nThank you!"; return priceMessage; }
Also couldn't we create the boolean variable for the chocolate CheckBox in just one line like this?:
boolean hasChooclate =((CheckBox) findViewById(R.id.chooclate_checkbox)).isChecked();
Thank you.
It's wonderful... I have completed it without seeing the solution.
Easy task but motivational
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
/**
- 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);
}
}
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); }
}
-
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); }
}
-
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();
}`
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);
}
}`
- This method is called when the order button is clicked.
Visit my github to see the code xP.
XML Code:
https://github.com/deepak9962/just-java/blob/master/app/src/main/res/layout/activity_main.xml
MainActivity:
https://github.com/deepak9962/just-java/blob/master/app/src/main/java/com/justjava/MainActivity.java
I did it using a different code, still worked :D ! thank you for the code tho !!
now there is a problem because the app just crahed