не могу понять метод расчета - PullRequest
0 голосов
/ 28 мая 2018

У меня есть проблема в методе расчета

это должно сделать так, как если бы вы дали мне эту общую сумму, например, количество * 5
это должно быть похоже на любое число * 5, как 1 * 5 = 5 2 * 5 = 10

но проблема здесь, если я изменю количество на 2, это даст мне цену 20 $

количество 3 даст мне цену 45, в то время как это должно быть цена 15, я пытался искатьпо проблеме ничего не могу найти

/**
 * 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 increamnt 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);
    }
}

1 Ответ

0 голосов
/ 28 мая 2018

Вы умножаете на количество в два раза.Измените это так:

 */
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;

}
...