Solved

PHP calculate change

Posted on 2010-11-17
3
596 Views
Last Modified: 2012-05-10
I have a class called payment where I get the amount that a person owes then the amount the person paid then the amount of change. I also need to provide the a member function to determine the largest amount of each denomination to return to the customer. This is really confusing me I am not sure do I make a variable for each denomination. and then the math to figure out how to distribute it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calculate Change</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<?php



	class payment {

		public function __construct($paid,$owed) {
			$this->paid = $paid;
			$this->owed = $owed;
		}

		public function getChange() {
			return $this->owed - $this->paid;
		}

		public function getOwes() {
			return $this->owed - $this->paid;
		}
		public function returnDenomination(){
			
			$one = 1.00;
			$five = 5.00;
			$ten = 10.00;
			$twenty = 20.00;
			$fifty = 50.00;
			$hundred = 100.00;
			$penny = 0.01;
			$nickle = 0.05;
			$dime = 0.10;
			$quarter = 0.25;
			
			if(this->paid === this->owed)
			{
				echo "The customer is not owed any change. <br />"
			}
			else
			{
				return 
			



	}

	$payment = new payment($_POST["owed"],$_POST["paid"]);



	$change = $payment->getChange();
	$stillOwes = $payment->getOwes();


?>
<h1>Calculate Change</h1>
<h1>Enter a transaction</h1>
<form action="CalculateChange.php" method="post">
<table border="0">
     <tr><td align="right">Amount Owed:</td>

         <td align="left">$<input type="text" size="6" name="owed"
              value="" /></td></tr>
     <tr><td align="right">Amount Paid:</td>
         <td align="left">$<input type="text" size="6" name="paid"
              value="" /></td></tr>
<tr><td colspan="2" align="center">
     <input type="reset" name="reset" value="Clear Form" /> &nbsp;
     <input type="submit" name="submit" value="Calculate Change" /></td></tr>
</table>
</form>


</body>
</html>

Open in new window

0
Comment
Question by:rcham68
  • 2
3 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 34157675
The simplest way to calculate how to generate change is to start with the largest currency denomination, and subtract as many of those as you can until the amount of change you need to give is less than the denomination, then move down to the next largest currency denomination, and repeat until you get to pennies.

E.g. if you have to give $13.57 back in change, the way you do it is:

Denomination          #          Total          New Subtotal
$50 bills         -        0          $0                     $13.57
$20 bills         -        0          $0                     $13.57
$10 bills         -        1          $10                 $ 3.57
$5   bills         -         0         $0                    $3.57
$1   bills         -         3         $3                    $0.57
25c coins      -          2         $0.50              $0.07
10c coins     -          0          $0                    $0.07
5c   coins      -         1           $0.05              $0.02
1c   coins      -         2           $0.02              $0.00

Total:         1x $10 bill,   3x $1 bill,   2x 25c coin,   1x 5c coin,    2x 2c coin

0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 34157729
Pseudocode:

changetogive   =   13.57
twentybill = 0
tenbill = 0
fivebill = 0
onebill = 0
quarters = 0
dimes = 0
nickels = 0
pennies = 0

// 20 dollar bills
while changetogive >= 20 {
      changetogive -= 20
      twentybills += 1
}

// 10 dollar bills
while changetogive >= 10 {
      changetogive -= 10
      tenbills += 1
}

..... repeat for each type of currency ......

// nickels
while changetogive >= 0.05 {
      changetogive -= 0.05
      nickels += 1
}

// pennies
while changetogive >= 0.01 {
      changetogive -= 0.01
      pennies += 1
}


// finished!

total change to give is stored in the variables:

twentybill, tenbill,fivebill,
onebill, quarters, dimes,
nickels, pennies
0
 
LVL 19

Expert Comment

by:Michael701
ID: 34157764
gee, the 50¢ piece never gets any respect.


Have it return an array where the key is the denomination and the value is the quantity.

I know I'd give my students a few extra points for such an answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Php pie charts 3 26
updating table data with inner join 9 24
Move wordpress site 3 18
Website Interactive tool that will do calculations 3 27
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now