1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11class BankAccountException extends RuntimeException
12{
13}
14
15/**
16 * A bank account.
17 *
18 */
19class BankAccount
20{
21    /**
22     * The bank account's balance.
23     *
24     * @var float
25     */
26    protected $balance = 0;
27
28    /**
29     * Returns the bank account's balance.
30     *
31     * @return float
32     */
33    public function getBalance()
34    {
35        return $this->balance;
36    }
37
38    /**
39     * Sets the bank account's balance.
40     *
41     * @param float $balance
42     *
43     * @throws BankAccountException
44     */
45    protected function setBalance($balance)
46    {
47        if ($balance >= 0) {
48            $this->balance = $balance;
49        } else {
50            throw new BankAccountException;
51        }
52    }
53
54    /**
55     * Deposits an amount of money to the bank account.
56     *
57     * @param float $balance
58     *
59     * @throws BankAccountException
60     */
61    public function depositMoney($balance)
62    {
63        $this->setBalance($this->getBalance() + $balance);
64
65        return $this->getBalance();
66    }
67
68    /**
69     * Withdraws an amount of money from the bank account.
70     *
71     * @param float $balance
72     *
73     * @throws BankAccountException
74     */
75    public function withdrawMoney($balance)
76    {
77        $this->setBalance($this->getBalance() - $balance);
78
79        return $this->getBalance();
80    }
81}
82