ATM交易行为,面额为php

时间:2018-10-08 17:04:47

标签: php class currency

可以复制ATM交易行为的Php类。

  

假设我们在ATM上有50,000现金可用,我们需要   用100,500和1000卢比的钞票分配钱。

     

让我们举个例子,如果金额为5000,则显示

     

1000 * 4

     

500 * 1

     

100 * 5

这是我的代码

<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>

如果我传递5000作为金额,它会给我这样的输出

1000 * 5 = 5000

但输出我需要的是

  

1000 * 4 = 4000

     

500 * 1 = 500

     

100 * 5 = 500

请帮帮我

预先感谢

1 个答案:

答案 0 :(得分:0)

<?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*/
  }
 ?>