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;


    }

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.