0
votes

I need to change the price of an item when its checkbox is checked. I need to do this in a for loop but I am quite unsure how to make this work. The prices are in a separate class and I do not know how to put them in an array.

This is my pizza class:

public double getCheese_Price() {
    return cheese_Price;
}

public void setCheese_Price(double cheese_Price) {
    this.cheese_Price = cheese_Price;
}

public double getPepperoniPrice() {
    return pepperoniPrice;
}

public void setPepperoniPrice(double pepperoniPrice) {
    this.pepperoniPrice = pepperoniPrice;
}

public double getChickenPrice() {
    return chickenPrice;
}

public void setChickenPrice(double chickenPrice) {
    this.chickenPrice = chickenPrice;
}

So, I need to set this to a certain number in this for loop but I'm not sure how to put those in an array:

for ( int i = 0; i < checkTopping.length; i++ ) {
    if(checkCount >= 6 && !checkTopping[i].isChecked()) ((CheckBox) view).setEnabled(false);
    else ((CheckBox) view).setEnabled(true);

    if(checkTopping[i].isChecked())
    {
        if(!toppingList.contains(checkTString[i]))
            toppingList.add(checkTString[i]);
        //pizza.setPrice[i](i);??
        checkCount++;
    }else{
        toppingList.remove(checkTString[i]);
        checkCount--;
    }
}
Without all the code it is hard to know exactly what you are trying to do - I'm guessing a small app letting you 'build' a pizza and work out the total price? It looks like the initial approach to the classes is wrong. I would think that rather than a class having a price for cheese, pepperoni, etc. as fields you should have a map/array containing the items added to the pizza - even just strings like 'pepperoni', 'cheese'. Then when you want to total the price you lookup those prices in another price-map. { 'cheese': 1.29, 'pepperoni':0.59}. - JohnXF