Skip to content

Commit d60ff1e

Browse files
author
Lyla
committed
Final state of app at end of Lesson 3
1 parent ee20086 commit d60ff1e

File tree

5 files changed

+313
-82
lines changed

5 files changed

+313
-82
lines changed

app/src/main/java/com/example/android/justjava/MainActivity.java

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515
*/
1616
package com.example.android.justjava;
1717

18+
import android.content.Intent;
19+
import android.net.Uri;
1820
import android.os.Bundle;
19-
import android.support.v7.app.ActionBarActivity;
21+
import android.support.v7.app.AppCompatActivity;
22+
import android.text.Editable;
2023
import android.view.View;
24+
import android.widget.CheckBox;
25+
import android.widget.EditText;
2126
import android.widget.TextView;
2227

2328
import java.text.NumberFormat;
2429

2530
/**
2631
* This app displays an order form to order coffee.
2732
*/
28-
public class MainActivity extends ActionBarActivity {
33+
public class MainActivity extends AppCompatActivity {
2934

30-
// Number of cups of coffee ordered
3135
int quantity = 2;
3236

3337
@Override
@@ -40,51 +44,112 @@ protected void onCreate(Bundle savedInstanceState) {
4044
* This method is called when the plus button is clicked.
4145
*/
4246
public void increment(View view) {
47+
if (quantity == 100) {
48+
return;
49+
}
4350
quantity = quantity + 1;
44-
display(quantity);
51+
displayQuantity(quantity);
4552
}
4653

4754
/**
4855
* This method is called when the minus button is clicked.
4956
*/
5057
public void decrement(View view) {
58+
if (quantity == 0) {
59+
return;
60+
}
5161
quantity = quantity - 1;
52-
display(quantity);
62+
displayQuantity(quantity);
5363
}
5464

5565
/**
5666
* This method is called when the order button is clicked.
5767
*/
5868
public void submitOrder(View view) {
59-
int price = quantity * 5;
60-
String priceMessage = "Total: $" + price;
61-
priceMessage = priceMessage + "\nThank you!";
62-
displayMessage(priceMessage);
69+
// Get user's name
70+
EditText nameField = (EditText) findViewById(R.id.name_field);
71+
Editable nameEditable = nameField.getText();
72+
String name = nameEditable.toString();
73+
74+
// Figure out if the user wants whipped cream topping
75+
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
76+
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
77+
78+
// Figure out if the user wants whipped cream topping
79+
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
80+
boolean hasChocolate = chocolateCheckBox.isChecked();
81+
82+
// Calculate the price
83+
int price = calculatePrice(hasWhippedCream, hasChocolate);
84+
85+
// Display the order summary on the screen
86+
String message = createOrderSummary(name, price, hasWhippedCream, hasChocolate);
87+
88+
// Use an intent to launch an email app.
89+
// Send the order summary in the email body.
90+
Intent intent = new Intent(Intent.ACTION_SENDTO);
91+
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
92+
intent.putExtra(Intent.EXTRA_SUBJECT,
93+
getString(R.string.order_summary_email_subject, name));
94+
intent.putExtra(Intent.EXTRA_TEXT, message);
95+
96+
if (intent.resolveActivity(getPackageManager()) != null) {
97+
startActivity(intent);
98+
}
6399
}
64100

65101
/**
66-
* This method displays the given quantity value on the screen.
102+
* Calculates the price of the order.
103+
*
104+
* @param addWhippedCream is whether or not we should include whipped cream topping in the price
105+
* @param addChocolate is whether or not we should include whipped cream topping in the price
106+
* @return total price
67107
*/
68-
private void display(int number) {
69-
TextView quantityTextView = (TextView) findViewById(
70-
R.id.quantity_text_view);
71-
quantityTextView.setText("" + number);
108+
private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
109+
// First calculate the price of one cup of coffee
110+
int basePrice = 5;
111+
112+
// If the user wants whipped cream, add $1 per cup
113+
if (addWhippedCream) {
114+
basePrice = basePrice + 1;
115+
}
116+
117+
// If the user wants chocolate, add $2 per cup
118+
if (addChocolate) {
119+
basePrice = basePrice + 2;
120+
}
121+
122+
// Calculate the total order price by multiplying by the quantity
123+
return quantity * basePrice;
72124
}
73125

74126
/**
75-
* This method displays the given price value on the screen.
127+
* Create summary of the order.
128+
*
129+
* @param name on the order
130+
* @param price of the order
131+
* @param addWhippedCream is whether or not to add whipped cream to the coffee
132+
* @param addChocolate is whether or not to add chocolate to the coffee
133+
* @return text summary
76134
*/
77-
private void displayPrice(int number) {
78-
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
79-
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
135+
private String createOrderSummary(String name, int price, boolean addWhippedCream,
136+
boolean addChocolate) {
137+
String priceMessage = getString(R.string.order_summary_name, name);
138+
priceMessage += "\n" + getString(R.string.order_summary_whipped_cream, addWhippedCream);
139+
priceMessage += "\n" + getString(R.string.order_summary_chocolate, addChocolate);
140+
priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity);
141+
priceMessage += "\n" + getString(R.string.order_summary_price,
142+
NumberFormat.getCurrencyInstance().format(price));
143+
priceMessage += "\n" + getString(R.string.thank_you);
144+
return priceMessage;
80145
}
81146

82147
/**
83-
* This method displays the given text on the screen.
148+
* This method displays the given quantity value on the screen.
84149
*/
85-
private void displayMessage(String message) {
86-
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
87-
priceTextView.setText(message);
150+
private void displayQuantity(int numberOfCoffees) {
151+
TextView quantityTextView = (TextView) findViewById(
152+
R.id.quantity_text_view);
153+
quantityTextView.setText("" + numberOfCoffees);
88154
}
89-
90155
}

app/src/main/res/layout/activity_main.xml

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,74 +12,87 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
-->
15-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
15+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
1616
xmlns:tools="http://schemas.android.com/tools"
1717
android:layout_width="match_parent"
1818
android:layout_height="match_parent"
19-
android:orientation="vertical"
20-
android:paddingBottom="@dimen/activity_vertical_margin"
21-
android:paddingLeft="@dimen/activity_horizontal_margin"
22-
android:paddingRight="@dimen/activity_horizontal_margin"
23-
android:paddingTop="@dimen/activity_vertical_margin"
2419
tools:context=".MainActivity">
2520

26-
<TextView
27-
android:layout_width="wrap_content"
28-
android:layout_height="wrap_content"
29-
android:layout_marginBottom="16dp"
30-
android:text="Quantity"
31-
android:textAllCaps="true" />
32-
3321
<LinearLayout
3422
android:layout_width="match_parent"
35-
android:layout_height="wrap_content"
36-
android:orientation="horizontal">
23+
android:layout_height="match_parent"
24+
android:orientation="vertical"
25+
android:paddingBottom="@dimen/activity_vertical_margin"
26+
android:paddingLeft="@dimen/activity_horizontal_margin"
27+
android:paddingRight="@dimen/activity_horizontal_margin"
28+
android:paddingTop="@dimen/activity_vertical_margin">
3729

38-
<Button
39-
android:layout_width="48dp"
40-
android:layout_height="48dp"
41-
android:onClick="decrement"
42-
android:text="-" />
30+
<EditText
31+
android:id="@+id/name_field"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
android:hint="@string/name"
35+
android:inputType="text" />
4336

4437
<TextView
45-
android:id="@+id/quantity_text_view"
38+
style="@style/HeaderTextStyle"
39+
android:text="@string/toppings" />
40+
41+
<CheckBox
42+
android:id="@+id/whipped_cream_checkbox"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:paddingLeft="24dp"
46+
android:text="@string/whipped_cream"
47+
android:textSize="16sp" />
48+
49+
<CheckBox
50+
android:id="@+id/chocolate_checkbox"
4651
android:layout_width="wrap_content"
4752
android:layout_height="wrap_content"
48-
android:paddingLeft="8dp"
49-
android:paddingRight="8dp"
50-
android:text="2"
51-
android:textColor="@android:color/black"
53+
android:paddingLeft="24dp"
54+
android:text="@string/chocolate"
5255
android:textSize="16sp" />
5356

57+
<TextView
58+
style="@style/HeaderTextStyle"
59+
android:text="@string/quantity" />
60+
61+
<LinearLayout
62+
android:layout_width="match_parent"
63+
android:layout_height="wrap_content"
64+
android:orientation="horizontal">
65+
66+
<Button
67+
android:layout_width="48dp"
68+
android:layout_height="48dp"
69+
android:onClick="decrement"
70+
android:text="-" />
71+
72+
<TextView
73+
android:id="@+id/quantity_text_view"
74+
android:layout_width="wrap_content"
75+
android:layout_height="wrap_content"
76+
android:paddingLeft="8dp"
77+
android:paddingRight="8dp"
78+
android:text="@string/initial_quantity_value"
79+
android:textColor="@android:color/black"
80+
android:textSize="16sp" />
81+
82+
<Button
83+
android:layout_width="48dp"
84+
android:layout_height="48dp"
85+
android:onClick="increment"
86+
android:text="+" />
87+
88+
</LinearLayout>
89+
5490
<Button
55-
android:layout_width="48dp"
56-
android:layout_height="48dp"
57-
android:onClick="increment"
58-
android:text="+" />
91+
android:layout_width="wrap_content"
92+
android:layout_height="wrap_content"
93+
android:layout_marginTop="16dp"
94+
android:onClick="submitOrder"
95+
android:text="@string/order" />
5996

6097
</LinearLayout>
61-
62-
<TextView
63-
android:layout_width="wrap_content"
64-
android:layout_height="wrap_content"
65-
android:layout_marginTop="16dp"
66-
android:text="Price"
67-
android:textAllCaps="true" />
68-
69-
<TextView
70-
android:id="@+id/price_text_view"
71-
android:layout_width="wrap_content"
72-
android:layout_height="wrap_content"
73-
android:layout_marginTop="16dp"
74-
android:text="$10"
75-
android:textColor="@android:color/black"
76-
android:textSize="16sp" />
77-
78-
<Button
79-
android:layout_width="wrap_content"
80-
android:layout_height="wrap_content"
81-
android:layout_marginTop="16dp"
82-
android:onClick="submitOrder"
83-
android:text="Order" />
84-
85-
</LinearLayout>
98+
</ScrollView>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!-- Copyright (C) 2015 The Android Open Source Project
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
16+
<!-- Title for the application. [CHAR LIMIT=12] -->
17+
<string name="app_name">Sólo Java</string>
18+
19+
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
20+
<string name="name">Nombre</string>
21+
22+
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
23+
<string name="toppings">Ingredientes</string>
24+
25+
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
26+
<string name="whipped_cream">Crema batida</string>
27+
28+
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
29+
<string name="chocolate">Chocolate</string>
30+
31+
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
32+
<string name="quantity">Cantidad</string>
33+
34+
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=5] -->
35+
<string name="initial_quantity_value">2</string>
36+
37+
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
38+
<string name="order">Ordenar</string>
39+
40+
<!--
41+
Name for the order summary. It will be shown in the format of "Name: Amy" where Amy is the
42+
user's name. [CHAR LIMIT=NONE]
43+
-->
44+
<string name="order_summary_name">Nombre: <xliff:g id="name" example="Amy">%s</xliff:g></string>
45+
46+
<!--
47+
Whipped cream topping for the order summary. It will be shown in the format of
48+
"Add whipped cream? true" or "Add whipped cream? false". [CHAR LIMIT=NONE]
49+
-->
50+
<string name="order_summary_whipped_cream">Agregue la crema batida? <xliff:g id="addWhippedCream" example="true">%b</xliff:g></string>
51+
52+
<!--
53+
Chocolate topping for the order summary. It will be shown in the format of
54+
"Add chocolate? true" or "Add chocolate? false". [CHAR LIMIT=NONE]
55+
-->
56+
<string name="order_summary_chocolate">Agregue el chocolate? <xliff:g id="addChocolate" example="true">%b</xliff:g></string>
57+
58+
<!--
59+
Quantity of coffee cups for the order summary. It will be shown in the format of
60+
"Quantity: 2", where 2 is the number of cups ordered. [CHAR LIMIT=NONE]
61+
-->
62+
<string name="order_summary_quantity">Cantidad: <xliff:g id="quantity" example="2">%d</xliff:g></string>
63+
64+
<!--
65+
Total price for the order summary. It will be shown in the format of
66+
"Total: $10" where $10 is the price. [CHAR LIMIT=NONE]
67+
-->
68+
<string name="order_summary_price">Total: <xliff:g id="price" example="$10">%s</xliff:g></string>
69+
70+
<!-- Thank you message for the order summary. [CHAR LIMIT=NONE] -->
71+
<string name="thank_you">¡Gracias!</string>
72+
73+
<!--
74+
Subject line for the order summary email. It will be in the format of
75+
"Just Java order for Amy" where Amy is the user's name. [CHAR LIMIT=NONE]
76+
-->
77+
<string name="order_summary_email_subject">Sólo java para <xliff:g id="name" example="Amy">%s</xliff:g></string>
78+
</resources>

0 commit comments

Comments
 (0)