使用验证器计算透支

时间:2015-06-29 00:23:26

标签: php oop

我试图通过将一些练习从几个Java类转换成它来学习PHP OOP。除了微妙的差异,我在这种转换之前一直相对成功。这个想法是你有一个银行账户和余额开始。使用一系列交易类型(提款或存款),您将在交易后​​输出余额。如果提款金额大于余额,它仍将处理交易并输出新余额,并增加30美元的透支费用。看似简单。

但是,无论我尝试什么,它都不会使用validateWithdrawalAmount来验证提取,以返回' 0'或者' FALSE"如果没有资金。我在网上搜索过,但一直无法找到解决方案。这段代码来自我最近尝试使用整数而不是TRUE / FALSE,我已经尝试过区分大小写/不敏感。我怀疑这个问题与PHP处理布尔值的方式有些奇怪,但我很困惑。任何帮助将不胜感激!

请记住,这些都是为了学习,而不是美。但是,如果您在解决方案之外看到可以改进我的代码的内容,请随时告诉我。我一直在寻找建设性的批评。我添加了评论以帮助理解。

class_bank_account.php

<?php
class BankAccount
{
    // constant(s)
    const OVERDRAFT_CHARGE = 30;

    // variable(s)
    private $accountNumber;
    private $balance;

    /**
     * Construct the object with set parameters.
     * 
     * @param   String  $sAccountNumber     Account number to use.
     * @param   Double  $dBalance           Starting balance.
     **/
    public function __construct($sAccountNumber, $dBalance)
    {
        $this->accountNumber = $sAccountNumber;
        $this->balance = $dBalance;
    }

    /**
     * Process a withdrawal by, first, checking to see if there are
     * sufficient funds and then subtract the amount of the transaction
     * from the balance.  If the balance is less than the transaction
     * amount then add the overdraft fee.
     *
     * @param   Double $dWithdrawal         Withdrawal amount.
     **/
    public function processWithdrawal($dWithdrawal)
    {
        // check if the withdrawal amount is in good standing with internal function
        $goodStanding = $this->validateWithdrawalAmount($dWithdrawal);

        // process withdrawal
        if($goodStanding == 0)
        {
            $this->balance += $dWithdrawal - self::OVERDRAFT_CHARGE;
        }
        elseif($goodStanding == 1)
        {
            $this->balance += $dWithdrawal;
        }
        else
        {
            echo "error"; // this was added as a way to confirm flow
        }
    }

    /**
     * Validates the withdrawal amount to confirm if there are
     * sufficient funds or not.
     *
     * @param   Double  $dWithdrawal        Withdrawal Amount
     * @return  Boolean                         True or False
     **/
    public function validateWithdrawalAmount($dWithdrawal)
    {
        $sufficientFunds = 1;

        if($dWithdrawal > $this->balance)
        {
            $sufficientFunds = 0;
        }

        return $sufficientFunds; // return either 1 or 0 for True or False
    }

    /**
     * Processes deposits into the account.
     *
     * @param   Double  $dDeposit       Deposit amount.
     **/
    public function processDeposit($dDeposit)
    {
        $this->balance += $dDeposit;
    }

    /**
     * Get the account number.
     *
     * @return String   Account number.
     **/
    public function get_AccountNumber()
    {
        return $this->accountNumber;
    }

    /**
     * Set the account number.
     *
     * @param  String   $sAccountNumber     Account number to use.
     **/
    public function set_AccountNumber($sAccountNumber)
    {
        $this->accountNumber = $sAccountNumber;
    }

    /**
     * Get the account balance.
     *
     * @return  Double  Account balance.
     **/
    public function get_Balance()
    {
        return $this->balance;
    }
}
?>

bankdriver.php

<!DOCTYPE html>
<html>
<head>
    <title>Bank Driver</title>
</head>
<body>
    <?php
        // our class
        include "class_bank_account.php";

        // array of transactions
        $trans = array(
            -20.00,
            120.00,
            -35.00,
            -251.00,
            -2000.00,
            3000.00,
            -4.00,
            -60.00,
            -555.00
        );

        // length of array for counting foreach loop
        $array_length = count($trans);

        // create our bank account object
        $account = new BankAccount("BJS15923", 2000);
    ?>
    <h1><?php echo $account->get_AccountNumber(); ?></h1>
    <table border="1px" width="50%">
        <tr><th>AMOUNT</th><th>BALANCE</th></tr>
        <tr><td>&nbsp;</td><td><?php echo $account->get_Balance(); ?></td></tr>

        <?php
            // cycle through the array
            foreach($trans as $amount)
            {
                // output the amount of the transaction
                echo '<tr><td>'.$amount.'</td><td>';
                if($amount < 0)
                {
                    // processess withdrawal if transaction was negative
                    $account->processWithdrawal($amount);
                }
                elseif($amount > 0)
                {
                    // process deposit if transaction was positive
                    $account->processDeposit($amount);
                }

                // output our new balance
                echo $account->get_Balance().'</td></tr>';
            }
        ?>
    </table>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我认为你的主要问题是你说“如果一个负数”(对于提款)大于我的余额,那么......“ - 见例子:

  1. 您的余额为1814美元
  2. 您想提取2000美元( - $ 2000交易)
  3. 你的函数说if (-2000 > 1814) { ... } - 这是它返回true而不是false的地方。它应该是if (2000 > 1814) { ... },它将您想要提取的金额(2000美元)与您的余额(1814美元)进行比较。
  4. 您可以通过添加abs()调用来强制数字为正数来解决此问题:

    $sufficientFunds = true;
    
    if (abs($dWithdrawal) > $this->getBalance()) {
        $sufficientFunds = false;
    }
    return (bool) $sufficientFunds;
    

    See a working example here。我只更改了代码中的其他一些东西 - 使用布尔值而不是1和0,一些PHPDoc更改(double不是PHP类型,浮点数),从setter返回类的实例通常是好的idea(return $this - 允许方法链接),从方法名称中删除下划线,使用下划线保护前缀受保护的私有类属性等。大多只是风格的东西。