-
-
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); | |
} | |
} |
All good here.
Done.. perfect work.
This is my version with notification
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark">
<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/white"
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/white"
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/white"
android:onClick="submitOrder"
android:text="ORDER" />
</LinearLayout>
</ScrollView>
package com.example.android.justjava;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
int quantity = 2, pricePerCup = 5;
/**
* This app displays an order form to order coffee.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View des = new TextView(getApplicationContext());
des.setVisibility(View.GONE);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
createOrderSummary();
Toast toast = Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT);
toast.show();
}
/**
* Calculates the price of the order.
*
* @param quantity is the number of cups of coffee ordered
* @param pricePerCup is the price of the cup
*/
private int calculatePrice(int quantity, int pricePerCup) {
return quantity * pricePerCup;
}
/**
* Creates an order summary
*/
private void createOrderSummary() {
CheckBox isWhipped = findViewById(R.id.whipped_checkbox);
CheckBox isChocolate = findViewById(R.id.choco_checkbox);
int totalPrice = calculatePrice(quantity, pricePerCup);
String report = "Name : Waleed El-Badry" + "\n" +
"Add Whipped Cream? " + isWhipped.isChecked() + "\n" +
"Add Chocolate? " + isChocolate.isChecked() + "\n" +
"Quantity : " + quantity + "\n" +
"Price : $" + totalPrice;
TextView reportTextView = (TextView) findViewById(R.id.ordrer_summary__text_view);
reportTextView.setText("" + report);
}
/**
* This method is called when increment button is clicked
*/
public void decrement(View view) {
if (quantity <= 0)
quantity = 0;
else
quantity -= 1;
displayMessage(quantity);
}
/**
* This method is called when decrement button is clicked
*/
public void increment(View view) {
quantity += 1;
displayMessage(quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayMessage(int value) {
TextView quantityTextView = findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + value);
}
/**
* THis method displays the given price value on the screen
*/
private void displayPrice(int number) {
String price = "Total of $" + number + "\n" + "Thanks!";
displayMessage(price);
}
/**
* Testing string method
*/
private void displayMessage(String msg) {
TextView msgTextView = findViewById(R.id.ordrer_summary__text_view);
msgTextView.setText(msg);
}
}
Thank for providing the whole Java file
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_value;
@OverRide
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
display_quantity ( quantity_value );
}public void submit_Order ( View view ) {
calculatePrise ();
displayMessage ( createOrderSummary () );
}private void display_quantity ( int number ) {
final TextView quantityTextView = findViewById ( R.id.quantity_text_view );
String quantity_text_view = "" + number;
quantityTextView.setText ( quantity_text_view );
}public void increment ( View view ) {
quantity_value = quantity_value + 1;
display_quantity ( quantity_value );
}public void decrement ( View view ) {
quantity_value = quantity_value - 1;
display_quantity ( quantity_value );
}private int calculatePrise () {
return quantity_value * 5;
}private void displayMessage ( String message ) {
TextView orderSummaryTextView = findViewById ( R.id.order_summary_text_view );
orderSummaryTextView.setText ( message );
}private String createOrderSummary () {
CheckBox whipped_cream_CheckBox = findViewById ( R.id.whipped_cream_CheckBox );
final boolean checked_cream = whipped_cream_CheckBox.isChecked ();CheckBox chocolate_CheckBox = findViewById ( R.id.chocolate_CheckBox ); final boolean checked_chocolate = chocolate_CheckBox.isChecked (); String summaryText = "Name: Sama Soft"; summaryText += "\nAdd Whipped Cream: " + checked_cream; summaryText += "\nAdd chocolate: " + checked_chocolate; summaryText += "\nQuantity: " + quantity_value; summaryText += "\nTotal Price: $" + calculatePrise (); summaryText += "\nThank you!"; return summaryText;
}
}
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.justjava.MainActivity">
<TextView
android:id="@+id/text_view_topping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/topping"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@android:color/black"
android:textSize="16sp" />
<CheckBox
android:id="@+id/whipped_cream_CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/whipped_cream"
android:textSize="16sp" />
<CheckBox
android:id="@+id/chocolate_CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/chocolate"
android:textSize="16sp" />
<TextView
android:id="@+id/text_view_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/quantity"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@android:color/black"
android:textSize="16sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="increment"
android:padding="16dp"
android:text="@string/increment"
tools:ignore="OnClick"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="168dp" />
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="@string/_0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:id="@+id/button_decrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="decrement"
android:text="@string/decrement"
tools:ignore="OnClick" />
</LinearLayout>
<TextView
android:id="@+id/price_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/order_summary"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/order_summary_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/_0_00"
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:id="@+id/button_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submit_Order"
android:padding="16dp"
android:text="@string/order"
tools:ignore="OnClick" />
</LinearLayout>
My android studio always flag these words red saying "unable to resolve." "whippedCreamCheckBox;" and "chocolateCheckBox;" under sumbmitOrder (View view). All I did is to add these lines below to the "public class MakingAppInteractive extends AppCompatActivity" and problem solved.
private CheckedTextView whippedCreamCheckBox;
private CheckedTextView chocolateCheckBox;
I just hope it wont be a problem in the future!
now there is a problem because the app just crahed
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 !!
Android Studio shows me this Error: "Error:(49, 9) error: cannot find symbol class CheckBox" How could I solve this problem?
package com.example.android.justjava;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
/**
This app displays an order form to order coffee.
/
public class MainActivity extends Activity {
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;
}
/**
/
public void increment(View view) {
quantity = quantity + 1;
displayQuantity(quantity);
}
private String createOrderSummary(int price, boolean hasWhippedCream, boolean hasChocolate) {
String priceMessage = "Name: Wreia" ;
priceMessage += priceMessage + "\nAdd Whipped Cream? " + hasWhippedCream;
priceMessage += priceMessage + "\nAdd Chocolate? " + hasChocolate;
priceMessage += priceMessage + "\nQuantity: " + quantity;
priceMessage += priceMessage + "\nTotal: $" + price;
priceMessage += priceMessage + "\n Thank you!";
return priceMessage;
}
private int calculatePrice() {
int price = quantity * 5;
return price;
}
public void submitOrder(View view) {
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasChocolate = chocolateCheckBox.isChecked();
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
int price = calculatePrice();
String priceMessage = createOrderSummary(price, hasWhippedCream, hasChocolate);
displayMessage(priceMessage);
}
/*
*/
public void decrement(View view) {
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
/
private void displayQuantity(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/*
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
}
}