Home > Software design >  How to do Salary >= 10000 && Salary <= 20000
How to do Salary >= 10000 && Salary <= 20000

Time:10-19

So heres my code:

public class employee {

    //attributes
    String Name = "name";
    String Position = "position";


    //behavior
    public static char getSalary(int Salary) {
        if (Salary >= 10000 && Salary <= 20000) {
            return 'C';
        } else if (Salary >= 20000 && Salary <= 40000) {
            return 'B';
        } else {
            return 'A';
        }
    }
}

when I run a test it gets error because if (Salary >= 10000 && Salary <= 20000) and (Salary >= 20000 && Salary <= 40000) both have 20000, what should I do?

CodePudding user response:

I think maybe u can modify condition such as item >= lower && item < upper. In most situation, reach 20000 is different from 19999.

CodePudding user response:

Depending on your business logic change the first or second condition to < 20000 or > 20000. If you want to have 'C' and 'B' returned, create an extra condition for it.

CodePudding user response:

FOR 20000 below code will always return C. because if first condition is True in IF-ELSE then all other if-else will not be used(try using breakpoint you will understand). Also you need to find out what is your boundary value. like if 20000 mean B, then you should use <20000 for C.

  • Related