I'm making a small Pizza Order App for a college assignment. In the app, if the user selects "small" or "medium" or "large" from a radiobutton group, the price shown will be recalculated with the information given. However, for some reason, the price shown after each time is not correct.
Here's the relevant code:
MainActivity.java
private void Init(){
onCalculate();
switchToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (switchToggle.isChecked()){
switchToggle.setText("Yes");
}
else {
switchToggle.setText("No");
}
}
});
smallRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sizeStatus = "Small";
onCalculate();
}
});
mediumRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sizeStatus = "Medium";
onCalculate();
}
});
largeRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sizeStatus = "Large";
onCalculate();
}
});
meatCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onCalculate();
}
});
cheeseCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onCalculate();
}
});
veggiesCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onCalculate();
}
});
}
private void onCalculate(){
resultTextView.setText("$" + Integer.toString(Order.CalculatePrice(sizeStatus, meatCheckBox.isChecked(), cheeseCheckBox.isChecked(), veggiesCheckBox.isChecked())));
}
Order.java
public final class Order {
public static int CalculatePrice(String size, boolean meat, boolean cheese, boolean veggies){
int price = 0;
if (size == "Small"){
price += 9;
}
else if (size == "Medium"){
price += 10;
}
else if (size == "Large"){
price += 11;
}
if (meat == true){
price += 2;
}
if (cheese == true){
price += 2;
}
if (veggies == true){
price += 2;
}
return price;
}
}
Note that the Init() method is called in the onCreate() method in MainActivity.java.
sizeStatus is a variable declared in MainActivity and is set to "Small" (string) as default (same as the initial RadioButton check).
It starts up the app with the correct value of $9 since, that is the radiobutton defaulted as checked. After that it displays incorrect size values for each button randomly:
Before Selection:
After Selection:
The rest of the method that handles toppings added to the pizza works fine. But the radiobuttons are messing up.
Any help would be appreciated :)