ant2009
ant2009

Reputation: 22526

cleaning up some complicated logic for different conditions

Java 8

I have the following chart that will do something based on the conditions in the below table

enter image description here

Just wondering is this the best way to satisfy all conditions

private void setDisplayTheStars() {
    if (discount && rate) {
        2Stars = true;
    }
    else if (!discount && rate) {
        2Stars = true;
    }
    else if (discount && !rate) {
        1Stars = true;
    }
}

Upvotes: 1

Views: 39

Answers (1)

Thiyagu
Thiyagu

Reputation: 17900

You should set 2Stars if rate is true;

Then check if discount is true - then set 1Stars to true.

if (rate) {
    2Stars = true;
}
else if (discount) {
    1Stars = true;
}

If you are declaring boolean, then it can be done as

boolean twoStars = rate == true;
boolean oneStars = !twoStars && discount == true;

Upvotes: 4

Related Questions