我的意图是否干扰了我的方法?

时间:2018-03-25 23:08:44

标签: java android-intent methods

我来回试图获取我的咖啡订购应用程序的数量和价格数字,以便正确显示。

自从我添加了我的Intent代码以便将我的订单摘要传递给电子邮件应用程序后,他们就变得很糟糕。我尝试的每个修复都修复了一个或另一个,但从来没有。

如果有人能够看一遍并帮我弄清楚错误的位置,那将非常感激。我认为问题在于区分数量和列出的数量。

/**
 * 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.content.Intent;
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;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {



    int listedQuantity = 2;

    int price;

    @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) {
//        String message = createOrderSummary(listedQuantity);
//        displayQuantityPrice(message);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject());
        intent.putExtra(Intent.EXTRA_TEXT, createOrderSummary(price));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    private String emailSubject(){
        String subjectText;
        subjectText = "Coffee Order";
        return subjectText;
    }


    /**
     * Creates an order summary.
     *
     * @param quantity is the number of cups of coffee ordered
     */

        private String createOrderSummary(int quantity) {
    //find the checkbox here
    //make a boolean variable
    // set boolean variable to state of checkbox using isChecked()
    price = calculatePrice(quantity);
    String priceMessage ="Name: " + customersName();
    priceMessage = priceMessage + "\nAdd whipped cream? " + hasWhippedCream();
    priceMessage = priceMessage + "\nAdd chocolate? " + hasChocolate();
    priceMessage = priceMessage + "\nQuantity: " + listedQuantity;
    priceMessage = priceMessage + "\nTotal: $" + price;
    priceMessage = priceMessage + "\nThank You!";
    return priceMessage;
}

private boolean hasChocolate() {
    boolean hasChocolate;
    CheckBox chocolateCheckBox = findViewById(R.id.chocolate_check_box);
    hasChocolate = chocolateCheckBox.isChecked();
    return (hasChocolate);
}

public boolean hasWhippedCream() {
    boolean hasWhippedCream;
    CheckBox creamCheckBox = findViewById(R.id.cream_check_box);
    hasWhippedCream = creamCheckBox.isChecked();
    Log.v("MainActivity", "Add whipped cream? " + hasWhippedCream);
    return(hasWhippedCream);
}

private String customersName() {
    String customersName;
    EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);
    customersName = nameEditText.getText().toString();
    return(customersName);
}

/** These method is supposed to modify calculatePrice to include results for whipped cream
* and chocolate.
*/
/**
* Displays full order summary.
*/

private int calculatePrice(int quantity) {
    boolean hasWhippedCream;
    boolean hasChocolate;
    if (hasWhippedCream() && hasChocolate()) {
        return listedQuantity * 8;
    }else if (hasChocolate()){
        return listedQuantity * 7;
    }else if (hasWhippedCream()){
        return listedQuantity * 6;
    }
    return listedQuantity * 5;
}
/*** This method is called when the increment button is clicked.*/

public void increment(View view) {
    if (listedQuantity == 100) {
        Toast.makeText(this, "you cannot have more than 100 cups of coffee", Toast.LENGTH_SHORT).show();
        return;
    }listedQuantity = listedQuantity + 1;
    displayQuantity(listedQuantity);
}
/*** This method is called when the decrement button is clicked.*/

public void decrement(View view) {
    if (listedQuantity == 1) {
        Toast.makeText(this, "you cannot have less than 1 cup ofcoffee", Toast.LENGTH_SHORT).show();
        return;
    }listedQuantity = listedQuantity - 1;
    displayQuantity(listedQuantity);
}
/*** This method displays the given quantity value on the screen.*/

private void displayQuantity(int amount) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + amount);
}
/*** This method displays the given price on the screen.*/
private void displayQuantityPrice(String message) {
    TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
    orderSummaryTextView.setText(message);
}
private void displayMessage(String message) {
    TextView orderSummaryTextView = findViewById(R.id.order_summary_text_view);
    orderSummaryTextView.setText(message);
}

我的订单摘要应该显示数量大于0的数字和数量的数字*咖啡杯及其配料的价格。起初价格总是0,但数量是正确的。为了解决这个问题,我尝试在第118行替换代码中的所有数量,除了[private int calculatePrice(int quantity)]。这样就修改了价格以显示正确的数字,但它将我的数量显示为0.然后我改变了在第118行的数量上市数量认为这是错误,但只是将两个数字都设置为0.我在整个课程中一直在研究这个应用程序,我不记得为什么我做了数量和列出数量两个不同变量,但我知道这是一个重要原因。真正奇怪的是,在我将一个intent放入我的submitOrder方法之前,这个错误永远不会出现。

0 个答案:

没有答案