Guest User

Untitled

a guest
Apr 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. // MainActivity.java
  2.  
  3. package com.example.android.justjava;
  4.  
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.text.Editable;
  10. import android.view.View;
  11. import android.widget.CheckBox;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. import java.text.NumberFormat;
  16.  
  17. /**
  18. * This app displays an order form to order coffee.
  19. */
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22. int quantity = 2;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. }
  29.  
  30. /**
  31. * This method is called when the plus button is clicked.
  32. */
  33. public void increment(View view) {
  34. if (quantity == 100) {
  35. return;
  36. }
  37. quantity = quantity + 1;
  38. displayQuantity(quantity);
  39. }
  40.  
  41. /**
  42. * This method is called when the minus button is clicked.
  43. */
  44. public void decrement(View view) {
  45. if (quantity == 0) {
  46. return;
  47. }
  48. quantity = quantity - 1;
  49. displayQuantity(quantity);
  50. }
  51.  
  52. /**
  53. * This method is called when the order button is clicked.
  54. */
  55. public void submitOrder(View view) {
  56. // Get user's name
  57. EditText nameField = (EditText) findViewById(R.id.name_field);
  58. Editable nameEditable = nameField.getText();
  59. String name = nameEditable.toString();
  60.  
  61. // Figure out if the user wants whipped cream topping
  62. CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
  63. boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
  64.  
  65. // Figure out if the user wants choclate topping
  66. CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
  67. boolean hasChocolate = chocolateCheckBox.isChecked();
  68.  
  69. // Calculate the price
  70. int price = calculatePrice(hasWhippedCream, hasChocolate);
  71.  
  72. // Display the order summary on the screen
  73. String message = createOrderSummary(name, price, hasWhippedCream, hasChocolate);
  74.  
  75. // Use an intent to launch an email app.
  76. // Send the order summary in the email body.
  77. Intent intent = new Intent(Intent.ACTION_SENDTO);
  78. intent.setData(Uri.parse("mailto:")); // only email apps should handle this
  79. intent.putExtra(Intent.EXTRA_SUBJECT,
  80. getString(R.string.order_summary_email_subject, name));
  81. intent.putExtra(Intent.EXTRA_TEXT, message);
  82.  
  83. if (intent.resolveActivity(getPackageManager()) != null) {
  84. startActivity(intent);
  85. }
  86. }
  87.  
  88. /**
  89. * Calculates the price of the order.
  90. *
  91. * @param addWhippedCream is whether or not we should include whipped cream topping in the price
  92. * @param addChocolate is whether or not we should include chocolate topping in the price
  93. * @return total price
  94. */
  95. private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
  96. // First calculate the price of one cup of coffee
  97. int basePrice = 5;
  98.  
  99. // If the user wants whipped cream, add $1 per cup
  100. if (addWhippedCream) {
  101. basePrice = basePrice + 1;
  102. }
  103.  
  104. // If the user wants chocolate, add $2 per cup
  105. if (addChocolate) {
  106. basePrice = basePrice + 2;
  107. }
  108.  
  109. // Calculate the total order price by multiplying by the quantity
  110. return quantity * basePrice;
  111. }
  112.  
  113. /**
  114. * Create summary of the order.
  115. *
  116. * @param name on the order
  117. * @param price of the order
  118. * @param addWhippedCream is whether or not to add whipped cream to the coffee
  119. * @param addChocolate is whether or not to add chocolate to the coffee
  120. * @return text summary
  121. */
  122. private String createOrderSummary(String name, int price, boolean addWhippedCream,
  123. boolean addChocolate) {
  124. String priceMessage = getString(R.string.order_summary_name, name);
  125. priceMessage += "\n" + getString(R.string.order_summary_whipped_cream, addWhippedCream);
  126. priceMessage += "\n" + getString(R.string.order_summary_chocolate, addChocolate);
  127. priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity);
  128. priceMessage += "\n" + getString(R.string.order_summary_price,
  129. NumberFormat.getCurrencyInstance().format(price));
  130. priceMessage += "\n" + getString(R.string.thank_you);
  131. return priceMessage;
  132. }
  133.  
  134. /**
  135. * This method displays the given quantity value on the screen.
  136. */
  137. private void displayQuantity(int numberOfCoffees) {
  138. TextView quantityTextView = (TextView) findViewById(
  139. R.id.quantity_text_view);
  140. quantityTextView.setText("" + numberOfCoffees);
  141. }
  142. }
Add Comment
Please, Sign In to add comment