-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathOrderActivity.java
More file actions
307 lines (252 loc) · 11 KB
/
OrderActivity.java
File metadata and controls
307 lines (252 loc) · 11 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (C) 2017 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.
*/
package com.example.android.teatime;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.text.NumberFormat;
public class OrderActivity extends AppCompatActivity {
private int mQuantity = 0;
private int mTotalPrice = 0;
private static final int SMALL_PRICE = 5;
private static final int MEDIUM_PRICE = 6;
private static final int LARGE_PRICE = 7;
private static final String TEA_SIZE_SMALL = "Small ($5/cup)";
private static final String TEA_SIZE_MEDIUM = "Medium ($6/cup)";
private static final String TEA_SIZE_LARGE = "Large ($7/cup)";
private String mMilkType;
private String mSugarType;
private String mTeaName = "";
private String mSize;
public final static String EXTRA_TOTAL_PRICE = "com.example.android.teatime.EXTRA_TOTAL_PRICE";
public final static String EXTRA_TEA_NAME = "com.example.android.teatime.EXTRA_TEA_NAME";
public final static String EXTRA_SIZE = "com.example.android.teatime.EXTRA_SIZE";
public final static String EXTRA_MILK_TYPE = "com.example.android.teatime.EXTRA_MILK_TYPE";
public final static String EXTRA_SUGAR_TYPE = "com.example.android.teatime.EXTRA_SUGAR_TYPE";
public final static String EXTRA_QUANTITY = "com.example.android.teatime.EXTRA_QUANTITY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Toolbar menuToolbar = (Toolbar) findViewById(R.id.order_toolbar);
setSupportActionBar(menuToolbar);
getSupportActionBar().setTitle(getString(R.string.order_title));
// Set header name and image depending on which item was clicked in the gridView
Intent intent = getIntent();
mTeaName = intent.getStringExtra(MenuActivity.EXTRA_TEA_NAME);
TextView teaNameTextView = (TextView) findViewById(R.id.tea_name_text_view);
teaNameTextView.setText(mTeaName);
// Set cost default to $0.00
TextView costTextView = (TextView) findViewById(
R.id.cost_text_view);
costTextView.setText(getString(R.string.initial_cost));
setupSizeSpinner();
setupMilkSpinner();
setupSugarSpinner();
}
/**
* Sets up the dropdown spinner for user to select tea mSize
*/
private void setupSizeSpinner() {
Spinner mSizeSpinner = (Spinner) findViewById(R.id.tea_size_spinner);
// Create an ArrayAdapter using the string array and a default mSizeSpinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.tea_size_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the mSizeSpinner
mSizeSpinner.setAdapter(adapter);
// Set the integer mSelected to the constant values
mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
mSize = getString(R.string.tea_size_small);
break;
case 1:
mSize = getString(R.string.tea_size_medium);
break;
case 2:
mSize = getString(R.string.tea_size_large);
break;
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
@Override
public void onNothingSelected(AdapterView<?> parent) {
mSize = getString(R.string.tea_size_small);
}
});
}
/**
* Sets up the dropdown spinner for user to select milk type
*/
private void setupMilkSpinner() {
Spinner mSizeSpinner = (Spinner) findViewById(R.id.milk_spinner);
// Create an ArrayAdapter using the string array and a default mSizeSpinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.milk_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the mSizeSpinner
mSizeSpinner.setAdapter(adapter);
// Set the integer mSelected to the constant values
mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
mMilkType = getString(R.string.milk_type_none);
break;
case 1:
mMilkType = getString(R.string.milk_type_nonfat);
break;
case 2:
mMilkType = getString(R.string.milk_type_1_percent);
break;
case 3:
mMilkType = getString(R.string.milk_type_2_percent);
break;
case 4:
mMilkType = getString(R.string.milk_type_whole);
break;
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
@Override
public void onNothingSelected(AdapterView<?> parent) {
mMilkType = getString(R.string.milk_type_none);
}
});
}
/**
* Setup the dropdown spinner for user to select amount of sugar
*/
private void setupSugarSpinner() {
Spinner mSizeSpinner = (Spinner) findViewById(R.id.sugar_spinner);
// Create an ArrayAdapter using the string array and a default mSizeSpinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.sugar_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the mSizeSpinner
mSizeSpinner.setAdapter(adapter);
// Set the integer mSelected to the constant values
mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
mSugarType = getString(R.string.sweet_type_0);
break;
case 1:
mSugarType = getString(R.string.sweet_type_25);
break;
case 2:
mSugarType = getString(R.string.sweet_type_50);
break;
case 3:
mSugarType = getString(R.string.sweet_type_75);
break;
case 4:
mSugarType = getString(R.string.sweet_type_100);
break;
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
@Override
public void onNothingSelected(AdapterView<?> parent) {
mSugarType = getString(R.string.sweet_type_100);
}
});
}
/**
* Increments the quantity and recalculates the price
*/
public void increment(View view) {
mQuantity = mQuantity + 1;
displayQuantity(mQuantity);
mTotalPrice = calculatePrice();
displayCost(mTotalPrice);
}
/**
* Decrements the quantity and recalculates the price
*/
public void decrement(View view) {
if (mQuantity > 0) {
mQuantity = mQuantity - 1;
displayQuantity(mQuantity);
mTotalPrice = calculatePrice();
displayCost(mTotalPrice);
}
}
/**
* Calculates the TotalPrice of the order.
*
* @return total mTotalPrice
*/
private int calculatePrice() {
// Calculate the total order mTotalPrice by multiplying by the mQuantity
switch (mSize) {
case TEA_SIZE_SMALL:
mTotalPrice = mQuantity * SMALL_PRICE;
break;
case TEA_SIZE_MEDIUM:
mTotalPrice = mQuantity * MEDIUM_PRICE;
break;
case TEA_SIZE_LARGE:
mTotalPrice = mQuantity * LARGE_PRICE;
break;
}
return mTotalPrice;
}
/**
* Displays the given mQuantity value on the screen then
* calculates and displays the cost
*/
private void displayQuantity(int numberOfTeas) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(numberOfTeas));
}
private void displayCost(int totalPrice) {
TextView costTextView = (TextView) findViewById(
R.id.cost_text_view);
String convertPrice = NumberFormat.getCurrencyInstance().format(totalPrice);
costTextView.setText(convertPrice);
}
/**
* This method is called when the "Brew Tea" button is clicked
* and a new intent opens the the {@link OrderSummaryActivity}
*/
public void brewTea(View view) {
// Create a new intent to open the {@link OrderSummaryActivity}
Intent intent = new Intent(OrderActivity.this, OrderSummaryActivity.class);
intent.putExtra(EXTRA_TOTAL_PRICE, mTotalPrice);
intent.putExtra(EXTRA_TEA_NAME, mTeaName);
intent.putExtra(EXTRA_SIZE, mSize);
intent.putExtra(EXTRA_MILK_TYPE, mMilkType);
intent.putExtra(EXTRA_SUGAR_TYPE, mSugarType);
intent.putExtra(EXTRA_QUANTITY, mQuantity);
startActivity(intent);
}
}