Last active
December 22, 2023 08:49
-
-
Save udacityandroid/609dbb5370ba0665955a to your computer and use it in GitHub Desktop.
Android for Beginners : Add the Chocolate Topping Checkbox Solution Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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
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