I am wondering if there's a way to loop a checkbox instead of using bunch of IF loops.
My concern here is: Is there a way to loop this and why I cannot use Else If when conditioning checkbox.
Here is my code:
@FXML
private CheckBox cb1;
@FXML
private CheckBox cb2;
@FXML
private CheckBox cb3;
@FXML
private CheckBox cb4;
public void checkEvent(ActionEvent event) {
int price = 0;
String message = "";
if (cb1.isSelected()) {
price = 150;
message += cb1.getText() + " Price: " + price + "\n";
}
if (cb2.isSelected()) {
price = 200;
message += cb2.getText() + " Price: " + price + "\n";
}
if (cb3.isSelected()) {
price = 100;
message += cb3.getText() + " Price: " + price + "\n";
}
if (cb4.isSelected()) {
price = 200;
message += cb4.getText() + " Price: " + price +"\n";
}
lbllist.setText(message);
}
Output:
Let's say I have
- Fries Price 150
- Burger Price 200
- Salad Price 100
- Bacon Price 200
So I have a total of 650 money
The output works, but if I have placed these data into a model class, which follows the MVC pattern. It does not add up or increment the values.