Skip to content

Instantly share code, notes, and snippets.

@eeprevost
Forked from udacityandroid/MainActivity.java
Created July 10, 2017 20:24
Show Gist options
  • Select an option

  • Save eeprevost/5f6aa38d15f007024544e59e502f2693 to your computer and use it in GitHub Desktop.

Select an option

Save eeprevost/5f6aa38d15f007024544e59e502f2693 to your computer and use it in GitHub Desktop.

Revisions

  1. udacityandroid revised this gist Jun 25, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    package com.example.android.justjava;


    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
  2. udacityandroid created this gist Jun 17, 2015.
    103 changes: 103 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    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);
    }

    }