Reputation: 624
Php Class which can replicate ATM transaction behavior.
Assume that we have 50,000 cash available in ATM and we need to dispense money in 100,500 and 1000 rupee notes.
lets take an example if amount is 5000 then show
1000 * 4
500 * 1
100 * 5
here is my code
<html>
<body>
<center>
<h2>Atm Machine</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" name="txtAmount" placeholder="Enter Amount" required>
<input type="submit" value="Submit">
</form>
<?php
if (!empty($_POST)) {
$message = "";
class AtmBehavior{
public function atmDispencer($amount){
if($amount != ""){
// //$amount= intval($amount);
$notes = array(1000,500,100);
$noteCount = array(0,0,0);
$output="";
if($amount <=0)
{
$output="<b>Invalid Amount</b>";
return $output;
}
elseif ($amount > 50000) {
$output="<b>Amount should not exceed 50k</b>";
return $output;
}
else{
if(!preg_match('/\d\d[0]$/',$amount))
{
$output="<b>Invalid Amount</b>";
return $output;
}else{
for($i=0;$i<count($notes);$i++){
if($notes[$i]<$amount || $notes[$i]==$amount){
$noteCount[$i]=intval($amount/$notes[$i]);
$amount=$amount-$noteCount[$i]*$notes[$i];
//$amount=$amount%$notes[$i];
}
}
for($i=0;$i<count($noteCount);$i++){
if($noteCount[$i]!=0){
$output .= "<br><b>".$notes[$i]." X ".$noteCount[$i]." = ".($notes[$i]*$noteCount[$i])."</b>";
}
}
return $output;
}
}
}else{
$output="<b>Invalid Amount- Amount Input Not Blank</b>";
return $output;
}
}
}
$transaction = new AtmBehavior;
$message = $transaction->atmDispencer($_POST['txtAmount']);
?>
<div><br>
<?php
echo "Output: $message";
}
?>
</div>
</center>
</body>
</html>
if i pass 5000 as an amount it gives me output like this
1000 * 5 = 5000
but output what i need is
1000 * 4 = 4000
500 * 1 = 500
100 * 5 = 500
please help me out
Thanks in advance
Upvotes: 0
Views: 1426
Reputation: 1525
<?php
echo 'Atm is on<br>';
//amount to be withdrawn
$amount=5000;
//when a set of denomination is available indicate
/*denomination sets which are available are indicated by true*/
$one_thousands=true;
$five_hundreds=true;
$one_hundreds=false;
//check if amount to be withdrawn is a multiple of 100
if(($amount % 100)==0)
{
if($one_thousands==true && $five_hundreds==true && $one_hundreds==true)
{
//check if amount is divisible by 1000 or by 500 else divide by 100 and remit cash
if($amount%1000==0)
{
$number_to_print = $amount/1000;
$currency_denomination = 'one_thousands';
echo "".$number_to_print." of ".$currency_denomination."";
}
elseif($amount%500==0)
{
$number_to_print = $amount/500;
$currency_denomination = 'five_hundreds';
echo "".$number_to_print." of ".$currency_denomination."";
}
else
{
$number_to_print = $amount/100;
$currency_denomination = 'one_hundreds';
echo "".$number_to_print." of ".$currency_denomination."";
}
}
elseif($one_thousands!=true && $five_hundreds==true && $one_hundreds==true)
{
if($amount%500==0)
{
$number_to_print = $amount/500;
$currency_denomination = 'five_hundreds';
echo "".$number_to_print." of ".$currency_denomination."";
}
else
{
$number_to_print = $amount/100;
$currency_denomination = 'one_hundreds';
echo "".$number_to_print." of ".$currency_denomination."";
}
}
elseif($one_thousands==true && $five_hundreds!=true && $one_hundreds==true)
{
if($amount%1000==0)
{
$number_to_print = $amount/1000;
$currency_denomination = 'one_thousands';
echo "".$number_to_print." of ".$currency_denomination."";
}
else
{
$number_to_print = $amount/100;
$currency_denomination = 'one_hundreds';
echo "".$number_to_print." of ".$currency_denomination."";
}
}
elseif($one_thousands==true && $five_hundreds==true && $one_hundreds!=true)
{
if($amount%1000==0)
{
$number_to_print = $amount/1000;
$currency_denomination = 'one_thousands';
echo "".$number_to_print." of ".$currency_denomination."";
}
elseif($amount%500==0)
{
$number_to_print = $amount/500;
$currency_denomination = 'five_hundreds';
echo "".$number_to_print." of ".$currency_denomination."";
}
else
{
echo'enter an amount in multiples of 1000 or 500';
}
}
/*they are 4 other instances, add that to complete the logic*/
}
?>
Upvotes: 0