Guest User

Untitled

a guest
Mar 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. package com.example.atnimak.coffeeorder;
  2.  
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.View;
  8. import android.widget.CheckBox;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import java.text.NumberFormat;
  14.  
  15. /**
  16. * This application makes a coffee order and sends it by email
  17. */
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20. //quantity of cups of coffee
  21. int quantity = 0;
  22. //price of one cup of coffee
  23. int price = 5;
  24. //true if ORDER button was clicked
  25. boolean orderChecked = false;
  26. //customer`s name
  27. String name = "";
  28. //total order to display and send by e-mail
  29. String totalOrder = "";
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. }
  36.  
  37. /**
  38. * This method is called when the order button is clicked.
  39. */
  40. public void submitOrder(View view) {
  41. name = "";
  42. totalOrder = "";
  43.  
  44. if (configName().trim().equals("")) {
  45. Toast.makeText(getApplicationContext(), getString(R.string.toast_havetoWriteAname), Toast.LENGTH_SHORT).show();
  46. } else {
  47. int priceWithToppings = price;
  48. name = configName();
  49. totalOrder = getString(R.string.name, name);
  50. totalOrder += getString(R.string.quantity_sum, quantity);
  51.  
  52. String toppings = "";
  53. if (isWhippedCream()) {
  54. toppings += getString(R.string.whipped_cream_sum);
  55. priceWithToppings++;
  56. }
  57.  
  58. if (isChokolate()) {
  59. if (isWhippedCream()) {
  60. toppings += getString(R.string.andChocolate_sum);
  61. priceWithToppings += 2;
  62. } else {
  63. toppings += getString(R.string.chocolate_sum);
  64. priceWithToppings += 2;
  65. }
  66. }
  67.  
  68. if (!isChokolate() && !isWhippedCream()) {
  69. toppings = getString(R.string.notoppings);
  70. }
  71.  
  72. // totalOrder += getString(R.string.total_sum, quantity * priceWithToppings);
  73. totalOrder += getString(R.string.total_sum, NumberFormat.getCurrencyInstance().format(quantity * priceWithToppings));
  74. totalOrder += getString(R.string.toppings_sum, toppings);
  75. totalOrder += getString(R.string.thankU);
  76.  
  77. /*String[] adresses = {getString(R.string.orderEmail)};
  78. composeEmail(adresses, getString(R.string.justCoffeeFor) + name, totalOrder);*/
  79. displayPrice(totalOrder);
  80. orderChecked = true;
  81. }
  82. }
  83.  
  84. /**
  85. * This method displays the given quantity value on the screen.
  86. */
  87. private void display(int number) {
  88. TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  89. quantityTextView.setText("" + number);
  90. }
  91.  
  92. /**
  93. * This method decrements quantity of cups of coffee
  94. */
  95. public void decrementCoffee(View view) {
  96. if (quantity > 1) {
  97. quantity--;
  98. } else {
  99. Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.toast_lessOne), Toast.LENGTH_SHORT);
  100. toast.show();
  101. }
  102. display(quantity);
  103. }
  104.  
  105. /**
  106. * This method displays total price
  107. */
  108. private void displayPrice(String message) {
  109. TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
  110. priceTextView.setText(message);
  111. }
  112.  
  113. /**
  114. * This method increments quantity of cups of coffee
  115. */
  116. public void incrementCoffee(View view) {
  117. if (quantity < 100) {
  118. quantity++;
  119. } else {
  120. Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.toast_more100), Toast.LENGTH_SHORT);
  121. toast.show();
  122. }
  123. display(quantity);
  124. }
  125.  
  126. /**
  127. * This method checks was the whipped cream selected or not.
  128. */
  129. public boolean isWhippedCream() {
  130. CheckBox checkBox = findViewById(R.id.whippedCream);
  131. return checkBox.isChecked();
  132. }
  133.  
  134. /**
  135. * This method checks was the chocolate selected or not.
  136. */
  137. public boolean isChokolate() {
  138. CheckBox checkBox = findViewById(R.id.chocolate);
  139. return checkBox.isChecked();
  140. }
  141.  
  142. /**
  143. * This method get the customer`s name
  144. */
  145. public String configName() {
  146. EditText editText = (EditText) findViewById(R.id.editName);
  147. String name = editText.getText().toString();
  148. return name;
  149. }
  150.  
  151. /**
  152. * This method send the e-mail intent
  153. */
  154. public void composeEmail(String[] addresses, String subject, String message) {
  155. Intent intent = new Intent(Intent.ACTION_SENDTO);
  156. intent.setData(Uri.parse("mailto:")); // only email apps should handle this
  157. intent.putExtra(Intent.EXTRA_EMAIL, addresses);
  158. intent.putExtra(Intent.EXTRA_SUBJECT, subject);
  159. intent.putExtra(Intent.EXTRA_TEXT, message);
  160. if (intent.resolveActivity(getPackageManager()) != null) {
  161. startActivity(intent);
  162. }
  163. }
  164.  
  165. /**
  166. * This method send totel order by e-mail
  167. */
  168. public void sendOrder(View view) {
  169. if (orderChecked) {
  170. orderChecked = false;
  171. String[] adresses = {getString(R.string.orderEmail)};
  172. composeEmail(adresses, getString(R.string.justCoffeeFor) + name, totalOrder);
  173. name = "";
  174. totalOrder = "";
  175. } else {
  176. Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.shoudOrder), Toast.LENGTH_SHORT);
  177. toast.show();
  178. }
  179.  
  180. }
  181. }
Add Comment
Please, Sign In to add comment