-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
210 lines (182 loc) · 7.12 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* IMPORTANT: Make sure you are using the correct package name.
* This example uses the package name:
* package com.example.android.justjava
* If you get an error when copying this code into Android studio, update it to match the package name found
* in the project's AndroidManifest.xml file.
**/
package com.example.android.justjava;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
//Declare and Initialise Global Vars
public int quantity = 1;
//Declare and Initialise Constants
static final int PRICE_PER_CUP = 5;
static final int CHOCOLATE_PRICE = 2;
static final int WHIPPED_CREAM_PRICE = 1;
//String constants for keys
static final String QUANTITY_KEY = "quantity";
//UI object vars
TextView quantityTextView;
CheckBox whippedCreamCheckBox;
CheckBox chocolateCheckBox;
EditText nameEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get the view objects using the view IDs
quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
nameEditText = (EditText) findViewById(R.id.name_edit_text);
updateUIValues();
}
// https://stackoverflow.com/questions/151777/saving-android-activity-state-using-save-instance-state
//prevent the application from restarting when changing orientation. Saving global vars between activity states.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putInt(QUANTITY_KEY, quantity);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
quantity = savedInstanceState.getInt(QUANTITY_KEY);
updateUIValues();
}
/**
* This method is called when the quantity increment button is clicked.
*/
public void increment(View view) {
if (quantity < 100) {
quantity = quantity + 1;
displayQuantity(quantity);
} else {
displayToastMessage(getString(R.string.max_order));
}
}
/**
* This method is called when the quantity decrement button is clicked.
*/
public void decrement(View view) {
// You can order one or more coffees!
if (quantity > 1) {
quantity = quantity - 1;
displayQuantity(quantity);
} else {
displayToastMessage(getString(R.string.min_order));
}
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
String orderMessage = prepareOrderMessage();
String subjectLine = getString(R.string.order_summary_email_subject, nameEditText.getText().toString());
composeEmail(subjectLine, orderMessage);
}
/**
* This method displays the given quantity value on the screen.
*/
public void displayQuantity(int number) {
quantityTextView.setText(String.valueOf(number));
}
/**
* This method, when called, updates all the TextViews dynamically with the current global vars values
*/
public void updateUIValues() {
displayQuantity(quantity);
}
/**
* This method starts an Activity to send email with the order
* More on https://developer.android.com/guide/components/intents-common.html#Email
* @param subject
* @param emailBody
*/
private void composeEmail(String subject, String emailBody) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, emailBody);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
/**
* Create summary of the order.
*
* @param name
* @param totalPrice
* @param hasWhippedCream is whether or not the user wants whipped cream topping
* @param hasChocolate is whether or not the user wants chocolate topping
* @return text summary
*/
private String createOrderSummary(String name, int totalPrice, boolean hasWhippedCream, boolean hasChocolate) {
String priceMessage = getString(R.string.order_summary_name, name);
priceMessage += "\n" + getString(R.string.order_summary_whipped_cream, hasWhippedCream);
priceMessage += "\n" + getString(R.string.order_summary_chocolate,hasChocolate);
priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity) ;
priceMessage += "\n" + getString(R.string.order_summary_price, NumberFormat.getCurrencyInstance().format(totalPrice));
priceMessage += "\n" + getString(R.string.thank_you);
return priceMessage;
}
/**
* Calculates the price of the order.
*
* @return total price of the order
*/
private int calculatePrice() {
int totalPricePerCup = PRICE_PER_CUP;
if (whippedCreamCheckBox.isChecked()){
totalPricePerCup += WHIPPED_CREAM_PRICE;
}
if (chocolateCheckBox.isChecked()){
totalPricePerCup += CHOCOLATE_PRICE;
}
return quantity * totalPricePerCup;
}
/**
* Gets values and calls createOrderSummary method.
*
* @return Order Summary String
*/
private String prepareOrderMessage(){
int totalPrice = calculatePrice();
String customerName = nameEditText.getText().toString();
boolean isWhippedCreamChecked = whippedCreamCheckBox.isChecked();
boolean isChocolateChecked = chocolateCheckBox.isChecked();
return createOrderSummary(customerName, totalPrice, isWhippedCreamChecked, isChocolateChecked);
}
/**
* This method displays a Toast Message when called.
*
* @param message, the message to be displayed
*
*/
//https://developer.android.com/guide/topics/ui/notifiers/toasts.html
private void displayToastMessage(String message){
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, message, duration);
toast.show();
}
}