public class MainActivity extends AppCompatActivity { int quantity = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitiy_main);
}
/**
* 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;
int someQuantity = 100;
displayQuantity(quantity);
}
//This method is called when the order button is clicked.
public void submitOrder(View view) {
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
// Figure out if user the wants whipped cream topping
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
// Log.v("MainActivity", "Has Whipped Cream: " + hasWhippedCream);
// Figure out if the user wants chocolate topping
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
boolean hasChocolate = chocolateCheckBox.isChecked();
int price = calculatePrice(hasWhippedCream, hasChocolate);
String priceMessage = createOrderSummary(name, price, hasWhippedCream, hasChocolate);
displayMessage(priceMessage);
}
/**
* Calculates the price of the order.
*
* @param addWhippedCream is whether or not the user wants whipped cream topping
* @param addChocolate is whether or not the user wants chocolate cream topping
* @return total price
*/
private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
// price of 1 of cup coffee
int basePrice = 5;
// add $1 if the use wants whipped cream
if (addWhippedCream) {
basePrice = basePrice + 1;
}
// add $1 if the use wants chocolate
if (addChocolate) {
basePrice = basePrice + 2;
}
// calculate the total of order price by multiplying by quantity
return quantity * basePrice;
}
/**
* @param name of the customer
* @param price of the order
* @param addWhippedCream is whether or not the user wants whipped cream topping
* @param addChocolate is whether or not the user wants whipped cream topping
* @return
*/
private String createOrderSummary(String name, int price, boolean addWhippedCream, boolean addChocolate) {
String priceMessage = "Name: " + name;
priceMessage += "\nQuantity: " + quantity;
priceMessage += "\nAdd Whipped cream? " + addWhippedCream;
priceMessage += "\nAdd Chocolate? " + addChocolate;
priceMessage += "\nTotal: $" + price;
priceMessage += "\nThank you!";
return priceMessage;
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int numberOfCoffee) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + numberOfCoffee);
}
/**
* 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);
}
} [![enter image description here][1]][1] Add Chocolate? false when it is false on UI i want to show Hello if it is true i want to show hi, how can i replace ?
UI show me True or False i want to show on UI if true I want to show Hello, if it is False i want to show hi, how can i write if else in that code i am now experience can you help me please
Aucun commentaire:
Enregistrer un commentaire