咖啡厅以5美元的价格出售一种咖啡,可以选择加奶油和巧克力,每只加1美元。我无法弄清楚如何正确地将鲜奶油和巧克力的CheckBox
选项添加到show_total
?
例如,一份含有奶油和巧克力的咖啡应该等于7美元,三份只含巧克力的咖啡应该等于18美元。
我是否需要调用一个单独的方法来获取onCreamClick,onChocClick,countIN和countDE以正确地合作一个总价格?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.NumberFormat;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
TextView show_quantity, show_total, show_summary;
Button order, plus_one, negative_one;
EditText customer_name;
CheckBox whipped_cream, chocolate;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show_quantity = (TextView) findViewById(R.id.amount);
show_total = (TextView) findViewById(R.id.total);
show_summary = (TextView) findViewById(R.id.summary);
customer_name = (EditText) findViewById(R.id.customer_name);
plus_one = (Button) findViewById(R.id.plus_one);
negative_one = (Button) findViewById(R.id.negative_one);
whipped_cream = (CheckBox) findViewById(R.id.whipped_cream);
chocolate = (CheckBox) findViewById(R.id.chocolate);
order = (Button) findViewById(R.id.order);
order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (customer_name.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter a name.", Toast.LENGTH_SHORT).show();
} else {
show_summary.setText("ORDER SUMMARY" + " \n" +
"Customer - " + customer_name.getText().toString() + " \n" +
"Whipped Cream? " + whipped_cream.isChecked() + " \n" +
"Chocolate? " + chocolate.isChecked() + " \n" +
"Total - " + show_total.getText().toString() + " \n" +
"Thank you!");
}
}
});
}
public void onCreamClick(View c) {
boolean ch1 = whipped_cream.isChecked();
if (!ch1) {
double coffee = 5;
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String cof = format.format(coffee);
show_total.setText(cof);}
else if (ch1) {
double cream = 1;
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String crm = format.format(cream);
show_total.setText(crm);}
}
public void onChocClick(View chc) {
boolean ch2 = chocolate.isChecked();
if (!ch2) {
double coffee = 5;
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String cof = format.format(coffee);
show_total.setText(cof);}
else if (ch2){
double chocolate = 1;
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String choc = format.format(chocolate);
show_total.setText(choc);}
}
public void countIN(View in) {
counter++;
double coffee = 5 * counter;
if (counter > 10) {
Toast.makeText(getApplicationContext(), "You can not order more than 10 coffees", Toast.LENGTH_SHORT).show();
} else {
show_quantity.setText(Integer.toString(counter));
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String cof = format.format(coffee);
show_total.setText(cof);
}
}
public void countDE(View de) {
counter--;
double coffee = 5 * counter;
if (counter < 0) {
Toast.makeText(getApplicationContext(), "Please order at least one coffee", Toast.LENGTH_SHORT).show();
} else {
show_quantity.setText(Integer.toString(counter));
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
String cof = format.format(coffee);
show_total.setText(cof);
}
}
public static double display_total(double price, double total) {
return price;
}
}
答案 0 :(得分:0)
您需要在复选框和按钮上注册侦听器,以便对这些操作做出反应。
whipped_cream.setOnCheckedChangeListener((checkBox, isChecked) -> onCreamClick());