Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Second Assignment
- Write a script, module, application or program in PHP, which upon execution accepts
- two arguments. One of those arguments should be a "total cost" (in dollars and/or cents)
- and the other an "amount
- provided" (also in dollars and/or cents). Return as output the change that should be
- provided, by returning the count of each denomination of bills and/or coins. Extra
- points for object oriented and/or advanced concepts.
- */
- function makeChange($amount) {
- if ($amount >= 100) {
- $numBills = floor($amount / 100);
- echo "You get {$numBills} $100 bills.\n";
- $amountLeft = round($amount - ($numBills * 100), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= 50) {
- $numBills = floor($amount / 50);
- echo "You get {$numBills} $50 bills.\n";
- $amountLeft = round($amount - ($numBills * 50), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= 20) {
- $numBills = floor($amount / 20);
- echo "You get {$numBills} $20 bills.\n";
- $amountLeft = round($amount - ($numBills * 20), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= 10) {
- $numBills = floor($amount / 10);
- echo "You get {$numBills} $10 bills.\n";
- $amountLeft = round($amount - ($numBills * 10), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= 5) {
- $numBills = floor($amount / 5);
- echo "You get {$numBills} $5 bills.\n";
- $amountLeft = round($amount - ($numBills * 5), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= 1) {
- $numBills = floor($amount / 1);
- echo "You get {$numBills} $1 bills.\n";
- $amountLeft = round($amount - ($numBills * 1), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= .25) {
- $numBills = floor($amount / .25);
- echo "You get {$numBills} quarters.\n";
- $amountLeft = round($amount - ($numBills * .25), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= .10) {
- $numBills = floor($amount / .10);
- echo "You get {$numBills} dimes.\n";
- $amountLeft = round($amount - ($numBills * .10), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= .05) {
- $numBills = floor($amount / .05);
- echo "You get {$numBills} nickels.\n";
- $amountLeft = round($amount - ($numBills * .05), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- } elseif ($amount >= .01) {
- $numBills = floor($amount / .01);
- echo "You get {$numBills} pennies.\n";
- $amountLeft = round($amount - ($numBills * .01), 2);
- if ($amountLeft > 0) {
- makeChange($amountLeft);
- }
- }
- }
- $options = getopt("u:d:");
- if (!isset($options['u']) || (isset($options['u']) && $options['u'] == '')) {
- die("Invalid Parameters: expecting '-u [total cost] -d [amount provided]'\n");
- } else {
- $total = (float)$options['u'];
- if (!isset($options['d']) || (isset($options['d']) && $options['d'] == '')) {
- die("Invalid Parameters: expecting '-u [total cost] -d [amount provided]'\n");
- } else {
- $amountProvided = (float)$options['d'];
- }
- }
- if ($amountProvided < $total) {
- $change = $total - $amountProvided;
- echo "Not Enough Provided. You Still Need To Give \${$change}.\n";
- } else if ($amountProvided == $total) {
- $change = 0;
- echo "No Change Is Required.\n";
- } else {
- $change = round($amountProvided - $total, 2);
- echo "You Get \${$change} in change.\n";
- makeChange($change);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement