androidcalculated-field

Can't understand calculate method


I have problem in calculate method

it should do like give me that total price like quantity5
it should do like any number
5 like 15 =5 25 =10

but the problem here if I change the quantity to 2 it will give me the price 20 $

quantity 3 it will give me price 45, while it should price be 15 I tried to search for the problem can't find anything

/**
 * 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 justjava.andriod.example.com.justjava;


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;
import android.widget.Toast;

import java.text.NumberFormat;

import justjava.andriod.example.com.justjava.R;

/**
 * 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) {
        // this method to block the user to use increment method if the number is 100
        if (quantity==100){
           //show an error message as a toast
            Toast.makeText(this, "you cannot have more than 100 coffes", Toast.LENGTH_SHORT).show();
            // Exit this method early because there's nothing left to do
            return;
        }
        quantity = quantity+1;
        display(quantity);

    }
    /**
     * This method is called when the minus button is clicked.
     */
    public void decrement (View view) {
// this if statment block user to make decremnt method below number 1 so it wont go to 0
        if (quantity==1) {
            // show an error message as a toast
            Toast.makeText(this, "you cannot have more less than 1 coffee", Toast.LENGTH_SHORT).show();
            // Exit this method early because there's nothing left to do
            return;
        }
        quantity =  quantity - 1;
        display(quantity);
    }



    /**
     * Calculates the price of the order.
     *@return total price

     */
    private String creatOrderSummary(String name, int basePrice, boolean addWhippedCream , boolean addCocolate) {
        String priceMessage = "Name : " + name;
        priceMessage += "\nAdd Whipped Cream? " + addWhippedCream;
        priceMessage += "\nadd chocolate? " + addCocolate;
        priceMessage = priceMessage + "\nQuantity" + quantity;
        priceMessage = priceMessage + "\nTotall: $"+ basePrice;
        priceMessage = priceMessage + "\nthank you!";
        return priceMessage;


    }


    /**
     * Calculates the price of the order.
     *@return total price

     */
    private int calculatePrice(boolean addWhippedCream, boolean addChocolate ) {
        // pice of 1 cup of coffe
        int basePrice = quantity *5;
        // add $1 if the user wants whipped cream
        if (addChocolate) {
            basePrice = basePrice + 1;
        }
        // add $2 if the user wants chocolate
        if (addChocolate) {
            basePrice = basePrice + 2;
        }

        // calcuate the tottal order price by multiplying by quantity
        return quantity * basePrice;

    }



    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        // find the user's name
        EditText text = (EditText) findViewById(R.id.name_field);
        String name = text.getText().toString();
        // figure it out if user wants whipped cream topping
        CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
        boolean hasWippedCream = whippedCreamCheckBox.isChecked();
        Log.v("MainActivity", "Has whipped cream: " + hasWippedCream);
        // figure it out if user want chocolate
        CheckBox chocolate = (CheckBox) findViewById(R.id.add_chocolate);
        boolean hasCocolate = chocolate.isChecked();

        int price = calculatePrice(hasWippedCream, hasCocolate);
        String priceMessage = creatOrderSummary(name,price, hasWippedCream, hasCocolate);
        displayMessage(priceMessage);

    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(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);
    }
}

Solution

  • You're multiplying by quantity twice. Change it like this:

     */
    private int calculatePrice(boolean addWhippedCream, boolean addChocolate ) {
        // pice of 1 cup of coffe
        int basePrice = 5;
        // add $1 if the user wants whipped cream
        if (addChocolate) {
            basePrice = basePrice + 1;
        }
        // add $2 if the user wants chocolate
        if (addChocolate) {
            basePrice = basePrice + 2;
        }
    
        // calcuate the tottal order price by multiplying by quantity
        return quantity * basePrice;
    
    }