Solved

Vending Machine Porgram

Posted on 2004-03-28
5
257 Views
Last Modified: 2012-08-14
I know some of the functions I need but I don't have a clue on the syntax whatsoever @_@

I'm trying to make a vending machine using php that sells 65 cent chocolate bars and takes quarters, dimes and nickels. Instead of having user input I want to use the rand(0, 10) function three times to choose the number of coins of each type entered. It has to then add up how much money total and how many candy bars and change.

I want the display page to say the number of each type of coin entered (just a print out of the data generated by rand(0,10) I think), the total money entered in dollars, and the number of candy bars that were eventually dispensed. :-/
0
Comment
Question by:Wraith10
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
This should put in right direction........

$num_to_purchase = rand(0,10);
$quarters= rand(0,10);
$dimes= rand(0,10);
$nickels= rand(0,10);

$totaldollars = 0.25 * $quarters  + 0.10*$dimes + 0.05 * $nickels;
$totalcost = 0.65 * $num_to_purchase;

 $change = totaldollars-$totalcost;

if ($change > 0)
{
   print "your change is $change"
}
else print "Still ".(-1*($change))." dollars is required."
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
Sorry,

 $change = totaldollars-$totalcost;

should be

 $change = $totaldollars-$totalcost;

venkat.
0
 
LVL 6

Accepted Solution

by:
Reapz earned 125 total points
Comment Utility
<?php
if($_GET['vend'] == "true"){
      $priceperbar = "0.25";
      $quarters = rand(0,10);
      $dimes = rand(0,10);
      $nickels = rand(0,10);
      
      $total = ($quarters * 0.25)+($dimes * 0.10)+($nickels * 0.05);
      if($total > 0){
            $bars = floor($total/$priceperbar);
            $change = fmod($total,$priceperbar);
      }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="300" border="0" cellspacing="3" cellpadding="3">
  <form name="form1" action="?vend=true" method="post">
  <tr>
    <td colspan="2"><div align="center">
      <input type="submit" name="submit" value="Vend"></a>
    </div></td>
  </tr>
  </form>
 <?php if($_GET['vend'] == "true"){ ?>
  <tr>
    <td>Quarters Used: </td>
    <td><?php echo $quarters; ?></td>
  </tr>
  <tr>
    <td>Dimes Used: </td>
    <td><?php echo $dimes; ?></td>
  </tr>
  <tr>
    <td>Nickels Used: </td>
    <td><?php echo $nickels; ?></td>
  </tr>
  <tr>
    <td colspan="2">You pumped a total of $<?php echo $total; ?> into this machine and got <?php echo $bars; ?> candy bars for it!</td>
  </tr>
  <tr>
    <td colspan="2">
      <?php if($change > 0){ ?>
      Sorry but this machine does not give change. You've been robbed of <?php echo $change; ?> cents!
      <?php } ?>
      </td>
  </tr>
 <?php } ?>
</table>
</body>
</html>
0
 
LVL 6

Expert Comment

by:Reapz
Comment Utility
Correction:

$total = ($quarters * 0.25)+($dimes * 0.10)+($nickels * 0.05);  Should be...  $total = number_format(($quarters * 0.25)+($dimes * 0.10)+($nickels * 0.05),2);

$change = fmod($total,$priceperbar);  Should be...  $change = number_format(fmod($total,$priceperbar),2);

Oh and change...

You've been robbed of <?php echo $change; ?> cents!   To...   You've been robbed of $<?php echo $change; ?>!

I really shouldgo to sleep now! :)
0
 

Author Comment

by:Wraith10
Comment Utility
venkateshwarr I did try yours first but I ended up with a parse error, could be my bad though.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

728 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

12 Experts available now in Live!

Get 1:1 Help Now