-1

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:

http://prntscr.com/l6ke08

After Selection:

http://prntscr.com/l6ke6h

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 :)

4 Answers4

1

compare strings using:"string1.equals(String target)" check below link: if statement comparing strings in java android

Jerry
  • 21
  • 2
0

On first look the thing that I think gets wrong is the equals() method not being used to compare the strings in your calculatePrice() method.

You should use a code like this:

public final class Order {

    public static int CalculatePrice(String size, boolean meat, boolean cheese, boolean veggies){
        int price = 0;

        if (size.equals("Small")){
            price += 9;
        }
        else if (size.equals("Medium")){
            price += 10;
        }
        else if (size.equals("Large")){
            price += 11;
        }
        if (meat == true){
            price += 2;
        }
        if (cheese == true){
            price += 2;
        }
        if (veggies == true){
            price += 2;
        }

        return price;
    }

}
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
0

SetOnCheckedChangedListener calls the function when checked status is changed. So, onCheckedChanged function is called, even if the radio button is UNCHECKED.

So, if you want to change savedStatus, you could use isChecked parameters, or try this.

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (radioGroup.getCheckedRadioButtonId()) {
                case R.id.small_radio_button:
                    sizeStatus = "Small";
                    break;
                case R.id.medium_radio_button:
                    sizeStatus = "Medium";
                    break;
                case R.id.large_radio_button:
                    sizeStatus = "Large";
            }
            onCalculate();
        }
    });
sso.techie
  • 136
  • 7
0

Use Equals method for comparing String and use equalsIgnoreCase method for case complexity.

public static int CalculatePrice(String size, boolean meat, boolean cheese, boolean veggies){
    int price = 0;

    if (size.equalsIgnoreCase("Small")){ 
      price += 9;
    }
    else if (size.equalsIgnoreCase("Medium")){
      price += 10;
    }
    else if (size.equalsIgnoreCase("Large")){
      price += 11;
    }
    if (meat){
      price += 2;
    }
    if (cheese){
      price += 2;
    }
    if (veggies){
      price += 2;
    }

    return price;
  }
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39