/** *How much money this person has *@var float */ public $bankBalance; public function deposit($amount) { $this->bankBalance += $amount; return $this->bankBalance; } public function withdraw($amount) { if ($this->bankBalance >= $amount) { $this->bankBalance -= $amount; return $this->bankBalance; } else { throw new Exception('Insufficient funds'); } } } try { $myAccount = new BankAccount(); $myAccount->owner = 'Samir'; $myAccount->bankBalance = 200; $myAccount->deposit(10); $myAccount->withdraw(300); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } echo "<pre>"; var_dump($myAccount);
echo "Error: insufficient funds!<br>\n"; } } function print_log() { $attr = array("date", "operation", "amount", "old_balance", "new_balance"); echo "<table>"; for ($i = 0; $i < count($this->_log); $i++) { echo "<tr>"; for ($j = 0; $j < count($attr); $j++) { echo "<td>" . $this->_log[$i][$attr[$j]] . "</td>"; } echo "</tr>"; } echo "</table>"; } } // test $ba = new BankAccount("my name", 1000); echo "Balance: " . $ba->get_balance() . "<br>\n"; $ba->deposit(200); $ba->deposit(300); echo "Balance: " . $ba->get_balance() . "<br>\n"; $ba->withdraw(500); echo "Balance: " . $ba->get_balance() . "<br>\n"; $ba->withdraw(2000); $ba->print_log(); ?> </body> </html>
return $this->bankBalance; } /** * Remove some money from your account * @param float $amount How much you want to remove * @return float * @throws Exception */ public function withdraw($amount) { if ($this->bankBalance < $amount) { // An exception is a PHP class that can be extended and worked with in a diverse number of ways throw new Exception('You can\'t withdraw ' . $amount); } $this->bankBalance -= $amount; return $this->bankBalance; } } // Because we have the potential of throwing an exception, all of the code that may throw an exception should be wrapped in a try tag // The try could be placed in the withdraw function but this is bad design because in more complicated structures you want each method to // only perform 1 function and route all exceptions through 1 place try { $myAccount = new BankAccount(); $myAccount->owner = 'Jared'; $myAccount->deposit(12345); $myAccount->withdraw(100000); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } echo '<pre>'; print_r($myAccount);
* @todo: add validation to this to make sure they have enough money! * Throw an Exception if they are broke! * @throws Exception * @return float */ public function withdraw($amount) { if ($this->bankBalance < $amount) { throw new Exception('Go away'); } if ($this->owner == 'Samir') { throw new Exception('Toobroke'); } $this->bankBalance = $this->bankBalance - $amount; return $this->bankBalance; } } try { $myAccount = new BankAccount(); $myAccount->owner = 'Samir'; $myAccount->deposit(20); $myAccount->withdraw(20); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } //echo '<pre>'; //print_r( $e); //die(); //echo '<pre>'; //print_r($myAccount); //die();
/** @dataProvider withdrawDataProvider * @param BankAccount $bank_account_stub */ public function testWithdraw($bank_account_stub, $money, $expected_balance) { $bank_account_stub->withdraw($money); $this->assertEquals($expected_balance, $bank_account_stub->getBalance()); }
public function testWithdrawDecreasesBalance() { $this->_account->withdraw(100); $this->assertEquals(-100, $this->_account->getBalance()); }
{ $this->bankBalance += $amount; return $this->bankBalance; } /** * Withdraw some money from your account * @param float $amount How much money you want to withdraw * @throws Exception * @return float */ public function withdraw($amount) { if ($amount < $this->bankBalance) { $this->bankBalance -= $amount; return $this->bankBalance; } else { throw new Exception('Insufficient Funds'); } } } try { $myAccount = new BankAccount(); $myAccount->owner = 'Samir'; $myAccount->deposit(100); $myAccount->withdraw(50); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } echo '<pre>'; print_r($myAccount); die;