Week 3: Movie Class

This commit is contained in:
Llewellyn van der Merwe 2022-04-08 02:31:21 +02:00
parent df358ee4a3
commit 453de17848
Signed by: Llewellyn
GPG Key ID: EFC0C720A240551C
3 changed files with 291 additions and 0 deletions

35
week-03/README.md Normal file
View File

@ -0,0 +1,35 @@
# Homework: Movie Class
Create a Movie class that determines the cost of a ticket to a cinema, based on the moviegoer's age. Assume that the cost of a full-price ticket is $10. Assign the age to a private data member. Use a public member function to determine the ticket price, based on the following schedule:
| Age | Price |
| --- | --- |
| Under 5 | Free |
| 5 to 17 | Half Price |
| 18 to 55 | Full Price |
| Over 55 | $2 off |
Make sure the assignment is done in a dynamic format. No database is needed, however, make sure that a user can appropriately enter their age into a textbox. Once the user submits their age, the appropriate prices will be displayed using the above table.
> See [homework](https://git.vdm.dev/champlain/WEBD-325-45/src/branch/master/week-03/homework)...
# Project: Logging In
This weeks assignment involves creating PHP scripts and associated staff/admin site web pages developed to accept username and passwords, verifying the user against information (secure/hashed password) in the database, recording failed login attempts, and either giving users access to the site or allowing them to try again. After five failed passwords the user session is terminated and access to their username is permanently blocked.
- Create PHP scripts and associated web pages developed to accept username and passwords
- Verify user information against the database
- Either give users access to the staff site or allow them to try again
User session begins on their first attempt to log in and continues if their credentials are accepted. After five failed passwords the user session is terminated and access to their username is permanently blocked.
**Submit the following three items to Canvas:**
- A login username and password (the encrypted version of the password should be stored in the database)
- A zip file of your entire staff website
- A dump of your database. You could use the [mysqldump](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html) (Links to an external site.)program or [phpMyAdmin](https://fragments.turtlemeat.com/mysql-database-backup-restore-phpmyadmin.php)
**Extra Credit:** If you build a "Request Account" feature so new users can email the admin to request a new account, you'll earn 2 extra credit points making this assignment worth a possible 17 points.
> See [project proposal](https://git.vdm.dev/champlain/WEBD-325-45/src/branch/master/week-03/project)...

113
week-03/homework/README.md Normal file
View File

@ -0,0 +1,113 @@
# Movie Class
> We have a basic interface
```php
// the movie interface
interface movieInterface
{
// get the cost in money notation
function getCost(): string;
}
```
> With a basic class that implement that interface so we can be sure we can call that class method `getCost()`.
```php
// the movie class
class Movie implements movieInterface
{
/**
* The moviegoer's age
*
* @var int
* @since 1.0.0
*/
private $age;
/**
* The currency symbol
*
* @var string
* @since 1.0.0
*/
protected $currency = '';
/**
* The movie prices
*
* @var array
* @since 1.0.0
*/
protected $prices;
/**
* Constructor
*
* @param int $age moviegoer's age
* @param array $prices the prices of the movies
* @param string $currency the current currency
*
* @since 1.0.0
*/
public function __construct(int $age, array $prices = [5 => 0, 17 => 5, 55 => 10], string $currency = '$')
{
// we set the age
$this->age = $age;
// The default prices
//----------------------
// Under 5 Free
// 5 to 17 Half Price
// 18 to 55 Full Price
// Over 55 $2 off
$this->prices = $prices;
// set the currency symbol
$this->currency = $currency;
}
/**
* Get the cost of this movie goer
*
* @return string the cost value
* @since 1.0.0
*
*/
public function getCost(): string
{
return $this->currency . number_format($this->getPrice(), 2);
}
/**
* Get the price of a movie by age
*
* @return int the movie price
*/
protected function getPrice(): int
{
foreach ($this->prices as $age => $cost)
{
if ($this->age <= $age)
{
return $cost;
}
}
return 8;
}
}
```
> We get the age from the global POST array:
```php
$age = (isset($_POST['age']) && is_numeric($_POST['age'])) ? (int) $_POST['age'] : null;
```
> Once we have the age submitted in the form we simple instantiate the class with the age and then call the `getCost()` method like this:
```php
<h2>Movie Cost is: <b><?php echo (new Movie($age))->getCost(); ?></b></h2>
```

143
week-03/homework/index.php Normal file
View File

@ -0,0 +1,143 @@
<?php
/**
* @package Movie Class
*
* @created 9th April 2022
* @author Llewellyn van der Merwe <https://git.vdm.dev/Llewellyn>
* @git WEBD-325-45 <https://git.vdm.dev/champlain/WEBD-325-45>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// the movie interface
interface movieInterface
{
// get the cost in money notation
function getCost(): string;
}
// the movie class
class Movie implements movieInterface
{
/**
* The moviegoer's age
*
* @var int
* @since 1.0.0
*/
private $age;
/**
* The currency symbol
*
* @var string
* @since 1.0.0
*/
protected $currency = '';
/**
* The movie prices
*
* @var array
* @since 1.0.0
*/
protected $prices;
/**
* Constructor
*
* @param int $age moviegoer's age
* @param array $prices the prices of the movies
* @param string $currency the current currency
*
* @since 1.0.0
*/
public function __construct(int $age, array $prices = [5 => 0, 17 => 5, 55 => 10], string $currency = '$')
{
// we set the age
$this->age = $age;
// The default prices
//----------------------
// Under 5 Free
// 5 to 17 Half Price
// 18 to 55 Full Price
// Over 55 $2 off
$this->prices = $prices;
// set the currency symbol
$this->currency = $currency;
}
/**
* Get the cost of this movie goer
*
* @return string the cost value
* @since 1.0.0
*
*/
public function getCost(): string
{
return $this->currency . number_format($this->getPrice(), 2);
}
/**
* Get the price of a movie by age
*
* @return int the movie price
*/
protected function getPrice(): int
{
foreach ($this->prices as $age => $cost)
{
if ($this->age <= $age)
{
return $cost;
}
}
return 8;
}
}
// check if we have an age value
$age = (isset($_POST['age']) && is_numeric($_POST['age'])) ? (int) $_POST['age'] : null;
?><html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Movie Cost Calculator</title>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.13.7/dist/css/uikit.min.css" />
<!-- UIkit JS -->
<script src="https://cdn.jsdelivr.net/npm/uikit@3.13.7/dist/js/uikit.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/uikit@3.13.7/dist/js/uikit-icons.min.js"></script>
</head>
<body>
<?php if ($age > 0) : ?>
<div class="uk-container">
<h2>Movie Cost is: <b><?php echo (new Movie($age))->getCost(); ?></b></h2>
<form method="post">
<fieldset class="uk-fieldset">
<input class="uk-button" type="submit" value="Try Another"/>
</fieldset>
</form>
<?php else: ?>
<div class="uk-container">
<h2>Movie Cost Calculator</h2>
<form method="post">
<fieldset class="uk-fieldset">
<div class="uk-margin">
<label>
<input name="age" class="uk-input" type="text" placeholder="Enter Your Age">
</label>
</div>
<input class="uk-button" type="submit" value="Calculate"/>
</fieldset>
</form>
</div>
<?php endif; ?>
</body>
</html>