Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2. /*
  3. Second Assignment
  4.  
  5. Write a script, module, application or program in PHP, which upon execution accepts
  6. two arguments. One of those arguments should be a "total cost" (in dollars and/or cents)
  7. and the other an "amount
  8. provided" (also in dollars and/or cents). Return as output the change that should be
  9. provided, by returning the count of each denomination of bills and/or coins. Extra
  10. points for object oriented and/or advanced concepts.
  11.  
  12. */
  13.  
  14. function makeChange($amount) {
  15. if ($amount >= 100) {
  16. $numBills = floor($amount / 100);
  17. echo "You get {$numBills} $100 bills.\n";
  18. $amountLeft = round($amount - ($numBills * 100), 2);
  19. if ($amountLeft > 0) {
  20. makeChange($amountLeft);
  21. }
  22. } elseif ($amount >= 50) {
  23. $numBills = floor($amount / 50);
  24. echo "You get {$numBills} $50 bills.\n";
  25. $amountLeft = round($amount - ($numBills * 50), 2);
  26. if ($amountLeft > 0) {
  27. makeChange($amountLeft);
  28. }
  29. } elseif ($amount >= 20) {
  30. $numBills = floor($amount / 20);
  31. echo "You get {$numBills} $20 bills.\n";
  32. $amountLeft = round($amount - ($numBills * 20), 2);
  33. if ($amountLeft > 0) {
  34. makeChange($amountLeft);
  35. }
  36. } elseif ($amount >= 10) {
  37. $numBills = floor($amount / 10);
  38. echo "You get {$numBills} $10 bills.\n";
  39. $amountLeft = round($amount - ($numBills * 10), 2);
  40. if ($amountLeft > 0) {
  41. makeChange($amountLeft);
  42. }
  43. } elseif ($amount >= 5) {
  44. $numBills = floor($amount / 5);
  45. echo "You get {$numBills} $5 bills.\n";
  46. $amountLeft = round($amount - ($numBills * 5), 2);
  47. if ($amountLeft > 0) {
  48. makeChange($amountLeft);
  49. }
  50. } elseif ($amount >= 1) {
  51. $numBills = floor($amount / 1);
  52. echo "You get {$numBills} $1 bills.\n";
  53. $amountLeft = round($amount - ($numBills * 1), 2);
  54. if ($amountLeft > 0) {
  55. makeChange($amountLeft);
  56. }
  57. } elseif ($amount >= .25) {
  58. $numBills = floor($amount / .25);
  59. echo "You get {$numBills} quarters.\n";
  60. $amountLeft = round($amount - ($numBills * .25), 2);
  61. if ($amountLeft > 0) {
  62. makeChange($amountLeft);
  63. }
  64. } elseif ($amount >= .10) {
  65. $numBills = floor($amount / .10);
  66. echo "You get {$numBills} dimes.\n";
  67. $amountLeft = round($amount - ($numBills * .10), 2);
  68. if ($amountLeft > 0) {
  69. makeChange($amountLeft);
  70. }
  71. } elseif ($amount >= .05) {
  72. $numBills = floor($amount / .05);
  73. echo "You get {$numBills} nickels.\n";
  74. $amountLeft = round($amount - ($numBills * .05), 2);
  75. if ($amountLeft > 0) {
  76. makeChange($amountLeft);
  77. }
  78. } elseif ($amount >= .01) {
  79. $numBills = floor($amount / .01);
  80. echo "You get {$numBills} pennies.\n";
  81. $amountLeft = round($amount - ($numBills * .01), 2);
  82. if ($amountLeft > 0) {
  83. makeChange($amountLeft);
  84. }
  85. }
  86. }
  87.  
  88. $options = getopt("u:d:");
  89.  
  90. if (!isset($options['u']) || (isset($options['u']) && $options['u'] == '')) {
  91. die("Invalid Parameters: expecting '-u [total cost] -d [amount provided]'\n");
  92. } else {
  93. $total = (float)$options['u'];
  94. if (!isset($options['d']) || (isset($options['d']) && $options['d'] == '')) {
  95. die("Invalid Parameters: expecting '-u [total cost] -d [amount provided]'\n");
  96. } else {
  97. $amountProvided = (float)$options['d'];
  98. }
  99. }
  100.  
  101. if ($amountProvided < $total) {
  102. $change = $total - $amountProvided;
  103. echo "Not Enough Provided. You Still Need To Give \${$change}.\n";
  104. } else if ($amountProvided == $total) {
  105. $change = 0;
  106. echo "No Change Is Required.\n";
  107. } else {
  108. $change = round($amountProvided - $total, 2);
  109. echo "You Get \${$change} in change.\n";
  110. makeChange($change);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement