Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. package id.firman.app.iak;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.CheckBox;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. /**
  12. * Lanjutan dari MainActivity
  13. */
  14. public class Exercise3Activity extends AppCompatActivity {
  15.  
  16.  
  17. // Number of cups of coffee ordered
  18. int quantity = 2;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_exercise3);
  24. }
  25.  
  26. /**
  27. * This method is called when the plus button is clicked.
  28. */
  29. public void increment(View view) {
  30. quantity = quantity + 1;
  31. displayQuantity(quantity);
  32. }
  33.  
  34. /**
  35. * This method is called when the minus button is clicked.
  36. */
  37. public void decrement(View view) {
  38. quantity = quantity - 1;
  39. displayQuantity(quantity);
  40. }
  41.  
  42. /**
  43. * This method is called when the order button is clicked.
  44. */
  45. public void submitOrder(View view) {
  46.  
  47. //Add EditTextView
  48. EditText nameField = (EditText) findViewById(R.id.name_fields);
  49. String names = nameField.getText().toString();
  50. Log.v("Exercise3Activity", "Name: " + names);
  51.  
  52. CheckBox whippedCreamCheckBox = (CheckBox)findViewById(R.id.whipped_cream_checkboxs);
  53. boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
  54. Log.v("Main Activity", "Has Whipped Cream: " + hasWhippedCream);
  55.  
  56. CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkboxs);
  57. boolean hasChocolate = chocolateCheckBox.isChecked();
  58.  
  59. int price = calculatePrice();
  60.  
  61. String message = createOrderSummary(names, price, hasWhippedCream, hasChocolate);
  62. displayMessage(message);
  63.  
  64.  
  65. }
  66.  
  67. /**
  68. * This method displays the given quantity value on the screen.
  69. */
  70. private void displayQuantity(int number) {
  71. TextView quantityTextView = (TextView) findViewById(
  72. R.id.quantity_text_view);
  73. quantityTextView.setText("" + number);
  74. }
  75.  
  76.  
  77.  
  78. /**
  79. * This method displays the given text on the screen.
  80. */
  81. private void displayMessage(String message) {
  82. TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
  83. orderSummaryTextView.setText(message);
  84. }
  85.  
  86. private int calculatePrice() {
  87. int price = quantity * 5;
  88. return price;
  89.  
  90. }
  91.  
  92. private String createOrderSummary(String name, int price, boolean addWhipedCream, boolean addChocolate) {
  93. String priceMessage = "Nama : " + name;
  94. priceMessage += "\nAdd Whiped Cream? " + addWhipedCream;
  95. priceMessage += "\nAdd Chocolate? " + addChocolate;
  96. priceMessage = priceMessage + "\nQuantity " + quantity;
  97. priceMessage = priceMessage + "\nTotal: " + price;
  98. priceMessage = priceMessage + "\nThank You";
  99. return priceMessage;
  100.  
  101.  
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement