dimanche 20 juin 2021

Adding variable automatically when checkbox is checked Android Studio

Is it possible to make arithmetic calculation automatically when checkbox is pressed? I want to add the total harga (total price) change based the choice of checkbox topping prices and add them with the basic prices (product price).

ex: checked CB Extra Ayam and CB Kepala will add 3000 + 4000 then add it with basic product price 10000 so the total price would be 17000 and the total price will back to 10000 when im unchecked CB Extra Ayam and CB Kepala and so on

screenshoot

Here is my code

if (Code.toUpperCase().matches((inMieAyam.toUpperCase()))){
            
    //set text topping
    CBTopping1.setText("Extra Ayam");
    CBTopping2.setText("Extra Tetelan Sapi");
    CBTopping3.setText("Cakar (2 pcs)");
    CBTopping4.setText("Kepala");
    CBTopping5.setText("Bakso Sapi");
    CBTopping6.setText("Telur Mata Sapi");
    CBTopping7.setText("Balungan Rica Ayam");
    CBTopping8.setText("Extra Sawi");
    CBTopping9.setText("Extra Acar");

    //hide remaining topping CB
    CBTopping10.setVisibility(View.GONE);

    //set price to variable
    Price1 = 3000;
    Price2 = 7000;
    Price3 = 4000;
    Price4 = 4000;
    Price5 = 3000;
    Price6 = 4000;
    Price7 = 4000;
    Price8 = 1000;
    Price9 = 1000;
    Price10 = 0;

    //set price topping
    TxtvToppingPrice1.setText("Rp."+Price1);
    TxtvToppingPrice2.setText("Rp."+Price2);
    TxtvToppingPrice3.setText("Rp."+Price3);
    TxtvToppingPrice4.setText("Rp."+Price4);
    TxtvToppingPrice5.setText("Rp."+Price5);
    TxtvToppingPrice6.setText("Rp."+Price6);
    TxtvToppingPrice7.setText("Rp."+Price7);
    TxtvToppingPrice8.setText("Rp."+Price8);
    TxtvToppingPrice9.setText("Rp."+Price9);


    //hide remaining topping price Txtv
    TxtvToppingPrice10.setVisibility(View.GONE);
    

    FinalPrice1 = 0;
    FinalPrice2 = 0;
    FinalPrice3 = 0;
    FinalPrice4 = 0;
    FinalPrice5 = 0;
    FinalPrice6 = 0;
    FinalPrice7 = 0;
    FinalPrice8 = 0;
    FinalPrice9 = 0;
    FinalPrice10 = 0;

    CBTopping1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice1 = Price1;
            }
            else {
                FinalPrice1 = 0;
            }
        }
    });

    CBTopping2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice2 = Price2;
            }
            else {
                FinalPrice2 = 0;
            }
        }
    });

    CBTopping3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice3 = Price3;
            }
            else {
                FinalPrice3 = 0;
            }
        }
    });

    CBTopping4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice4 = Price4;
            }
            else {
                FinalPrice4 = 0;
            }
        }
    });

    CBTopping5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice5 = Price5;
            }
            else {
                FinalPrice5 = 0;
            }
        }
    });

    CBTopping6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice6 = Price6;
            }
            else {
                FinalPrice6 = 0;
            }
        }
    });

    CBTopping7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice7 = Price7;
            }
            else {
                FinalPrice7 = 0;
            }
        }
    });

    CBTopping8.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice8 = Price8;
            }
            else {
                FinalPrice8 = 0;
            }
        }
    });

    CBTopping9.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice9 = Price9;
            }
            else {
                FinalPrice9 = 0;
            }
        }
    });


    FoodPrice1 = Integer.parseInt(Price);
    TotalFinalPrice = FoodPrice1 + FinalPrice1 + FinalPrice2 + FinalPrice3 +
            FinalPrice4 + FinalPrice5 + FinalPrice6 +
            FinalPrice7 + FinalPrice8 + FinalPrice9 ;

    TxtvTotalHarga.setText(Integer.toString(TotalFinalPrice));

}

the variable of the TotalFinalPrice just not changed or maybe not calculated




Aucun commentaire:

Enregistrer un commentaire