TOA Posted September 19, 2011 Share Posted September 19, 2011 I need to know what change to return when making a purchase.. Is there a mathematical formula or something for this? Ex: The change from a purchase is 95 cents. I need to know that it should be 3 quarters and two dimes that should be returned. Thanks for reading Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 19, 2011 Share Posted September 19, 2011 Are you trying to calculate just the actual coins that need to be given, or all of the change the customer will receive in the event that the change amount is more than 1.00? Quote Link to comment Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 Hope this helps <?php // Should be largest to smallest. We use the value * 100 to avoid bad float precision $currency = array( '20 Bill' => 2000, '10 Bill' => 1000, '5 Bill' => 500, 'Toonie' => 200, // I'm Canadian 'Loonie' => 100, 'Quarter' => 25, 'Dime' => 10, 'Nickel' => 5, 'Penny' => '1' ); $cost = 11.07; $cashGiven = 20.00; $change = ($cashGiven*100) - ($cost*100); $changeArray = array(); // Will store the change to give. echo 'Cost was: '.$cost.'<br>'; echo 'Amount given was: '.$cashGiven.'<br>'; echo 'Change was: '.($change/100).'<br>'; // Loop through our currency, greatest to least foreach( $currency as $name => $value ) { $num = 0; // How many of the given currency to give if( $change >= $value ) { // Check if change still to be given is more than the currency while( $change >= $value ) { // Loop through until it's less $num++; // Increase the amount of currency to give $change -= $value; // subtract value from currency } } $changeArray[$name] = $num; // Add the amount to our change array } echo 'Currency to give back:<br>'; // Loop through the change we need to give foreach( $changeArray as $name => $amount ) { if( $amount ) {// Make sure amount isn't 0 echo $amount.' '.$name; echo ($amount > 1 ? 's' : '').'<br>'; // if $amount > 1 add an s to the end of $name } } ?> Bad float precision can be show in this example. If you change the values to their decimal equivalents, you will only get two pennies back. The remaining change will be '0.0099999999999997'. If you want to read more about it, here's a summary: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems Quote Link to comment Share on other sites More sharing options...
TOA Posted September 19, 2011 Author Share Posted September 19, 2011 Are you trying to calculate just the actual coins that need to be given, or all of the change the customer will receive in the event that the change amount is more than 1.00? I only thought about it in change, so maybe I should just start there Bad float precision can be show in this example. If you change the values to their decimal equivalents, you will only get two pennies back. The remaining change will be '0.0099999999999997'. If you want to read more about it, here's a summary: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems I think it will work without decimals. I'll look into it, thanks. And thanks for the script, I'll see what it does shortly Quote Link to comment Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 I've shown you how to make it work without decimals. You simply multiply both the COST OF ITEM and AMOUNT GIVEN by 100 before subtracting them. $cost = 11.07; $cashGiven = 20.00; $change = ($cashGiven*100) - ($cost*100); When you want to display in decimal again, simply divide by 100. echo 'Change was: '.($change/100).'<br>'; Quote Link to comment Share on other sites More sharing options...
TOA Posted September 19, 2011 Author Share Posted September 19, 2011 I've shown you how to make it work without decimals. You simply multiply both the COST OF ITEM and AMOUNT GIVEN by 100 before subtracting them. $cost = 11.07; $cashGiven = 20.00; $change = ($cashGiven*100) - ($cost*100); When you want to display in decimal again, simply divide by 100. echo 'Change was: '.($change/100).'<br>'; Oh, no, I was referring to the article; I'm using it in a very simple way, so I don't think my idea requires decimals. Maybe I just didn't understand what you meant Actually, I just ran it and it's exactly what I was needing, major thanks. I thought I was familiar with your "funny money" up there but never heard of a toonie. My family vacations in the Whiteshell almost every year.. Quote Link to comment Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 Two dollar coin. So much nicer than having a bill for such a small amount. Now we just have to be rid of the penny and things will be great The name on the other hand... jeez Quote Link to comment Share on other sites More sharing options...
TOA Posted October 3, 2011 Author Share Posted October 3, 2011 How could I get this script to use what the person has in their pocket? I've tried a bunch of things and could post about 9 butchered versions, but I thought it might be easier to start from square one to get help. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 3, 2011 Author Share Posted October 3, 2011 How could I get this script to use what the person has in their pocket? I've tried a bunch of things and could post about 9 butchered versions, but I thought it might be easier to start from square one to get help. Let me clarify..I need to check for what the "register" would have. Say the purchase is 45 and they give me a fifty. What this script does (correctly) is to return a five. What if I don't have a five? So I need to throw another dimension into $currency that has the number of each that I have to return. Hope that makes sense. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 4, 2011 Author Share Posted October 4, 2011 So I have a version of this code working with a multidimensional array that adds a "count". So essentially, it tells me how many of each currency I have to work with. I think I just need to know where in the first foreach loop I need to check for the count and if they don't have any more of that currency, to go on to the next smaller denomination. Hope that makes sense <?php $currency = array( '20 Bill' => array('value'=>2000, 'count' => 0), '10 Bill' => array('value'=>1000, 'count' => 0), '5 Bill' => array('value'=>500, 'count' => 0), 'Toonie' => array('value'=>200, 'count' => 0),// I'm Canadian 'Loonie' => array('value'=>100, 'count' => 0), 'Quarter' => array('value'=>25, 'count' => 6), 'Dime' => array('value'=>10, 'count' => 3), 'Nickel' => array('value'=>5, 'count' => 5), 'Penny' => array('value'=>1, 'count' => 10)); $cost = 11.07; $cashGiven = 20.00; $change = ($cashGiven*100) - ($cost*100); $changeArray = array(); // Will store the change to give. echo 'Cost was: '.$cost.'<br>'; echo 'Amount given was: '.$cashGiven.'<br>'; echo 'Change was: '.($change/100).'<br>'; // Loop through our currency, greatest to least foreach( $currency as $name => $value ) { $num = 0; // How many of the given currency to give if( $change >= $currency[$name]['value'] ) { // Check if change still to be given is more than the currency while( $change >= $currency[$name]['value']) { // Loop through until it's less $num++; // Increase the amount of currency to give $change -= $currency[$name]['value']; // subtract value from currency } } $changeArray[$name] = $num; // Add the amount to our change array } echo 'Currency to give back:<br>'; // Loop through the change we need to give foreach( $changeArray as $name => $amount ) { if( $amount ) {// Make sure amount isn't 0 echo $amount.' '.$name; echo ($amount > 1 ? 's' : '').'<br>'; // if $amount > 1 add an s to the end of $name } } ?> Quote Link to comment Share on other sites More sharing options...
TOA Posted October 4, 2011 Author Share Posted October 4, 2011 Ok, as far as I can tell, this does it foreach( $currency as $name => $value ) { $num = 0; // How many of the given currency to give if ($currency[$name]['count'] > $num) { if( $change >= $currency[$name]['value'] ) { // Check if change still to be given is more than the currency while( $change >= $currency[$name]['value']) { // Loop through until it's less $num++; // Increase the amount of currency to give $change -= $currency[$name]['value']; // subtract value from currency } } } $changeArray[$name] = $num; // Add the amount to our change array } Let me know if anyone sees any flagrant issues Ok, nevermind, I already encountered an issue. Use 1 Toonie and 8 loonies for counts and it gets some weird results. GDI! Quote Link to comment Share on other sites More sharing options...
TOA Posted October 4, 2011 Author Share Posted October 4, 2011 Ok, I believe this is correct <?php $currency = array( '20 Bill' => array('value'=>2000, 'count' => 0), '10 Bill' => array('value'=>1000, 'count' => 0), '5 Bill' => array('value'=>500, 'count' => 0), 'Toonie' => array('value'=>200, 'count' => 0),// I'm Canadian 'Loonie' => array('value'=>100, 'count' => 6), 'Quarter' => array('value'=>25, 'count' => 15), 'Dime' => array('value'=>10, 'count' => 0), 'Nickel' => array('value'=>5, 'count' => 5), 'Penny' => array('value'=>1, 'count' => 10) ); $cost = 11.07; $cashGiven = 20.00; $change = ($cashGiven*100) - ($cost*100); $changeArray = array(); // Will store the change to give. echo 'Cost was: '.$cost.'<br>'; echo 'Amount given was: '.$cashGiven.'<br>'; echo 'Change was: '.($change/100).'<br>'; // Loop through our currency, greatest to least foreach( $currency as $name => $value ) { $num = 0; // How many of the given currency to give if( $change >= $currency[$name]['value'] ) { // Check if change still to be given is more than the currency while( $change >= $currency[$name]['value'] && $currency[$name]['count'] > $num) { // Loop through until it's less <-- this is where I added the count check $num++; // Increase the amount of currency to give $change -= $currency[$name]['value']; // subtract value from currency } } $changeArray[$name] = $num; // Add the amount to our change array } echo 'Currency to give back:<br>';// Loop through the change we need to give foreach( $changeArray as $name => $amount ) { if( $amount ) {// Make sure amount isn't 0 echo $amount.' '.$name; echo ($amount > 1 ? 's' : '').'<br>'; // if $amount > 1 add an s to the end of $name } } ?> I've tested it with different counts and I think it's right this time. Let me know if anyone see's problems Quote Link to comment Share on other sites More sharing options...
xyph Posted October 5, 2011 Share Posted October 5, 2011 I'm really glad you attempted it yourself! And figured it out as well. I don't feel so bad just handing you a solution in a class I've made. I designed a cash register application for a local chain here, and I've ported it over to PHP, taking out the debit/credit garbage. I hope I've commented it enough. Let me know if there's anything I can help you with. <?php /* A few terms I've used *Currency - A type of currency the script will accept (ie $20 Bill) *$name or $n - The name of the specific currency the script is dealing with *$amount or $a - The amount of the specific currency *$total or $t - The float version of a given array of amounts (21.45) *scaled total - The integer version of that total (2145) */ // We'll start a try->catch block, because I love using exception for error handling try { // $scale in the __construct lets us know how many decimals we are dealing with. The script multiplies all values inserted to remove decimals before // doing any math. This makes sure we don't get messed up due to bad float math. $till = new wallet( 2 ); // Let's define the currencies this till will handle, along with their values. This could be hard coded right into the class $currency = array( '$20 Bill' => 20, '$10 Bill' => 10, '$5 Bill' => 5, 'Toonie' => 2, 'Loonie' => 1, 'Quarter' => 0.25, 'Dime' => 0.1, 'Nickel' => 0.05, 'Penny' => 0.01 ); // Now we'll let our till know about them and make sure no errors happened if( $till->addCurrency($currency) === FALSE ) throw new Exception('Could not add currency to wallet - '.$till->error); // Now that we have currency, let's fill our till with it! $amounts = array( '$20 Bill' => 5, '$10 Bill' => 7, '$5 Bill' => 10, 'Toonie' => 25, 'Loonie' => 25, 'Quarter' => 35, 'Dime' => 40, 'Nickel' => 40, 'Penny' => 60 ); if( $till->putArrayIn($amounts) === FALSE ) throw new Exception('Could not put amounts in as an array - '.$till->error); // Or if we jsut wanted to throw $75.19 in the till without caring what currencies go in if( $till->putTotalIn(75.19) === FALSE ) throw new Exception('Could not put amounts in as total - '.$till->error); // Now for some fun. First, let's see how much currency our till is holding echo '<h3>Amounts and Total in till before transaction</h3>'; echo '<pre>'; print_r( $till->getArrayInWallet() ); echo 'Total: '.$till->getTotalInWallet().'</pre>'; // Okay, now let's perform a transaction. if( ($change = $till->transaction(43.77,50)) === FALSE ) throw new Exception('Could not perform basic transaction - '.$till->error); // Now let's see the change we gave out and how our till has changed echo '<h3>Amount of change given in basic transaction</h3>'; echo '<pre>'; print_r( $change ); echo 'Total: '.$till->getTotalFromArray($change).'</pre>'; echo '<h3>Amount in till after basic transaction</h3>'; echo '<pre>'; print_r( $till->getArrayInWallet() ); echo 'Total: '.$till->getTotalInWallet().'</pre>'; // Neat huh? Now let's perform an advanced transaction. We will specify what kind of currency is being // used to pay, and optionally see if it's possible to return certain change $payment = array( '$20 Bill'=>2, '$10 Bill'=>1, '$5 Bill'=>6, 'Toonie'=>5 ); // Should be $90 $changeWanted = array('Quarter'=>6,'Loonie'=>7); // Get at least 6 quarters and 7 loonies back if possible if( ($change = $till->transaction(78.41, $payment, $changeWanted)) === FALSE ) throw new Exception('Could not perform advanced transaction - '.$till->error); // And now we'll view our change and how the till has changed echo '<h3>Amount of change given in advanced transaction</h3>'; echo '<pre>'; print_r( $change ); echo 'Total: '.$till->getTotalFromArray($change).'</pre>'; echo '<h3>Amount in till after advanced transaction</h3>'; echo '<pre>'; print_r( $till->getArrayInWallet() ); echo 'Total: '.$till->getTotalInWallet().'</pre>'; // Sweet! } catch ( Exception $e ) { echo 'Error! '.$e->getMessage(); } class wallet { public $amount = array(), $currency = array(), $scale, $decimals, $error = FALSE; public function __construct( $scale = 2 ) { $this->decimals = $scale; $this->scale = pow( 10, $scale ); } public function addCurrency( $array ) { // Make sure $array is actually an array if( is_array($array) === FALSE ) { $this->error = 'addCurrency() - Non-array value entered'; return FALSE; } // Loop through values and make sure they're valid before adding. This saves us from having to backtrack if an error happens half way // through the loop. foreach( $array as $n => &$value ) if( ($value = $this->isValidTotal($value)) === FALSE ) return FALSE; // No errors happened, we can now loop through a second time, entering values foreach( $array as $n => $v ) $this->currency[$n] = $v; arsort($this->currency); } public function putArrayIn( $array ) { // Make sure $array is actually an array if( is_array($array) === FALSE ) { $this->error = 'putArrayIn() - Non-array value entered'; return FALSE; } // Loop through amounts and make sure they're valid before inserting. This saves us from having to backtrack if an error happens half way // through the loop. foreach( $array as $n => $a ) if( $this->isValidCurrency($n) === FALSE || $this->isValidAmount($a) === FALSE ) return FALSE; // No errors happened, we can now loop through a second time, entering amounts foreach( $array as $n => $a ) { // If $this->amount[$n] is undefined, using += on it will throw a Notice, so we check before doing that $this->amount[$n] = isset( $this->amount[$n] ) ? $this->amount[$n] + $a : $a; } return TRUE; } public function putTotalIn( $total ) { if( ($array = $this->getArrayFromTotal($total)) === FALSE ) return FALSE; return $this->putArrayIn( $array ); } public function takeArrayOut( $array ) { // Make sure $array is actually an array if( is_array($array) === FALSE ) { $this->error = 'takeArrayOut() - Non-array value entered'; return FALSE; } // Loop through amounts and make sure they're valid before inserting. This saves us from having to backtrack if an error happens half way // through the loop. foreach( $array as $n => $a ) { if( $this->isValidCurrency($n) === FALSE || $this->isValidAmount($a) === FALSE ) return FALSE; if( isset($this->amount[$n]) === FALSE || $a > $this->amount[$n] ) { $this->error = 'takeArrayOut() - Tried to take out more "'.$n.'" than available'; return FALSE; } } // No errors happened, we can now loop through a second time, removing amounts foreach( $array as $n => $a ) { $this->amount[$n] -= $a; } return TRUE; } public function takeTotalOut( $total, $tryToInclude = FALSE ) { // Verify our total is valid, and covert to scaled total if( ($total = $this->isValidTotal($total)) === FALSE ) return FALSE; return $this->takeTotalOutScaled( $total, $tryToInclude ); } protected function takeTotalOutScaled( $total, $tryToInclude = FALSE ) { // Verify the total to take out doesn't exceed the total available if( $total > $this->getTotalFromArrayScaled($this->amount) ) { $this->error = 'takeTotalOut() - $total exceeds available total'; } // This will hold the amounts to pass to takeArrayOut() $takeOut = array(); // Check if there's any specific currencies wanted if( is_array($tryToInclude) ) { // Loop through currencies foreach( $tryToInclude as $n => $a ) { if( $this->isValidCurrency($n) === FALSE || $this->isValidAmount($a) === FALSE ) return FALSE; // If an amount hasn't been set for the currency, or there's none available we can skip if( isset($this->amount[$n]) === FALSE || $this->amount[$n] == 0 ) continue; // Otherwise, loop while $total more than or equal the value demanded, supply still exists and demand hasn't been met while( $total >= $this->currency[$n] && ((isset($takeOut[$n]) === FALSE && $this->amount[$n] > 0) || (isset($takeOut[$n]) && $takeOut[$n] <= $this->amount[$n])) && $a > 0 ) { $a--; $takeOut[$n] = isset( $takeOut[$n] ) ? $takeOut[$n] + 1 : 1; $total -= $this->currency[$n]; } } } // Now that any specific currencies we can include are in the $takeOut array, we can figure out what's needed for the rest of the change foreach( $this->currency as $n => $v ) { // Loop while $total is more than or equal the value of this currency and supply still exists // The isset calls make sure we don't get any undefined offset notices while( $total >= $v && ((isset($takeOut[$n]) === FALSE && $this->amount[$n] > 0) || (isset($takeOut[$n]) && $takeOut[$n] <= $this->amount[$n])) ) { $takeOut[$n] = isset( $takeOut[$n] ) ? $takeOut[$n] + 1 : 1; $total -= $v; } } // Now that our array is complete, we can pass that off to our function that handles it, and return whatever values it returns if( $this->takeArrayOut( $takeOut ) === FALSE ) return FALSE; return $takeOut; } public function transaction( $cost, $payment, $tryToInclude = FALSE ) { // Check if a total was given, and turn it into an array of currency if( is_array($payment) === FALSE && ($payment = $this->getArrayFromTotal( $payment )) === FALSE ) return FALSE; // Check if $cost is valid, and convert to scaled total if( ($cost = $this->isValidTotal($cost)) === FALSE ) return FALSE; // Verify that enough payment was given if( ($paymentTotal = $this->getTotalFromArrayScaled($payment)) < $cost ) { $this->error = 'transaction() - $payment was invalid or less than $cost'; return FALSE; } // Attempt to finalize payment - any failures from here SHOULD revert this action if( $this->putArrayIn($payment) === FALSE ) return FALSE; // Calculate change required and attempt to take it out if( ($change = $this->takeTotalOutScaled($paymentTotal-$cost, $tryToInclude)) === FALSE ) { $this->takeArrayOut($payment); return FALSE; } // Make sure changed received was correct - if something like pennies ran out, the script would short change. if( $this->getTotalFromArrayScaled($change) != ($paymentTotal - $cost) ) { $this->error('transaction() - Unable to give correct change, minimum currency is unavailable or undefined'); $this->takeArrayOut($payment); return FALSE; } return $change; } public function getArrayInWallet() { return $this->amount; } public function getTotalInWallet() { return $this->getTotalFromArray( $this->amount ); } public function getTotalFromArray( $array ) { return sprintf( '%01.'.$this->decimals.'f', $this->getTotalFromArrayScaled($array) / $this->scale ); } protected function getTotalFromArrayScaled( $array ) { $total = 0; foreach( $array as $n => $a ) { if( $this->isValidCurrency($n) === FALSE ) return FALSE; if( $this->isValidAmount($a) === FALSE ) return FALSE; $total += $this->currency[$n] * $a; } return $total; } public function getArrayFromTotal( $total ) { if( ($total = $this->isValidTotal($total)) === FALSE ) return FALSE; return $this->getArrayFromTotalScaled( $total ); } protected function getArrayFromTotalScaled( $total ) { foreach( $this->currency as $n => $v ) { while( $total >= $v ) { $return[$n] = isset( $return[$n] ) ? $return[$n] + 1 : 1; $total -= $v; } } return $return; } public function error() { return $this->error; } protected function isValidTotal( $total ) { if( ctype_digit((string)$total=($total*$this->scale)) === FALSE ) { $this->error = 'isValidTotal() - Total is too precise'; return FALSE; } return $total; } protected function isValidCurrency( $name ) { if( isset($this->currency[$name]) === FALSE ) { $this->error = 'isValidCurrency() - Unknown currency specified'; return FALSE; } return TRUE; } protected function isValidAmount( $amount, $allow_neg = FALSE ) { // This is a neat little snippet. It checks to see if a given variable is an integer, or a string that can be evaluated as an integer // It checks if either the (string) evaluation of $amount is all digits, or ( the first character of (string)$amount is '-' and the // rest are digits ). if( $allow_neg && (ctype_digit((string)$amount) || (substr($amount,0,1)=='-'&&ctype_digit(substr($amount,1)))) === FALSE ) { $this->error = 'isValidAmount() - Non-integer value entered'; return FALSE; } if( ctype_digit((string)$amount) === FALSE ) { $this->error = 'isValidAmount() - Negative or non-integer value entered'; return FALSE; } return TRUE; } } ?> Quote Link to comment Share on other sites More sharing options...
TOA Posted October 5, 2011 Author Share Posted October 5, 2011 Holy Script Batman! At first glance, that's a bit much for my intentions, but I'm gonna sink my teeth into that for educational purposes anyway, who knows maybe its not. I'm really glad you attempted it yourself! And figured it out as well. I always try myself first, I had just exhausted all the options I could see at that time. I ran through about a dozen different attempts, alot probably the same as something I'd attempted before but was getting an array of things from the wrong results to an infinite loop (yep, hit 3 of em ) Anyway, thanks for the help I really appreciate it. I'll let you know if I have any questions when I'm going through that script. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 5, 2011 Author Share Posted October 5, 2011 Well, the only issue I've encountered (with my script) is if you don't have enough of the smallest currency (say you need 5 pennies but you only have 4) it doesn't notice that, it just finishes out and shorts the person. For my purposes, that's ok, because it won't happen like that, but still, it's an issue worth noting for future reference. Quote Link to comment Share on other sites More sharing options...
xyph Posted October 5, 2011 Share Posted October 5, 2011 ONE NOTE TO CHANGE : The properties should be declared protected, not public. The were public for testing purposes and I forgot to change them back. protected $amount = array(), $currency = array(), $scale, $decimals, $error = FALSE; Yeah, I found that too, so I made a check in my transaction method // Make sure changed received was correct - if something like pennies ran out, the script would short change. if( $this->getTotalFromArrayScaled($change) != ($paymentTotal - $cost) ) { $this->error('transaction() - Unable to give correct change, minimum currency is unavailable or undefined'); $this->takeArrayOut($payment); return FALSE; } I convert the array of currencies given back into a total, and make sure it matches the $payment-$cost entered originally. I originally had a greedy system, where if it ran out of pennies it would either short change or round up to the nearest nickel depending on the value of a $greed property. I dropped it though, due to handling a scenario where there's no pennies or nickels - or dimes etc. becoming quite complex. Another thing I'd like to add is the ability to takeOutAtLeastTotal() - Say you want to use two instances of this class -> one for a user's wallet, and the other for a till. If the user only has 5x $20 Bill and wants to pay for an item that cost 52.23, it will take out as little as possible to exceed that number. It would be quite easy to set up. Loop through currencies greatest to smallest in the same manner we've done before. If the total after that loop hasn't met the total needed, it will then loop from smallest to greatest until the total needed has been exceeded. Or simpler, you could just loop through your currencies greatest to smallest until your total is greater than or equal the total needed. I'm happy to explain any section in detail. I use a lot of short-hand coding so it's not quite as easy to read through. Let me know. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 5, 2011 Author Share Posted October 5, 2011 ONE NOTE TO CHANGE : The properties should be declared protected, not public. The were public for testing purposes and I forgot to change them back. protected $amount = array(), $currency = array(), $scale, $decimals, $error = FALSE; 10-4. Yeah, I found that too, so I made a check in my transaction method // Make sure changed received was correct - if something like pennies ran out, the script would short change. if( $this->getTotalFromArrayScaled($change) != ($paymentTotal - $cost) ) { $this->error('transaction() - Unable to give correct change, minimum currency is unavailable or undefined'); $this->takeArrayOut($payment); return FALSE; } I convert the array of currencies given back into a total, and make sure it matches the $payment-$cost entered originally. I get that; that should be easy enough to implement (famous last words) Another thing I'd like to add is the ability to takeOutAtLeastTotal() - Say you want to use two instances of this class -> one for a user's wallet, and the other for a till. If the user only has 5x $20 Bill and wants to pay for an item that cost 52.23, it will take out as little as possible to exceed that number. It would be quite easy to set up. Loop through currencies greatest to smallest in the same manner we've done before. If the total after that loop hasn't met the total needed, it will then loop from smallest to greatest until the total needed has been exceeded. Or simpler, you could just loop through your currencies greatest to smallest until your total is greater than or equal the total needed. Umm..I think get what your saying I actually don't need anything like that for what I'm doing this time though. Basically, its a points based rewards system..you do stuff, I give you points that you can use to buy our stuff. We give the smaller point amounts out like candy, so they should not have that issue with our usage. The easiest way to explain it was as currency though; same principle. I'm happy to explain any section in detail. I use a lot of short-hand coding so it's not quite as easy to read through. Let me know. Thanks, I'm sure I'll need it. I'm ok with oop, but I have no doubt I'll encounter something I can't figure out. Actually, thats my favorite part. ** Afterthought - does it handle checks/credit cards? Sorry if that's a question that could be answered by looking through the code, I havn't had a chance yet Quote Link to comment Share on other sites More sharing options...
xyph Posted October 5, 2011 Share Posted October 5, 2011 No it doesn't. It's purely for currency exchange. I was thinking of that myself, but then realized that you shouldn't have to give change for checks/credit, nor would there be any currency added to the wallet on one of those transactions. If you wanted to simulate someone giving 53.21 in a check, the 'wallet' going to the bank and turning the check into currency and then add that to the wallet, you could simply use $wallet->putTotalIn( 53.21 );. That will add 53.21 into the wallet using the least amount of currency possible. Hope those terms made sense. Let me know what you're thinking. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 5, 2011 Author Share Posted October 5, 2011 No it doesn't. It's purely for currency exchange. I was thinking of that myself, but then realized that you shouldn't have to give change for checks/credit, nor would there be any currency added to the wallet on one of those transactions. Oh, right! Dumb DA lol Hope those terms made sense. Let me know what you're thinking. Really, I'm not sure yet; just exploring the path. At work, our system is purchased by an employer for their workforce, but the boss hog wants to tweak it to be open to the public, which would require the person to pay for it, where this might come in handy. Actually, I might be able to use it in a personal project I'm working on too; estimate payments, costs, what you could buy for the cash in your pocket, that type of thing. Problem is, these days in the States, cash is about worthless. About the only place it's good anymore is lemonade stands! (exagerating of course ) I suppose writing in a method to handle some sort of coupons wouldn't be tough either... Quote Link to comment Share on other sites More sharing options...
xyph Posted October 6, 2011 Share Posted October 6, 2011 This is where you'd want to use an interface http://php.net/manual/en/language.oop5.interfaces.php Make a similar class that follows the same design as the one above, perhaps trim them down a little. You then have a cash and plastic class with the same interactions... if a value coming in is a bit of cash, and two types of plastic, it's not an issue as both classes implement the same methods. Similar values can be put into $cash->transaction as $plastic->transaction, and running totals can be calculated with $cash->getTotal() + $plastic->getTotal() etc. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 6, 2011 Author Share Posted October 6, 2011 I'm happy to explain any section in detail. I use a lot of short-hand coding so it's not quite as easy to read through. Let me know. The only thing I came across that I hadn't seen before was this if( ($change = $till->transaction(78.41, $payment, $changeWanted)) === FALSE ) there's an extra set of parenthesis in there I can't quite place. You do it in two places. The statement makes sense to me (i think) but I'd like to know what purpose the extras serve. Thanks Quote Link to comment Share on other sites More sharing options...
TOA Posted October 6, 2011 Author Share Posted October 6, 2011 This is where you'd want to use an interface http://php.net/manual/en/language.oop5.interfaces.php Make a similar class that follows the same design as the one above, perhaps trim them down a little. You then have a cash and plastic class with the same interactions... if a value coming in is a bit of cash, and two types of plastic, it's not an issue as both classes implement the same methods. Similar values can be put into $cash->transaction as $plastic->transaction, and running totals can be calculated with $cash->getTotal() + $plastic->getTotal() etc. I saw this comment coming Quote Link to comment Share on other sites More sharing options...
xyph Posted October 6, 2011 Share Posted October 6, 2011 if( ($change = $till->transaction(78.41, $payment, $changeWanted)) === FALSE ) there's an extra set of parenthesis in there I can't quite place. You do it in two places. The statement makes sense to me (i think) but I'd like to know what purpose the extras serve. This is simply turning this... $change = $till->transaction(78.41, $payment, $changeWanted); if( $change === FALSE ) ...in to one line. The parenthesis make sure that whatever's inside is evaluated first, and the return of the declaration is used for comparison. It's just a short hard way of writing the above code. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 6, 2011 Author Share Posted October 6, 2011 This is simply turning this... $change = $till->transaction(78.41, $payment, $changeWanted); if( $change === FALSE ) ...in to one line. The parenthesis make sure that whatever's inside is evaluated first, and the return of the declaration is used for comparison. It's just a short hard way of writing the above code. Thought so. It dawned on my that's probably what it was for, but didn't know for sure. Is there a good resource for all the available shorthands? Thanks for the lesson! I appreciate the time. Quote Link to comment Share on other sites More sharing options...
xyph Posted October 6, 2011 Share Posted October 6, 2011 Not that I know of... it's something you pick up look through other's code and experimenting yourself. One neat think about conditional and loop structures - if you only have one line of code to execute, you don't need the curly braces. <?php for( $i = 0; $i < 10; $i++ ) echo 'We are on number '.($i+1).'<br>'; echo 'this is echoed after the loop'; ?> 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.