Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Basically, the math should be $5 + $1 + $2 if I get both the whipped cream and chocolate.
- If I do $5 + $1 should be just for the whipped cream
- $5 + $2 should be for the chocolate.
- The price goes up the more cups you add to the equation. What am I missing?
- When the code is ran on my device, nothing adds correctly.
- package com.example.android.justjava;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.TextView;
- /**
- * 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 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) {
- //Displays name of customer when order button is pressed.
- EditText nameField = (EditText) findViewById(R.id.name_field);
- String name = nameField.getText().toString();
- //Figures out if the user wants whipped cream topping.
- CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
- boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
- //Figures out if 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 user wants whipped cream
- * @param addChocolate is whether or not user wants chocolate
- * @return total price
- */
- private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
- //Price of 1 cup of coffee
- int basePrice = 5;
- //Adds $1 if user wants whipped cream
- if (addWhippedCream); {
- basePrice = basePrice + 1;
- }
- //Adds $2 if user wants chocolate
- if (addChocolate) {
- basePrice = basePrice + 2;
- }
- //Calculate total order price by multiplying by quantity
- return quantity * basePrice;
- }
- /**
- * Create summary of the order.
- *
- * @param name of the customer.
- * @param price of the order.
- * @param hasWhippedCream is whether or not user wants whipped cream.
- * @param hasChocolate is Whether or not user wants chocolate.
- * @return text summary.
- */
- private String createOrderSummary(String name, int price, boolean hasWhippedCream, boolean hasChocolate) {
- String priceMessage = "Name: " + name;
- priceMessage += "\nAdd whipped cream? " + hasWhippedCream;
- priceMessage += "\nAdd chocolate? " + hasChocolate;
- 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_summary_text_view);
- orderSummaryTextView.setText(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement