?
Solved

Vending Machine Porgram

Posted on 2004-03-28
5
Medium Priority
?
265 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
Suggested Courses

770 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