electr0mix Posted May 15, 2007 Share Posted May 15, 2007 The script would calculate the correct amount of change to return when performing a cash transaction. It must allow the user to enter two things: (1) the cost of the transaction and (2) the exact amount of money that the customer submits for the transaction. You script will determine the largest amount of each denomination to return to the customer. Assume that the largest denomination a customer will use to pay is $100. You will need to calculate the correct amount of change to return using: $50, $20, $10, $5 and $1 bills as well as quarters, dimes, nickels and pennies. (For example, if the price of a transaction is $7.33 and the customer gives a $10 bill to the cashier, the cashier should return $2.67. This would be 2 one dollar bills, two quarters, 1 dime, 1 nickels and 2 pennies. I was thinking about setting up 2 htlm pages. 1st one with two text boxes for input of: 1st HTML page: 1st box = totalprice 2nd box = totalcash then the PHP code using a switch statement that would use the remainder to break down the dollar amount. 2nd html page: Your change is: values from the PHP code. I think im heading in a right direction but I might need a little help. Do i need to use GET/POST method? Is switch statement ok? How and where would I declare all the values from $100 to pennies ... I feel a little lost with this one thats why im asking for help... thnx in advance ... Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted May 15, 2007 Share Posted May 15, 2007 Use the modulus operator to get what you want... $money_paid = 100; $price_of_goods = 54.37; $change = $money_paid - $price_of_goods; $fiftys = floor( $change % 50 ); $change = $change - ($fiftys * 50); $twentys = floor( $change % 20 ); $change = $change - ($twentys * 20); //etc for the other denominations of money..... //then echo out each denomination... Quote Link to comment Share on other sites More sharing options...
coolego1 Posted May 15, 2007 Share Posted May 15, 2007 Here's how I would set this up. Use a while loop. Get a bunch of if statements. Create variables $subtotal, $amtpaid, $amtowed. For keeping track of your denominations make variables $fifties, $twenties, $tens, $fives, $ones, $quarters, $dimes, $nickels, $pennies. These will be counters to keep track of how much is owed to the visitor. Code I would use goes like this: $amtowed = $amtpaid-$subtotal; while ($amtowed > 0) { //series of if statements begins here: if ($amtowed > 50) { // a fifty can be taken out $fifties++; $amtowed = $amtowed-50; } else if ($amtowed > 20) { // a twenty can be taken out $twenties++; $amtowed = $amtowed-20; } else if ($amtowed > 10) { // a ten can be taken out $tens++; $amtowed = $amtowed-10; } else if ($amtowed > 5) { // a five can be taken out $fives++; $amtowed = $amtowed-5; } else if ($amtowed > 1) { // a one can be taken out $ones++; $amtowed = $amtowed-1; } else if ($amtowed > .25) { // a quarter can be taken out $quarters++; $amtowed = $amtowed-.25; } else if ($amtowed > .10) { // a dime can be taken out $dimes++; $amtowed = $amtowed-.10; } else if ($amtowed > .05) { // a nickel can be taken out $nickels++; $amtowed = $amtowed-.05; } else if ($amtowed > .01) { // a penny can be taken out $pennies++; $amtowed = $amtowed-.01; } } After this, just display the counts for each of the denominations and you're good to go! Quote Link to comment Share on other sites More sharing options...
electr0mix Posted May 15, 2007 Author Share Posted May 15, 2007 Thnx for the ideas! Thats what i was thinking about: Im not really sure how to approach hitmans idea. I guess im more familiar with coolego's approach. I see what your a re suggesting - question: I see where $amtpaid and $subtotal come from and I know that i need $amtowed but where do i declare $amtowed? Do I even have to, or is it used only as an output variable? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 try <?php function change($tender, $price) { $denom = array(10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1); $change = ($tender - $price)*100; $result = array(); foreach ($denom as $v) { if ($change > $v) { $x = floor($change/$v); $result[$v] = $x; $change -= $v*$x; } } return $result; } $res = change(50, 7.33); foreach ($res as $d => $n) { printf ('$%0.2f - %d<br>', $d/100, $n); } ?> Quote Link to comment Share on other sites More sharing options...
electr0mix Posted May 15, 2007 Author Share Posted May 15, 2007 I have this so far: <html> <head> <title>Values</title> </head> <body bgcolor="#000000" text="#FFFFFF"> <form action="calc.php", METHOD=POST> <p align="center"> <b> Please enter the total amount and total cash then press Submit.</b> <p align="center"> <form> <p align="center">Total Price: <input type="text" name="tender" size="5" maxlength="5"> <br> Total cash: <input type="text" name="price" size="5" maxlength="5"> <p align="center"> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </p> </form> </body> </html> when the PHP code runs will I be able to see the output in a browser? Quote Link to comment Share on other sites More sharing options...
electr0mix Posted May 15, 2007 Author Share Posted May 15, 2007 as far as the PHP itself, im not sure what this is doing there: $res = change(50, 7.33); foreach ($res as $d => $n) { printf ('$%0.2f - %d<br>', $d/100, $n); } Quote Link to comment Share on other sites More sharing options...
electr0mix Posted May 15, 2007 Author Share Posted May 15, 2007 OK so if I use my html page and this script : <?php function change($tender, $price) { $denom = array(10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1); $change = ($tender - $price)*100; $result = array(); foreach ($denom as $v) { if ($change > $v) { $x = floor($change/$v); $result[$v] = $x; $change -= $v*$x; } } return $result; } $res = change(50, 7.33); foreach ($res as $d => $n) { printf ('$%0.2f - %d<br>', $d/100, $n); } ?> the output page in the browser gives the riught amount of bille - exacly what i was looking for but only for this amount ... $res = change(50, 7.33); how can i modify this code so it grabs values from the HTML form I made? Quote Link to comment Share on other sites More sharing options...
electr0mix Posted May 15, 2007 Author Share Posted May 15, 2007 i tried changing $res = change(50, 7.33); to: $result = ($tender, $price); and now it gives a blank screen... Quote Link to comment Share on other sites More sharing options...
electr0mix Posted May 15, 2007 Author Share Posted May 15, 2007 bump ... still not working :'( Quote Link to comment Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 try this <?php function change($tender, $price) { /** * function to calculate change * Returns array of currency denominations and how many of each */ $demom = array(5000, 2000, 1000, 500, 100, 25, 10, 5, 1); $change = ($tender - $price)*100; $result = array(); foreach ($demom as $v) { if ($change > $v) { $x = floor($change/$v); $result[$v] = $x; $change -= $v*$x; } } return $result; } /** * get form data and process */ if (isset($_POST['B1'])) { $tender = $_POST['tender']; $price = $_POST['price']; /** * pass values to function */ $res = change($tender, $price); /** * print results of the function */ echo '<table align="center" width="200" border="1">'; echo '<tr><th colspan="2">Change</th></tr>'; printf ('<tr><th colspan="2">$%0.2f</th></tr>',$tender-$price); foreach ($res as $d => $n) { printf ('<tr><td>$%0.2f</td><td>%d</td></tr>', $d/100, $n); } echo '</table><hr>'; } ?> <html> <head> <title>Values</title> </head> <body bgcolor="#000000" text="#FFFFFF"> <form action="", METHOD=POST> <p align="center"> Please enter the total amount and total cash then press Submit. <p align="center"> <form> <p align="center">Total Price: <input type="text" name="price" size="5" maxlength="5"> Total cash tendered: <input type="text" name="tender" size="5" maxlength="5"> <p align="center"> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </p> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
electr0mix Posted May 15, 2007 Author Share Posted May 15, 2007 Barand - you are truly a Genius Thank you - thats exactly what I was looking for. Seems the simple approach worked out the best - I can actually understand your code. Thank You again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.