Поведение транзакции в банкомате с указанием суммы в php - PullRequest
0 голосов
/ 08 октября 2018

Php класс, который может реплицировать поведение транзакции банкомата.

Предположим, что у нас есть 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 голосов
/ 09 октября 2018
<?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*/
  }
 ?>
...