【问题标题】:ATM transaction behavior with denomination of an amount in phpATM交易行为与PHP中的金额面额
【发布时间】:2018-10-08 17:04:46
【问题描述】:

可以复制 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

请帮帮我

提前致谢

【问题讨论】:

  • 代码应该如何知道要“打印”什么样的账单?您的老师有没有更具体地告诉您如何拆分数字?
  • 这是什么? if(!preg_match('/\d\d[0]$/',$amount))
  • 需要像实际的ATM一样将金额拆分为多个面额
  • 我用过的自动取款机都没有做过这样的事情。所以请解释一下你想让代码做什么
  • 我想要一个运行起来完全像 atm 的代码。

标签: php class currency


【解决方案1】:
<?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*/
  }
 ?>

【讨论】:

  • 输出我需要的面额,每种货币都可以在集合中使用,而不仅仅是单一货币
  • 这正是代码显示的内容 $number_to_print 是要打印的货币数量。 $currency_denomination 是一组可用且要打印的货币。
猜你喜欢
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2020-07-10
  • 1970-01-01
  • 2022-01-11
  • 2020-10-16
  • 2019-02-11
相关资源
最近更新 更多