Solved

Vending Machine Porgram

Posted on 2004-03-28
5
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10700453
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
ID: 10700456
Sorry,

 $change = totaldollars-$totalcost;

should be

 $change = $totaldollars-$totalcost;

venkat.
0
 
LVL 6

Accepted Solution

by:
Reapz earned 125 total points
ID: 10700503
<?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
ID: 10700536
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
ID: 10701453
venkateshwarr I did try yours first but I ended up with a parse error, could be my bad though.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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