Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. Basically, the math should be $5 + $1 + $2 if I get both the whipped cream and chocolate.
  2.  
  3. If I do $5 + $1 should be just for the whipped cream
  4.  
  5. $5 + $2 should be for the chocolate.
  6.  
  7. The price goes up the more cups you add to the equation. What am I missing?
  8.  
  9. When the code is ran on my device, nothing adds correctly.
  10.  
  11. package com.example.android.justjava;
  12.  
  13. import android.os.Bundle;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.view.View;
  16. import android.widget.CheckBox;
  17. import android.widget.EditText;
  18. import android.widget.TextView;
  19.  
  20. /**
  21. * This app displays an order form to order coffee.
  22. */
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25. int quantity = 0;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. }
  32.  
  33. /**
  34. * This method is called when the plus button is clicked.
  35. */
  36. public void increment(View view) {
  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. quantity = quantity - 1;
  46. displayQuantity(quantity);
  47. }
  48.  
  49. /**
  50. * This method is called when the order button is clicked.
  51. */
  52. public void submitOrder(View view) {
  53.  
  54. //Displays name of customer when order button is pressed.
  55. EditText nameField = (EditText) findViewById(R.id.name_field);
  56. String name = nameField.getText().toString();
  57.  
  58. //Figures out if the user wants whipped cream topping.
  59. CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
  60. boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
  61.  
  62. //Figures out if user wants chocolate topping.
  63. CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
  64. boolean hasChocolate = chocolateCheckBox.isChecked();
  65.  
  66. int price = calculatePrice(hasWhippedCream, hasChocolate);
  67. String priceMessage = createOrderSummary(name, price, hasWhippedCream, hasChocolate);
  68. displayMessage(priceMessage);
  69. }
  70.  
  71. /**
  72. * Calculates the price of the order.
  73. *
  74. * @param addWhippedCream is whether or not user wants whipped cream
  75. * @param addChocolate is whether or not user wants chocolate
  76. * @return total price
  77. */
  78. private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
  79. //Price of 1 cup of coffee
  80. int basePrice = 5;
  81.  
  82. //Adds $1 if user wants whipped cream
  83. if (addWhippedCream); {
  84. basePrice = basePrice + 1;
  85. }
  86.  
  87. //Adds $2 if user wants chocolate
  88. if (addChocolate) {
  89. basePrice = basePrice + 2;
  90. }
  91.  
  92. //Calculate total order price by multiplying by quantity
  93. return quantity * basePrice;
  94. }
  95.  
  96. /**
  97. * Create summary of the order.
  98. *
  99. * @param name of the customer.
  100. * @param price of the order.
  101. * @param hasWhippedCream is whether or not user wants whipped cream.
  102. * @param hasChocolate is Whether or not user wants chocolate.
  103. * @return text summary.
  104. */
  105. private String createOrderSummary(String name, int price, boolean hasWhippedCream, boolean hasChocolate) {
  106. String priceMessage = "Name: " + name;
  107. priceMessage += "\nAdd whipped cream? " + hasWhippedCream;
  108. priceMessage += "\nAdd chocolate? " + hasChocolate;
  109. priceMessage += "\nQuantity: " + quantity;
  110. priceMessage += "\nTotal: $" + price;
  111. priceMessage += "\nThank you!";
  112. return priceMessage;
  113. }
  114.  
  115. /**
  116. * This method displays the given quantity value on the screen.
  117. */
  118. private void displayQuantity(int number) {
  119. TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  120. quantityTextView.setText("" + number);
  121. }
  122.  
  123. /**
  124. * This method displays the given text on the screen.
  125. */
  126. private void displayMessage(String message) {
  127. TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
  128. orderSummaryTextView.setText(message);
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement