Guest User

Untitled

a guest
Apr 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. package com.example.android.justjava;
  2.  
  3. import android.os.Bundle;
  4. import android.renderscript.Sampler;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;
  7. import android.view.TextureView;
  8. import android.view.View;
  9. import android.widget.CheckBox;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import com.example.android.justjava.R;
  14.  
  15.  
  16. /**
  17. * This app displays an order form to order coffee.
  18. */
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21. int quantity = 2;
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. }
  28.  
  29. /**
  30. * This method is called when the plus button is clicked.
  31. */
  32. public void increment(View view) {
  33. quantity = quantity + 1;
  34. displayQuantity(quantity);
  35. }
  36.  
  37. /**
  38. * This method is called when the minus button is clicked.
  39. */
  40. public void decrement(View view) {
  41. quantity = quantity - 1;
  42. displayQuantity(quantity);
  43. }
  44.  
  45. /**
  46. * This method is called when the order button is clicked.
  47. */
  48. public void submitOrder(View view) {
  49.  
  50. EditText nameField = (EditText)findViewById(R.id.name_field);
  51. String name = nameField.getText().toString();
  52.  
  53. // Figure out if the user wants whipped cream topping
  54. CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
  55. boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
  56.  
  57. // Figure out if the user wants chocolate topping
  58. CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.Chocolate_checkbox);
  59. boolean hasChocolate = chocolateCheckBox.isChecked();
  60.  
  61. // Calculate the price
  62. int price = calculatePrice();
  63.  
  64. // Display the order summary on the screen
  65. String message = createOrderSummary(name, price, hasWhippedCream, hasChocolate);
  66. displayMessage(message);
  67. }
  68.  
  69. /**
  70. * Calculates the price of the order.
  71. *
  72. * @return total price
  73. */
  74. private int calculatePrice() {
  75. return quantity * 10;
  76. }
  77.  
  78. /**
  79. * Create summary of the order.
  80. *
  81. * @param name of the customer
  82. * @param price of the order
  83. * @param addWhippedCream is whether or not to add whipped cream to the coffee
  84. * @param addChocolate is whether or not to add chocolate to the coffee
  85. * @return text summary
  86. */
  87. private String createOrderSummary(String name, int price, boolean addWhippedCream, boolean addChocolate) {
  88. String priceMessage = "Name: " + name;
  89. priceMessage += "\nAdd whipped cream? " + addWhippedCream;
  90. priceMessage += "\nAdd chocolate? " + addChocolate;
  91. priceMessage += "\nQuantity: " + quantity;
  92. priceMessage += "\nTotal: $" + price;
  93. priceMessage += "\nThank you!";
  94. return priceMessage;
  95. }
  96.  
  97. /**
  98. * This method displays the given quantity value on the screen.
  99. */
  100. private void displayQuantity(int numberOfCoffees) {
  101. TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_veiw);
  102. quantityTextView.setText("" + numberOfCoffees);
  103. }
  104.  
  105. /**
  106. * This method displays the given text on the screen.
  107. */
  108. private void displayMessage(String message) {
  109. TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
  110. orderSummaryTextView.setText(message);
  111. }
  112.  
  113. }
Add Comment
Please, Sign In to add comment