Advertisement
Guest User

Untitled

a guest
May 8th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. <! DOCTYPE html>
  2. <!--
  3.    
  4. -->
  5. <html>
  6. <head>
  7.     <title>Calculate Change</title>
  8. </head>
  9. <body>
  10.         <?php
  11.             // Creating the class and its members.
  12.             class CalculateChange {
  13.                 private $amountOwed;
  14.                 private $amountPaid;
  15.                 private $change = 0;
  16.                 private $changeDue;
  17.                 private $oneHundred = 0;
  18.                 private $fifty = 0;
  19.                 private $twenty = 0;
  20.                 private $ten = 0;
  21.                 private $five = 0;
  22.                 private $one = 0;
  23.                 private $quarter = 0;
  24.                 private $dime = 0;
  25.                 private $nickel = 0;
  26.                 private $penny = 0;
  27.                 private $errors = 1;
  28.                
  29.                 // Function that sets the amount owed.
  30.                 public function setOwed() {
  31.                     $this->amountOwed = isset($_POST['owed']) ? $_POST['owed'] : 0;
  32.                 }
  33.                 // Function that gets the amount owed.
  34.                 public function getOwed() {
  35.                     return $this->amountOwed;
  36.                 }
  37.                 // Function that sets the amount paid.
  38.                 public function setPaid() {
  39.                     $this->amountPaid = isset($_POST['paid']) ? $_POST['paid'] : 0;
  40.                 }
  41.                 // Function that gets the amount paid.
  42.                 public function getPaid() {
  43.                     return $this->amountPaid;
  44.                 }
  45.                
  46.                 public function validatePaid() {
  47.                     if ($this->getPaid() < $this->getOwed()) {
  48.                         ++$this->errors;
  49.                         echo "<p>The amount paid is less than the cost. Make sure the item is paid for!</p>\n";
  50.                     } else if ($this->getPaid() == $this->getOwed()) {
  51.                         ++$this->errors;
  52.                         echo "<p>They paid for the item in an exact amount. No change necessary!</p>\n";
  53.                     } else {
  54.                         --$this->errors;
  55.                         $changeDue = $this->amountPaid - $this->amountOwed;
  56.                         $change = $this->amountPaid - $this->amountOwed;
  57.                         $this->calculateChange();
  58.                     }
  59.                 }
  60.                
  61.                 public function calculateChange() {
  62.                 if ($this->errors == 0) {
  63.                     while ($this->change > 100) {
  64.                         $change -= 100;
  65.                         $oneHundred += 1;
  66.                     }
  67.                     while ($this->change > 50) {
  68.                         $change -= 50;
  69.                         $fifty += 1;
  70.                     }
  71.                     while ($this->change > 20) {
  72.                         $change -= 20;
  73.                         $twenty += 1;
  74.                     }
  75.                     while ($this->change > 10) {
  76.                         $change -= 10;
  77.                         $ten += 1;
  78.                     }
  79.                     while ($this->change > 5) {
  80.                         $change -= 5;
  81.                         $five += 1;
  82.                     }
  83.                     while ($this->change > 1) {
  84.                         $change -= 1;
  85.                         $one += 1;
  86.                     }
  87.                     while ($this->change > .25) {
  88.                         $change -= .25;
  89.                         $quarter += 1;
  90.                     }
  91.                     while ($this->change > .10) {
  92.                         $change -= .10;
  93.                         $dime += 1;
  94.                     }
  95.                     while ($this->change > .5) {
  96.                         $change -= .5;
  97.                         $nickel += 1;
  98.                     }
  99.                     while ($this->change > .1) {
  100.                         $change -= .1;
  101.                         $penny += 1;
  102.                     }
  103.                     echo "<p>The price of the transaction was $$this->amountOwed.</p>\n";
  104.                     echo "<p>The amount paid was $$this->amountPaid.</p>\n";
  105.                     echo "<p>The change due was $$this->changeDue.</p>\n";
  106.                     echo "<p>Return the following denominations as change.</p>\n";
  107.                     echo "<table border='1'>";
  108.                     echo "<th>Denomination</th><th>Amount</th>";
  109.                     echo "<tr><td>$100</td><td>$this->oneHundred</td>";
  110.                     echo "<tr><td>$50</td><td>$this->fifty</td>";
  111.                     echo "<tr><td>$20</td><td>$this->twenty</td>";
  112.                     echo "<tr><td>$10</td><td>$this->ten</td>";
  113.                     echo "<tr><td>$5</td><td>$this->five</td>";
  114.                     echo "<tr><td>$1</td><td>$this->one</td>";
  115.                     echo "<tr><td>$.25</td><td>$this->quarter</td>";
  116.                     echo "<tr><td>$.10</td><td>$this->dime</td>";
  117.                     echo "<tr><td>$.05</td><td>$this->nickel</td>";
  118.                     echo "<tr><td>$.01</td><td>$this->penny</td></tr>";
  119.                     echo "</table>";
  120.        
  121.                 }
  122.                
  123.             }
  124.         }
  125.         ?>
  126. </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement