[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Vending Machine Porgram

Posted on 2004-03-28
5
Medium Priority
?
268 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:
Andy earned 500 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:Andy
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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