Honinbo Shusaku
Honinbo Shusaku

Reputation: 1521

Alternative to if else statement when using doubles

Based on the salaries, I need to assign a specific tax rate on my employee objects. The salary is defined by yearlySalary, which is a double, so I couldn't use a switch statement. I instead used if/else:

public int getSalaryRank() {
    if(yearlySalary <= 60000.00) {
        salaryRank = 1;
    } else if(yearlySalary > 60000.00 && yearlySalary <= 80000.00) {
        salaryRank = 2;
    } else if(yearlySalary > 80000.00 && yearlySalary <= 100000.00) {
        salaryRank = 3;
    } else if(yearlySalary > 100000.00 && yearlySalary <= 125000.00) {
        salaryRank = 4;
    } else {
        salaryRank = 5;
    } return salaryRank; }

I will assign tax rates later on based on the rank. Is there a better way to write this?

Upvotes: 3

Views: 704

Answers (4)

Svetlin Zarev
Svetlin Zarev

Reputation: 15683

You can simplify it like this:

public int getSalaryRank() {
    int salaryRank;

    if(yearlySalary <= 60000.00) {
        return  1;
    } 

    if(yearlySalary <= 80000.00) {
        return 2;
    } 

    if(yearlySalary <= 100000.00) {
        return 3;
    } 

    if(yearlySalary <= 125000.00) {
       return 4;
    } 

    return 5

}

All the checks on the left side are unnecessary, because the statements are executed in order. Furthermore, you can remove the else statements and directly return the salaryrank . Also when you are dealing with money NEVER EVER use floating point numbers. Use BigDecimal instead

Edit: Taking into account @AlexWien's comment about the multiple exit points this might be a better solution:

public int getSalaryRank() {
        if(yearlySalary <= 60000.00) {
            salaryRank = 1;
        } else  if(yearlySalary <= 80000.00) {
            salaryRank = 2;
        } else if(yearlySalary <= 100000.00) {
            salaryRank = 3;
        } else if(yearlySalary <= 125000.00) {
           salaryRank = 4;
        } else {
           salaryRank = 5;
        }  

        return salaryRank;

    }

Upvotes: 3

AlexWien
AlexWien

Reputation: 28747

Consider using a while loop through an List or array of (salary) limits.

Upvotes: 4

Reinstate Monica
Reinstate Monica

Reputation: 2808

Your program will not get to the else statement if any of the previous if's were true. So, you could use that to your advantage and leave out the following: yearlySalary > 60000.00 &&

if(yearlySalary <= 60000.00) {
    salaryRank = 1;
} else if(yearlySalary <= 80000.00) {
    salaryRank = 2;
}

Also, doubles aren't particularly precise. They're floating point numbers, which means that you can't accurately hold the value of for instance 0.1. Look it up. BigDecimal is a better type to use with currency.

Upvotes: 2

Tony the Pony
Tony the Pony

Reputation: 41417

How about:

public int getSalaryRank() 
{
    if(yearlySalary <= 60000.00) return 1;
    if(yearlySalary <= 80000.00) return 2;
    if(yearlySalary <= 100000.00) return 3;
    if(yearlySalary <= 125000.00) return 4;
    return 5;
}

Upvotes: 2

Related Questions