the application gives you an order summary when squeezing the whipped cream box should add a dollar to the price, but this does not work
/** * IMPORTANT: Make sure you are using the correct package name. * This example uses the package name: * package com.example.android.justjava * If you get an error when copying this code into Android studio, update it to match teh package name found * in the project's AndroidManifest.xml file. **/
package com.example.android.justjava;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
import java.util.jar.Attributes;
import androidx.appcompat.app.AppCompatActivity;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 0 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.Whipped_cream_CheckBox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.Chocolate_CheckBox);
boolean hasChocolate = chocolateCheckBox.isChecked();
EditText nameEditText = findViewById(R.id.Name_view);
String name = nameEditText.getText().toString();
int price = calculatePrice();
displayMessage(createOrderSummary(price, hasWhippedCream, hasChocolate, name ) );
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice() {
boolean addWippedCream = false;
if (addWippedCream )
return (quantity * 5) + 1;
else {
return quantity *5;
}
}
/**
* 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);
}
private String createOrderSummary(int price, boolean addWippedCream, boolean addChocolate, String name ){
String priceMessage ="Name : " + name ;
priceMessage += "\nAdd whipped cream? " + addWippedCream;
priceMessage += "\nAdd chocolate? " + addChocolate;
priceMessage += "\nQuantity:" + quantity;
priceMessage += "\ntotal = $ " + price ;
priceMessage += "\nthank you!";
return priceMessage;
}
/**
* 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_Sammury_text_view);
orderSummaryTextView.setText(message);
}
}
Aucun commentaire:
Enregistrer un commentaire