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 whipped cream 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); } }