Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.firman.app.iak;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.TextView;
- /**
- * Lanjutan dari MainActivity
- */
- public class Exercise3Activity extends AppCompatActivity {
- // Number of cups of coffee ordered
- int quantity = 2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_exercise3);
- }
- /**
- * This method is called when the plus button is clicked.
- */
- public void increment(View view) {
- quantity = quantity + 1;
- displayQuantity(quantity);
- }
- /**
- * This method is called when the minus button is clicked.
- */
- public void decrement(View view) {
- quantity = quantity - 1;
- displayQuantity(quantity);
- }
- /**
- * This method is called when the order button is clicked.
- */
- public void submitOrder(View view) {
- //Add EditTextView
- EditText nameField = (EditText) findViewById(R.id.name_fields);
- String names = nameField.getText().toString();
- Log.v("Exercise3Activity", "Name: " + names);
- CheckBox whippedCreamCheckBox = (CheckBox)findViewById(R.id.whipped_cream_checkboxs);
- boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
- Log.v("Main Activity", "Has Whipped Cream: " + hasWhippedCream);
- CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkboxs);
- boolean hasChocolate = chocolateCheckBox.isChecked();
- int price = calculatePrice();
- String message = createOrderSummary(names, price, hasWhippedCream, hasChocolate);
- displayMessage(message);
- }
- /**
- * This method displays the given quantity value on the screen.
- */
- private void displayQuantity(int number) {
- TextView quantityTextView = (TextView) findViewById(
- R.id.quantity_text_view);
- quantityTextView.setText("" + number);
- }
- /**
- * This method displays the given text on the screen.
- */
- private void displayMessage(String message) {
- TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
- orderSummaryTextView.setText(message);
- }
- private int calculatePrice() {
- int price = quantity * 5;
- return price;
- }
- private String createOrderSummary(String name, int price, boolean addWhipedCream, boolean addChocolate) {
- String priceMessage = "Nama : " + name;
- priceMessage += "\nAdd Whiped Cream? " + addWhipedCream;
- priceMessage += "\nAdd Chocolate? " + addChocolate;
- priceMessage = priceMessage + "\nQuantity " + quantity;
- priceMessage = priceMessage + "\nTotal: " + price;
- priceMessage = priceMessage + "\nThank You";
- return priceMessage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement