15
15
*/
16
16
package com .example .android .justjava ;
17
17
18
+ import android .content .Intent ;
19
+ import android .net .Uri ;
18
20
import android .os .Bundle ;
19
- import android .support .v7 .app .ActionBarActivity ;
21
+ import android .support .v7 .app .AppCompatActivity ;
22
+ import android .text .Editable ;
20
23
import android .view .View ;
24
+ import android .widget .CheckBox ;
25
+ import android .widget .EditText ;
21
26
import android .widget .TextView ;
22
27
23
28
import java .text .NumberFormat ;
24
29
25
30
/**
26
31
* This app displays an order form to order coffee.
27
32
*/
28
- public class MainActivity extends ActionBarActivity {
33
+ public class MainActivity extends AppCompatActivity {
29
34
30
- // Number of cups of coffee ordered
31
35
int quantity = 2 ;
32
36
33
37
@ Override
@@ -40,51 +44,112 @@ protected void onCreate(Bundle savedInstanceState) {
40
44
* This method is called when the plus button is clicked.
41
45
*/
42
46
public void increment (View view ) {
47
+ if (quantity == 100 ) {
48
+ return ;
49
+ }
43
50
quantity = quantity + 1 ;
44
- display (quantity );
51
+ displayQuantity (quantity );
45
52
}
46
53
47
54
/**
48
55
* This method is called when the minus button is clicked.
49
56
*/
50
57
public void decrement (View view ) {
58
+ if (quantity == 0 ) {
59
+ return ;
60
+ }
51
61
quantity = quantity - 1 ;
52
- display (quantity );
62
+ displayQuantity (quantity );
53
63
}
54
64
55
65
/**
56
66
* This method is called when the order button is clicked.
57
67
*/
58
68
public void submitOrder (View view ) {
59
- int price = quantity * 5 ;
60
- String priceMessage = "Total: $" + price ;
61
- priceMessage = priceMessage + "\n Thank 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
+ }
63
99
}
64
100
65
101
/**
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
67
107
*/
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 ;
72
124
}
73
125
74
126
/**
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
76
134
*/
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 ;
80
145
}
81
146
82
147
/**
83
- * This method displays the given text on the screen.
148
+ * This method displays the given quantity value on the screen.
84
149
*/
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 );
88
154
}
89
-
90
155
}
0 commit comments