1

I am trying to apply discount at purchasing products as a menu but output is always 0 at selling price.

import java.util.List;

public class MenuProduct extends Product {

private double totalCE=0;
private double totalSP=0;

public MenuProduct(String name, List<Product> list){
    super(name);
    for (int i=0; i<list.size(); i++){
        if (list.get(i) instanceof MainDish){
            totalSP+= (list.get(i).getSellingPrice()*(90/100));
        }
        else if (list.get(i) instanceof Dessert){
            totalSP+= (list.get(i).getSellingPrice()*(80/100));
        }
        else if (list.get(i) instanceof Beverage){
            totalSP+= (list.get(i).getSellingPrice()*(50/100));
        }

        totalCE+= list.get(i).calculateExpense();
    }

}

public double calculateExpense() {
    return totalCE;
}
public double getSellingPrice(){
    return totalSP;
}

}

1 Answers1

3

The problem is because of the integer division. Note that 90/100 = 0 and not 0.9 which you are expecting.

Change 90/100 to 90.0/100 (and similarly other integer divisions posted in this question) to resolve the issue.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72