mardi 5 mai 2020

How to get required outcome from checkboxes and radio button in android studio java

I am making an app that allows the users to customise the waffle that they wish to order. it provides the options to select the filling, spread and size. As the user makes the selections, the total amount should be updated on the Textview at the bottom of the screen. I am having issue with showing the required output as the image and i get negative values when i uncheck or change radio buttons.

Image for final output



    //update the prices when checkbox checked
    public void updatePrice() {
        for (CheckBox filling : checkBoxFilling) {
            filling.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!isChecked) {
                        checkboxFillingCount--;
                        disableCheckboxes();

                        discountItem = discountItem - (waffleFilling.get(buttonView.getText().toString()) * 0.80);
                        nonDiscountItem = nonDiscountItem - waffleFilling.get(buttonView.getText().toString());

                    } else {
                        checkboxFillingCount++;
                        disableCheckboxes();
                        discountItem = discountItem + (waffleFilling.get(buttonView.getText().toString()) * 0.80);
                        nonDiscountItem = nonDiscountItem + waffleFilling.get(buttonView.getText().toString());
                    }
                    fillingPrice = discountItem + nonDiscountItem;
                    totalDamage();
                    totalPriceWithoutDiscount();
                    amountSaved();
                }
            });
        }
    }

    public void updatePriceSpread() {
        for (CheckBox spread : checkBoxSpread) {
            spread.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!isChecked) {
                        checkboxSpreadCount--;
                        disableCheckboxes();
                        spreadPrice = spreadPrice - waffleSpread.get(buttonView.getText().toString());
                        totalDamage();
                        totalPriceWithoutDiscount();
                        amountSaved();
                    } else {
                        checkboxSpreadCount++;
                        disableCheckboxes();
                        spreadPrice = 2.50;
                        spreadPrice = spreadPrice + waffleSpread.get(buttonView.getText().toString());
                        totalDamage();
                        totalPriceWithoutDiscount();
                        amountSaved();
                    }
                }
            });
        }
    }

    public void updateSizePrice() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                size = findViewById(checkedId);
                sizePrice = waffleSize.get(size.getText().toString());
                totalDamage();
            }
        });
    }

    public double spreadDiscount() {
        if (checkBoxSpread[1].isChecked() && checkBoxSpread[2].isChecked() && checkBoxSpread[3].isChecked()) {
            spreadPrice = 2.50;
        }
        return spreadPrice;

    }
}










Aucun commentaire:

Enregistrer un commentaire