A parking garage charges a \$2.00 minimum fee to park for up to three hours. After three hours, the garage charges an additional \$0.50 per hour. However, the maximum fee will be \$10.00. No one can park over 24 hours.

This is what I have:

``````public double calculate(double hours){
if(hours <= 3)
fee = 2.0;
else if(hours >= 3 & fee < 4)
fee= (2.0 +( .50*hours));
if(hours > 10)
fee = 10.0;
return fee;

}
``````
2
Contributors
1
17
Views
4 Years
Discussion Span
Last Post by Tarek_2

Hi,

There is my try :

``````public double calculate(double hours) throws Exception{

double fees = 0;
if(hours <= 3){
fees = 2.00;
}else{
if(hours > 3 && hours <= 24){
fees = 2.00 + (hours - 3) * (0.50);
if(fees >= 10)
fees = 10;
}else{
if(hours > 24)
throw new Exception("Max hours is 24");
}
}

return fees;

}
``````

So if hours is greater than 24, the method don't give back any value, instead, it will throw an Exception which can be treated in a try...catch.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.