The "pay()" function does not return the correct result

Asked

Viewed 443 times

-5

I have the following statement:

A parking lot charges a minimum fee of R$2.00 to park by three hours. An additional R$0.50 per hour does not necessarily is charged after the first three hours. The maximum amount for any given 24-hour period is $10.00. Suppose no car is parked for more than 24 hours at a time. Write an app which calculates and displays the parking fees for each customer who parked in this garage yesterday. You must enter the hours of parking for each customer. The program must display the charge for the current customer and calculate and display the total received at the end of the day. The program must use a pathValue function to determine the charge for each customer.

That’s what I tried to do:

   <?php 
    if ($_POST) {
        $t = $_POST['num-hor'];
        $total = valorApagar($t);
        echo"<br><br>Total: R$".number_format($total, 2, ',', '.');

    }
    function valorApagar($t){
        $total = 0;

        if ($t <= 3) {
            $total = 2;
        }else if ($t > 3 && $t < 24) {
            $total = 2; 
            $t -= 3;
            if(is_int($t) == true){
                $total += $t;
            }else{
                $t = ceil($t);
                $t *= 0.5;
                $total += $t;
            }
        }else {
            $total = 10;
        }

        return $total;
    }
 ?>

The function valorApagar() is not returning me the correct result, how to adjust it to return the result according to what is requested in the statement?

  • 5

    What’s the matter?

2 answers

4

The problem is much more mathematical than programming. Moreover the question is not clear about the problem, making it almost a service outsourcing. But as it is easy and simple to solve I will answer, even though maybe I shouldn’t.

You should just use:

$total = 2 + ((ceil($t) - 3)  * 0.5);

But there are other errors in PHP. Your question says that "per hour not necessarily entire", so if someone parks for 23 hours and 10 minutes will pay for 24 hours?! In my understanding this is it.

Therefore, by correcting everything:

if(isset($_POST['num-hor'])){
    echo 'Total: R$'.(number_format( valorApagar( $_POST['num-hor'] ), 2, ',', '.'));
}

function valorApagar($t){
    $t = ceil($t);

    if($t <= 3){
        $total = 2;
    }else if ($t > 3 && $t < 24) {  
        $total = 2 + (($t - 3)  * 0.5); 
    }else {
        $total = 10;
    }

    return $total;
}

Test it out here.

Upshot:

0 horas custam R$2,00
1 horas custam R$2,00
2 horas custam R$2,00
3 horas custam R$2,00
4 horas custam R$2,50
5 horas custam R$3,00
6 horas custam R$3,50
7 horas custam R$4,00
8 horas custam R$4,50
9 horas custam R$5,00
10 horas custam R$5,50
11 horas custam R$6,00
12 horas custam R$6,50
13 horas custam R$7,00
14 horas custam R$7,50
15 horas custam R$8,00
16 horas custam R$8,50
17 horas custam R$9,00
18 horas custam R$9,50
19 horas custam R$10,00
20 horas custam R$10,50
21 horas custam R$11,00
22 horas custam R$11,50
23 horas custam R$12,00
24 horas custam R$10,00
25 horas custam R$10,00

Old output (your code):

0 horas custam R$2,00
1 horas custam R$2,00
2 horas custam R$2,00
3 horas custam R$2,00
4 horas custam R$3,00
5 horas custam R$4,00
6 horas custam R$5,00
7 horas custam R$6,00
8 horas custam R$7,00
9 horas custam R$8,00
10 horas custam R$9,00
11 horas custam R$10,00
12 horas custam R$11,00
13 horas custam R$12,00
14 horas custam R$13,00
15 horas custam R$14,00
16 horas custam R$15,00
17 horas custam R$16,00
18 horas custam R$17,00
19 horas custam R$18,00
20 horas custam R$19,00
21 horas custam R$20,00
22 horas custam R$21,00
23 horas custam R$22,00
24 horas custam R$10,00
25 horas custam R$10,00

Results obtained using:

for($i = 0; $i < 26; $i++){
    echo $i.' horas custam R$'.(number_format( valorApagar( $i ), 2, ',', '.'))."\n";
}
  • How would it look if the function received separate time of entry and minute of entry and also time and minute of exit?

3

Follow a "short" version only to complement the post.

I did it based on reply from @inkeliz (which, by the way, is much more didactic for those who are learning, and has already taken my +1):

function valorAPagar($t) {
    return min( 10, max( 2, ceil( $t ) / 2 + .5 ) );
};
  • we use only the formula ceil( $t ) / 2 + .5, since the result is linear.
    ( It’s just a simplification of 2 + ( ($t - 3) * 0.5 ) )

  • the min( 10, valor ) makes it no more than 10

  • likewise, max( 2, valor ) makes the value at least 2

See a loop test on Online PHP Functions.

  • Okay, I really like "one line". [+1]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.