T.A. T.A. - 3 years ago 227
Android Question

Update Total Price Using Checkbox and Buttons Android Studio

The coffee shop sells one type of coffee for $5 and has the option for whipped cream and chocolate for $1 extra each. I can not figure out how to properly get the

CheckBox
option for whipped cream and chocolate to add to the
show_total
?

For example, one coffee with cream and chocolate should equal $7 and three coffees with just chocolate should equal $18.

Do I need to call a separate method to get onCreamClick, onChocClick, countIN, and countDE to work together properly for one total price?

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;
}
}

Answer Source

You need to register listeners on your checkboxes and buttons in order to react to these actions.

whipped_cream.setOnCheckedChangeListener((checkBox, isChecked) -> onCreamClick());    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download