Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.justjava;
- import android.os.Bundle;
- import android.renderscript.Sampler;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.TextureView;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.example.android.justjava.R;
- /**
- * 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) {
- EditText nameField = (EditText)findViewById(R.id.name_field);
- String name = nameField.getText().toString();
- // 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(name, price, hasWhippedCream, hasChocolate);
- displayMessage(message);
- }
- /**
- * Calculates the price of the order.
- *
- * @return total price
- */
- private int calculatePrice() {
- return quantity * 10;
- }
- /**
- * Create summary of the order.
- *
- * @param name of the customer
- * @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(String name, int price, boolean addWhippedCream, boolean addChocolate) {
- String priceMessage = "Name: " + name;
- 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_veiw);
- 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);
- }
- }
Add Comment
Please, Sign In to add comment