In: Computer Science
You will write a PHP program to calculate the payment
receipt for renting a number of movies, using a
conditional structure.
Suppose that the rental rate depends on the number of movies rented
by a customer at a time. There is a limit of renting a maximum of
20 movies at a time.
Conditions: For the first 2 movies rented, the rate is $5.50/movie.
For the next 2 movies rented, the rate is $4.25/movie. For the next
3 movies rented, the rate is $3.00/movie. For any more movies
rented (no more than 20), the rate is $2.00/movie.
Hence, if a customer rents 5 movies, two of those will be rented at
$5.50, two for $4.25 and one for $3.00, for a total of (2*$5.50) +
(2*$4.25) + $3.00 = $22.50.
1) Create a form that asks the customer for his/her name and the
number of movies he/she would like to rent. On submit, display the
total amount due (shown to 2 decimal places) on the same page. (15
points) 2) Save the customer’s submitted name in a flat file called
“visitor.txt”. You should overwrite this name every time someone
submits a name to the form. (10 points)
Give appropriate error or warning messages for both 1) and
2).
Screenshot of the Code:
Sample Output:
Output File:
Code to Copy:
<html>
<title>
Movie Rental
</title>
<head>
</head>
<body>
<h2 align="center">Movie Rental Form</h2>
<form action="" method="post">
<table align="center" border="4px">
<tr>
<td>Name: </td>
<td><input type="text" name="userName"/>
</tr>
<tr>
<td>Number of Movies to be Rented: </td>
<td><input type="text" name="numMoive"/>
</tr>
<tr colspan=>
<td colspan="4" align="center">
<input type="submit" value="Submit" name="submit"/>
</td>
</tr>
</table>
<p id = "demo"></p>
</form>
<?php
//On submit click
//get the name and number of movies.
if(isset($_POST['submit']))
{
$userName=$_POST['userName'];
$numberMoives=$_POST['numMoive'];
//Define the variable.
$cost=0;
//Calculate the cost as per the number of movies.
if($numberMoives<=20)
{
if($numberMoives<=2)
$cost+=$numberMoives*5.50;
else if($numberMoives<=4)
{
$cost=11.00;
$cost+=($numberMoives-2)*4.25;
}
else if($numberMoives<=7)
{
$cost=19.50;
$cost+=($numberMoives-4)*3.00;
}
else
{
$cost=28.50;
$cost+=($numberMoives-7)*2.00;
}
//Display the results.
echo "<h2 align='center'>".$userName." your total amount due: $".number_format($cost, 2)."</h2>";
//Create a file.
//Display the error message if the program is unable to create the file.
$visitorFile = fopen("visitor.txt", "w") or die("Unable to create the file!");
//Write the content in the file.
fwrite($visitorFile, $userName);
//Close the file.
fclose($visitorFile);
}
else
{
//Display the message for invalid input.
$msg = "you cannot rent more than 20 movies.";
echo "<script type='text/javascript'>alert('$msg');</script>";
}
}
?>
</body>
</html>