Skip to content

Commit

Permalink
Final state of app at end of Lesson 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Jul 8, 2015
1 parent ee20086 commit d60ff1e
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 82 deletions.
111 changes: 88 additions & 23 deletions app/src/main/java/com/example/android/justjava/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
*/
package com.example.android.justjava;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;

/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends ActionBarActivity {
public class MainActivity extends AppCompatActivity {

// Number of cups of coffee ordered
int quantity = 2;

@Override
Expand All @@ -40,51 +44,112 @@ protected void onCreate(Bundle savedInstanceState) {
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
if (quantity == 100) {
return;
}
quantity = quantity + 1;
display(quantity);
displayQuantity(quantity);
}

/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
if (quantity == 0) {
return;
}
quantity = quantity - 1;
display(quantity);
displayQuantity(quantity);
}

/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
int price = quantity * 5;
String priceMessage = "Total: $" + price;
priceMessage = priceMessage + "\nThank you!";
displayMessage(priceMessage);
// Get user's name
EditText nameField = (EditText) findViewById(R.id.name_field);
Editable nameEditable = nameField.getText();
String name = nameEditable.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 whipped cream topping
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
boolean hasChocolate = chocolateCheckBox.isChecked();

// Calculate the price
int price = calculatePrice(hasWhippedCream, hasChocolate);

// Display the order summary on the screen
String message = createOrderSummary(name, price, hasWhippedCream, hasChocolate);

// Use an intent to launch an email app.
// Send the order summary in the email body.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT,
getString(R.string.order_summary_email_subject, name));
intent.putExtra(Intent.EXTRA_TEXT, message);

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

/**
* This method displays the given quantity value on the screen.
* Calculates the price of the order.
*
* @param addWhippedCream is whether or not we should include whipped cream topping in the price
* @param addChocolate is whether or not we should include whipped cream topping in the price
* @return total price
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
// First calculate the price of one cup of coffee
int basePrice = 5;

// If the user wants whipped cream, add $1 per cup
if (addWhippedCream) {
basePrice = basePrice + 1;
}

// If the user wants chocolate, add $2 per cup
if (addChocolate) {
basePrice = basePrice + 2;
}

// Calculate the total order price by multiplying by the quantity
return quantity * basePrice;
}

/**
* This method displays the given price value on the screen.
* Create summary of the order.
*
* @param name on 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 void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
private String createOrderSummary(String name, int price, boolean addWhippedCream,
boolean addChocolate) {
String priceMessage = getString(R.string.order_summary_name, name);
priceMessage += "\n" + getString(R.string.order_summary_whipped_cream, addWhippedCream);
priceMessage += "\n" + getString(R.string.order_summary_chocolate, addChocolate);
priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity);
priceMessage += "\n" + getString(R.string.order_summary_price,
NumberFormat.getCurrencyInstance().format(price));
priceMessage += "\n" + getString(R.string.thank_you);
return priceMessage;
}

/**
* This method displays the given text on the screen.
* This method displays the given quantity value on the screen.
*/
private void displayMessage(String message) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(message);
private void displayQuantity(int numberOfCoffees) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + numberOfCoffees);
}

}
121 changes: 67 additions & 54 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +12,87 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Quantity"
android:textAllCaps="true" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-" />
<EditText
android:id="@+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:inputType="text" />

<TextView
android:id="@+id/quantity_text_view"
style="@style/HeaderTextStyle"
android:text="@string/toppings" />

<CheckBox
android:id="@+id/whipped_cream_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
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:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="2"
android:textColor="@android:color/black"
android:paddingLeft="24dp"
android:text="@string/chocolate"
android:textSize="16sp" />

<TextView
style="@style/HeaderTextStyle"
android:text="@string/quantity" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-" />

<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/initial_quantity_value"
android:textColor="@android:color/black"
android:textSize="16sp" />

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />

</LinearLayout>

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="@string/order" />

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Price"
android:textAllCaps="true" />

<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="$10"
android:textColor="@android:color/black"
android:textSize="16sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="Order" />

</LinearLayout>
</ScrollView>
78 changes: 78 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!-- Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Title for the application. [CHAR LIMIT=12] -->
<string name="app_name">Sólo Java</string>

<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="name">Nombre</string>

<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="toppings">Ingredientes</string>

<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="whipped_cream">Crema batida</string>

<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="chocolate">Chocolate</string>

<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="quantity">Cantidad</string>

<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=5] -->
<string name="initial_quantity_value">2</string>

<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="order">Ordenar</string>

<!--
Name for the order summary. It will be shown in the format of "Name: Amy" where Amy is the
user's name. [CHAR LIMIT=NONE]
-->
<string name="order_summary_name">Nombre: <xliff:g id="name" example="Amy">%s</xliff:g></string>

<!--
Whipped cream topping for the order summary. It will be shown in the format of
"Add whipped cream? true" or "Add whipped cream? false". [CHAR LIMIT=NONE]
-->
<string name="order_summary_whipped_cream">Agregue la crema batida? <xliff:g id="addWhippedCream" example="true">%b</xliff:g></string>

<!--
Chocolate topping for the order summary. It will be shown in the format of
"Add chocolate? true" or "Add chocolate? false". [CHAR LIMIT=NONE]
-->
<string name="order_summary_chocolate">Agregue el chocolate? <xliff:g id="addChocolate" example="true">%b</xliff:g></string>

<!--
Quantity of coffee cups for the order summary. It will be shown in the format of
"Quantity: 2", where 2 is the number of cups ordered. [CHAR LIMIT=NONE]
-->
<string name="order_summary_quantity">Cantidad: <xliff:g id="quantity" example="2">%d</xliff:g></string>

<!--
Total price for the order summary. It will be shown in the format of
"Total: $10" where $10 is the price. [CHAR LIMIT=NONE]
-->
<string name="order_summary_price">Total: <xliff:g id="price" example="$10">%s</xliff:g></string>

<!-- Thank you message for the order summary. [CHAR LIMIT=NONE] -->
<string name="thank_you">¡Gracias!</string>

<!--
Subject line for the order summary email. It will be in the format of
"Just Java order for Amy" where Amy is the user's name. [CHAR LIMIT=NONE]
-->
<string name="order_summary_email_subject">Sólo java para <xliff:g id="name" example="Amy">%s</xliff:g></string>
</resources>
Loading

0 comments on commit d60ff1e

Please sign in to comment.