# Thread: Alternatives to else if statement

1. Member
Join Date
Sep 2012
Posts
8
Rep Power
0

## Alternatives to else if statement

Hello everyone. So my problem is the following : I am creating this method which will allow me to calculate the cost of a person's luggage. So if the weight of the luggage is <20kg no fee is incurred. Thereafter for every 5kg increase in weight the passenger will be charged an extra 2\$. For example if a the weight is 27kg, the luggage cost is 4\$ and if 36kg luggage cost is 8\$ etc. Now I could do this with a number of else if statements or possibly a switch statement however it will be too cubersome. All I'm asking is whether there are any alternative ways to solve this problem. I don't want the code I just require some guidance on how to go about it. Below is the method in question. Thanks for your help.

Java Code:
```public void getBaggage(int weight)
{
this.weight = weight;
if (weight<=20)
{
luggagecost = 0;
}
else if (weight>20 && weight<100)
{
// Here where I'm stuck
}```

2. Member
Join Date
Sep 2012
Posts
26
Rep Power
0

## Re: Alternatives to else if statement

How about calculating the fee: weight-20? If it's negative or zero -> no fee.
Else luggagecost = (weight-20)/5)*2.

3. Member
Join Date
Sep 2012
Posts
8
Rep Power
0

## Re: Alternatives to else if statement

Hey JavaAdviser thanks for your reply. Unfortunatately that is not what I need to do. What I meant in my earlier post was that for every 5 kg increase the passenger would have to pay and extra 2\$ i.e. a luggage weighing 22kg and a luggage weighing 24kg would still both cost 2\$.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Alternatives to else if statement

Java Code:
`luggagecost = (weight-20)/5)*2;`
What does JavaAdviser's suggested code yield for the four cases you mention?

22kg->\$2
24kg->\$2
27kg->\$4
36kg->\$8

Is it close?

 Also, the name of that method is a little odd. It's a void method so it doesn't get anything. What it does is set the weight (and calculate the excess baggage charge as a side effect) so setWeight() would be a better name.
Last edited by pbrockway2; 09-15-2012 at 02:45 AM.

5. Member
Join Date
Sep 2012
Posts
8
Rep Power
0

## Re: Alternatives to else if statement

Hey pbrockway2 thx for the input. Unfortunately if I were to apply JavaAdviser's code the results wont match. ((27-20)/5)*2 gives 2 (I'm using integer not double) and not 4. As I said I could circumvent this by using if/else statement but I was looking for a more efficient way. Thx and any further help is appreciated.
Last edited by gerchan; 09-15-2012 at 03:56 AM.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Alternatives to else if statement

Yes, the code would give \$2 not \$4 for the 27kg case.

At the risk of repeating myself :) ... what about the other three cases? They don't give the "correct" answer, but are they close? and is there some pattern to the answer the code gives and the answer you are looking for? (Hint: yes, and yes)

7. Member
Join Date
Sep 2012
Posts
8
Rep Power
0

## Re: Alternatives to else if statement

Hey pbrockway2 yes there was a way Here's my working code. At the end of the day it was all about the math. Thanks.

Java Code:
```public int getWeight(int weight)
{
if (weight<=20)
{
this.cost = 0;
}
else if(weight>20)
{
{
this.weight = (weight-20)/5;
}
if (this.weight ==0)

{
this.cost = 2;

}
else if (this.weight!=0)
{
this.cost = (this.weight+1)*2;
}

}
return this.cost;
}```

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Alternatives to else if statement

Great.

Note that this block of code which you have at the moment

Java Code:
```if (this.weight ==0)
{
this.cost = 2;
}
else if (this.weight!=0)
{
this.cost = (this.weight+1)*2;
}```
is identical, in effect, to this:

Java Code:
`this.cost = (this.weight+1)*2;`
In other words both branches of the if statement do the same thing.

-----

Make sure you have tested for lots of values. The "+1" business is true most of the time, but what about multiples of 5kg?

9. Member
Join Date
Sep 2012
Posts
8
Rep Power
0

## Re: Alternatives to else if statement

I was thinking about the multiples of 5 as well I took you advice and updated the code and its now working fine. However I'm not entirely sure whether it can be written in a more efficient way considering that I used the same this.weight = (weight-20)/5 twice. Other than that I've tested the code and everything should be ok. Thanks a lot for your help.

Java Code:
```public int getWeight(int weight)
{
if (weight<=20)
{
this.cost = 0;
}
else if(weight>20)
{
if ((((weight-20))%5)==0)
{
this.weight= (weight-20)/5;
this.cost = this.weight*2;
}
else
{
this.weight = (weight-20)/5;
this.cost = (this.weight+1)*2;
}

}
return this.cost;
}```

10. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Alternatives to else if statement

The most important thing is that you've tested it and it's correct. So mentally (or as a comment) mark it as "odd", but don't let it detain you if there are other aspects of the problem to work on.

-----

Compare "this.weight= (weight-20)/5 + 1;" with "this.weight= (weight-15-1)/5;". (I think! Quite untested and it's early Sunday morning here...)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•