From 453de17848b192be4f7bff6eeb6a91ea4fdd194b Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Fri, 8 Apr 2022 02:31:21 +0200 Subject: [PATCH] Week 3: Movie Class --- week-03/README.md | 35 +++++++++ week-03/homework/README.md | 113 +++++++++++++++++++++++++++++ week-03/homework/index.php | 143 +++++++++++++++++++++++++++++++++++++ 3 files changed, 291 insertions(+) create mode 100644 week-03/README.md create mode 100644 week-03/homework/README.md create mode 100644 week-03/homework/index.php diff --git a/week-03/README.md b/week-03/README.md new file mode 100644 index 0000000..541d7f3 --- /dev/null +++ b/week-03/README.md @@ -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)... + diff --git a/week-03/homework/README.md b/week-03/homework/README.md new file mode 100644 index 0000000..f6b6723 --- /dev/null +++ b/week-03/homework/README.md @@ -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 +

Movie Cost is: getCost(); ?>

+``` + diff --git a/week-03/homework/index.php b/week-03/homework/index.php new file mode 100644 index 0000000..f1789fe --- /dev/null +++ b/week-03/homework/index.php @@ -0,0 +1,143 @@ + + * @git 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; + +?> + + + + Movie Cost Calculator + + + + + + + + + 0) : ?> +
+

Movie Cost is: getCost(); ?>

+
+
+ +
+
+ +
+

Movie Cost Calculator

+
+
+
+ +
+ +
+
+
+ + +