rls1982 - 6 months ago 31

Java Question

I am writing a method which calculates the commission a company gives its vendor for a given product, however it is not executing. Please help. Here are the guidelines that I have to follow.

This function calculates the commision a company gives its vendor for a given product. The commission is calculated as follows (can safely assume amount is greater than 0):

if it is an annual license, the rates are:

- 10% if sale is between $0 and $1,000
- 15% if sale is between $1,001 and $10,000
- 20% if sale is above $10,000

if it is a forever license (not an annual one), the rates are:

- 10% if sale is between $0 and $10,000
- 15% if sale is between $10,001 and $50,000
- 20% if sale is above $50,000

I wrote the above logic in the following code:

`public static int commissionRate(boolean isAnnualLicense, int saleAmount)`

{

if (isAnnualLicense == true){

if (saleAmount <= 1000){

return commissionRate(true,10);

} else if (saleAmount >= 1001 && saleAmount <= 10000) {

return commissionRate(true,15);

} else if (saleAmount > 10001) {

return commissionRate(true,20);

} else {

if (isAnnualLicense == false) {

if (saleAmount >= 10000) {

return commissionRate(false,10);

} else if (saleAmount >= 100001 && saleAmount <= 50000) {

return commissionRate(false, 15);

} else if (saleAmount >= 50001) {

return commissionRate(false,20);

}

}

}

}

return 0;

}

This is what it is suppose to be executing:

`public void testCommission(){`

Assert.assertEquals("commision for annual sales, 1,000 should be 10",

10, Assignment3.commissionRate(true,1000));

Assert.assertEquals("commision for annual sales, 1,001 should be 15",

15, Assignment3.commissionRate(true,1001));

Assert.assertEquals("commision for annual sales, 10,000 should be 15",

15, Assignment3.commissionRate(true,10000));

Assert.assertEquals("commision for annual sales, 10,001 should be 20",

20, Assignment3.commissionRate(true,10001));

Assert.assertEquals("commision for OneTime sales, 10,000 should be 10",

10, Assignment3.commissionRate(false,10000));

Assert.assertEquals("commision for OneTime sales, 10,001 should be 15",

15, Assignment3.commissionRate(false,10001));

Assert.assertEquals("commision for OneTime sales, 50,000 should be 15",

15, Assignment3.commissionRate(false,50000));

Assert.assertEquals("commision for OneTime sales, 50,001 should be 20",

20, Assignment3.commissionRate(false,50001));

}

@Grade(points=25)

@Test

Any help is appreciated. Thanks in advance.

Answer

Your code will never reach the `isAnnualLicense == false`

part, because it is at the wrong nesting level. It should be like this:

```
if (isAnnualLicense){
if (saleAmount <= 1000){
return 10;
} else if (saleAmount >= 1001 && saleAmount <= 10000) {
return 15;
} else { // saleAmount > 10001
return 20;
}
} else {
if (saleAmount <= 10000) {
return 10;
} else if (saleAmount >= 100001 && saleAmount <= 50000) {
return 15;
} else { // saleAmount >= 50001
return 20;
}
}
```

You can further simplify this by assigning commision rate to a variable, and making a single `return`

at the bottom of your method:

```
int rate;
if (isAnnualLicense){
if (saleAmount <= 1000){
rate = 10;
} else if (saleAmount >= 1001 && saleAmount <= 10000) {
rate = 15;
} else { // saleAmount > 10001
rate = 20;
}
} else {
if (saleAmount <= 10000) {
rate = 10;
} else if (saleAmount >= 100001 && saleAmount <= 50000) {
rate = 15;
} else { // saleAmount >= 50001
rate = 20;
}
}
return rate;
```