# Alternatives to else if statement

• 09-14-2012, 06:33 PM
gerchan
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.

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                 }```
• 09-14-2012, 07:14 PM
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.
• 09-15-2012, 01:36 AM
gerchan
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\$.
• 09-15-2012, 02:42 AM
pbrockway2
Re: Alternatives to else if statement
Quote:

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.
• 09-15-2012, 03:41 AM
gerchan
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.
• 09-15-2012, 05:09 AM
pbrockway2
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)
• 09-15-2012, 10:51 AM
gerchan
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.

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;         }```
• 09-15-2012, 11:03 AM
pbrockway2
Re: Alternatives to else if statement
Great.

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

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:

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?
• 09-15-2012, 02:07 PM
gerchan
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.

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;         }```
• 09-15-2012, 10:57 PM
pbrockway2
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...)