Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <! DOCTYPE html>
- <!--
- -->
- <html>
- <head>
- <title>Calculate Change</title>
- </head>
- <body>
- <?php
- // Creating the class and its members.
- class CalculateChange {
- private $amountOwed;
- private $amountPaid;
- private $change = 0;
- private $changeDue;
- private $oneHundred = 0;
- private $fifty = 0;
- private $twenty = 0;
- private $ten = 0;
- private $five = 0;
- private $one = 0;
- private $quarter = 0;
- private $dime = 0;
- private $nickel = 0;
- private $penny = 0;
- private $errors = 1;
- // Function that sets the amount owed.
- public function setOwed() {
- $this->amountOwed = isset($_POST['owed']) ? $_POST['owed'] : 0;
- }
- // Function that gets the amount owed.
- public function getOwed() {
- return $this->amountOwed;
- }
- // Function that sets the amount paid.
- public function setPaid() {
- $this->amountPaid = isset($_POST['paid']) ? $_POST['paid'] : 0;
- }
- // Function that gets the amount paid.
- public function getPaid() {
- return $this->amountPaid;
- }
- public function validatePaid() {
- if ($this->getPaid() < $this->getOwed()) {
- ++$this->errors;
- echo "<p>The amount paid is less than the cost. Make sure the item is paid for!</p>\n";
- } else if ($this->getPaid() == $this->getOwed()) {
- ++$this->errors;
- echo "<p>They paid for the item in an exact amount. No change necessary!</p>\n";
- } else {
- --$this->errors;
- $changeDue = $this->amountPaid - $this->amountOwed;
- $change = $this->amountPaid - $this->amountOwed;
- $this->calculateChange();
- }
- }
- public function calculateChange() {
- if ($this->errors == 0) {
- while ($this->change > 100) {
- $change -= 100;
- $oneHundred += 1;
- }
- while ($this->change > 50) {
- $change -= 50;
- $fifty += 1;
- }
- while ($this->change > 20) {
- $change -= 20;
- $twenty += 1;
- }
- while ($this->change > 10) {
- $change -= 10;
- $ten += 1;
- }
- while ($this->change > 5) {
- $change -= 5;
- $five += 1;
- }
- while ($this->change > 1) {
- $change -= 1;
- $one += 1;
- }
- while ($this->change > .25) {
- $change -= .25;
- $quarter += 1;
- }
- while ($this->change > .10) {
- $change -= .10;
- $dime += 1;
- }
- while ($this->change > .5) {
- $change -= .5;
- $nickel += 1;
- }
- while ($this->change > .1) {
- $change -= .1;
- $penny += 1;
- }
- echo "<p>The price of the transaction was $$this->amountOwed.</p>\n";
- echo "<p>The amount paid was $$this->amountPaid.</p>\n";
- echo "<p>The change due was $$this->changeDue.</p>\n";
- echo "<p>Return the following denominations as change.</p>\n";
- echo "<table border='1'>";
- echo "<th>Denomination</th><th>Amount</th>";
- echo "<tr><td>$100</td><td>$this->oneHundred</td>";
- echo "<tr><td>$50</td><td>$this->fifty</td>";
- echo "<tr><td>$20</td><td>$this->twenty</td>";
- echo "<tr><td>$10</td><td>$this->ten</td>";
- echo "<tr><td>$5</td><td>$this->five</td>";
- echo "<tr><td>$1</td><td>$this->one</td>";
- echo "<tr><td>$.25</td><td>$this->quarter</td>";
- echo "<tr><td>$.10</td><td>$this->dime</td>";
- echo "<tr><td>$.05</td><td>$this->nickel</td>";
- echo "<tr><td>$.01</td><td>$this->penny</td></tr>";
- echo "</table>";
- }
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement