im a java newbie and can’t seem to terminate my code wisely,here is my if else statement and what it does is just subtracts the other if statements,is there anyway i could do to subtract the availAmount to a specific if statement? thank you
double upgradeAccessories(double availAmount)
{
if(availAmount>21500)
{
hasAC=true;
availAmount-=21500;
}
else
{
hasAC=false;
}
if(availAmount>=14400)
{
hasLeatherSeats=true;
availAmount-=14400;
}
else
{
hasLeatherSeats=false;
}
if(availAmount>=6250)
{
hasBackWipers=true;
availAmount-=6250;
}
else
{
hasBackWipers=false;
}
if(availAmount>=3300)
{
hasFogLights=true;
availAmount-=3300;
}
else
{
hasFogLights=false;
}
return availAmount;
}
>Solution :
You can write:
double upgradeAccessories(double availAmount) {
hasAC = availAmount > 21500;
hasLeatherSeats = availAmount >= 14400;
hasBackWipers = availAmount >= 6250;
hasFogLights = availAmount >= 3300;
if (hasAC) return availAmount - 21500;
if (hasLeatherSeats) return availAmount - 14400;
if (hasBackWipers) return availAmount - 6250;
if (hasFogLights) return availAmount - 3300;
return availAmount;
}