Guest User

Untitled

a guest
Apr 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. package com.example.android.justjava;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.CheckBox;
  9. import android.widget.Checkable;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import java.lang.ref.SoftReference;
  15. import java.text.NumberFormat;
  16. /** * This app displays an order form to order coffee. */
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20. int quantity = 0;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. }
  27.  
  28. /** * This method is called when the order button is clicked. */
  29. public void addOrder(View view) {
  30. if (quantity == 100) {
  31. // Show an error message as a toast
  32. Toast.makeText (this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show ();
  33. // Exit this method early because there's nothing left to do
  34. return;
  35. }
  36. quantity = quantity + 1;
  37. displayQuantity(quantity);
  38. }
  39. public void subtractOrder(View view) {
  40. if (quantity == 1) {
  41. // Show an error message as a toast
  42. Toast.makeText (this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show ();
  43. // Exit this method early because there's nothing left to do
  44. return;
  45. }
  46. quantity = quantity - 1;
  47. displayQuantity(quantity);
  48. }
  49.  
  50. /**
  51. *
  52. * This method is called when the order button is clicked.
  53. */
  54.  
  55. public void submitOrder(View view) {
  56. //Find the user's name
  57. EditText nameField = (EditText)findViewById (R.id.name_field);
  58. String name = nameField.getText().toString ();
  59.  
  60. // Figure out if the user wants whipped cream topping
  61. CheckBox whippedCreamCheckBox = (CheckBox) findViewById (R.id.whipped_cream_checkbox);
  62. boolean hasWhippedCream = whippedCreamCheckBox.isChecked ();
  63.  
  64. //Figure out is the user wants chocolate topping
  65. CheckBox chocolateCheckBox = (CheckBox) findViewById (R.id.chocolate_checkbox);
  66. boolean hasChocolate = chocolateCheckBox.isChecked ();
  67.  
  68. int price = calculatePrice (hasWhippedCream, hasChocolate);
  69. String priceMessage = createOrderSummary (name, price, hasWhippedCream, hasChocolate);
  70.  
  71. Intent intent = new Intent(Intent.ACTION_SENDTO);
  72. intent.setData(Uri.parse("mailto:")); // only email apps should handle this
  73. intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + name);
  74. intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
  75. if (intent.resolveActivity(getPackageManager()) != null) {
  76. startActivity(intent);
  77. }
  78.  
  79. }
  80.  
  81. /**
  82. * Calculates the price of the order.*
  83. * @param addWhippedCream is whether or not the user wants whipped cream topping
  84. * @param addChocolate is whether or not the user wants chocolate topping
  85. * @return total price
  86. */
  87. private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
  88. //Price of 1 cup of coffee
  89. int basePrice = 5;
  90. //Add $1 if the user wants whipped cream
  91. if (addWhippedCream) {
  92. basePrice = basePrice + 1;
  93. }
  94. //Add $2 if the user wants chocolate
  95. if (addChocolate) {
  96. basePrice = basePrice +2;
  97. }
  98. //Calculate the total order price by multiplying by quantity
  99. return quantity * basePrice;
  100. }
  101.  
  102. /**
  103. *
  104. * @param price of the order
  105. * @param addWhippedCream is whether or not the user wants whipped cream topping
  106. * @param addChocolate is whether or not the user wants chocolate topping
  107. * @return text summary
  108. */
  109. private String createOrderSummary(String name, int price, boolean addWhippedCream, boolean addChocolate) {
  110. String priceMessage = getString (R.string.order_summary_name, name);
  111. priceMessage = priceMessage + "\nAdd Whipped Cream? " + addWhippedCream;
  112. priceMessage += "\nAdd Chocolate? " + addChocolate;
  113. priceMessage = priceMessage + "\nQuantity: " + quantity;
  114. priceMessage = priceMessage + "\nTotal: $" + price;
  115. priceMessage = priceMessage + "\n" + getString(R.string.thank_you);
  116. return priceMessage;
  117. }
  118. /**
  119. * This method
  120. * displays the given quantity value on the screen.
  121. */
  122. private void displayQuantity(int number) {
  123. TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  124. quantityTextView.setText("" + number);
  125. }
  126.  
  127. }
Add Comment
Please, Sign In to add comment