【发布时间】: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 的代码。