Solved

Vending Machine Porgram

Posted on 2004-03-28
5
260 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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
PHP Query return divisible by 3 3 25
Read a remote csv file from a https URL 8 44
selector:validator cookies 4 28
Wordpress Query 5 24
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

792 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