|
| 1 | +class BankAccount: |
| 2 | + """ Create a new bank account """ |
| 3 | + def __init__(self, name, hasCommissionDiscount): |
| 4 | + self._name = name |
| 5 | + self._hasCommissionDiscount = hasCommissionDiscount |
| 6 | + self._balance = 0 |
| 7 | + self._commission_rate = BankAccount._calc_commission_rate(hasCommissionDiscount) |
| 8 | + |
| 9 | + def info(self): |
| 10 | + """ Account information """ |
| 11 | + return { |
| 12 | + "name": self._name, |
| 13 | + "current_balance": self._balance, |
| 14 | + } |
| 15 | + |
| 16 | + def deposit(self, amount): |
| 17 | + """ deposit money """ |
| 18 | + if amount > 0: |
| 19 | + self._balance += amount - self._calc_commission_rate(self._hasCommissionDiscount) |
| 20 | + else: |
| 21 | + raise ValueError("deposit amount must be larger than 0") |
| 22 | + |
| 23 | + def balance(self): |
| 24 | + return self._balance |
| 25 | + |
| 26 | + def withdraw(self, amount): |
| 27 | + """ withdraw money """ |
| 28 | + if self._balance >= amount > 0: |
| 29 | + self._balance -= (amount + self._calc_commission_rate(self._hasCommissionDiscount)) |
| 30 | + else: |
| 31 | + raise ValueError("Insufficient funds for withdraw") |
| 32 | + |
| 33 | + def can_withdraw(self, amount): |
| 34 | + return self._balance >= amount > 0 |
| 35 | + |
| 36 | + def transfer_to_other_account(self, amount, other_account): |
| 37 | + """ transfer money """ |
| 38 | + if amount <= 0: |
| 39 | + raise ValueError("Transfer amount must be larger than 0") |
| 40 | + amount_including_commission = amount + self._commission_rate |
| 41 | + if self._balance >= amount_including_commission > 0: |
| 42 | + self._balance -= amount_including_commission |
| 43 | + other_account._balance += amount |
| 44 | + else: |
| 45 | + raise ValueError("Insufficient funds for transfer") |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def _calc_commission_rate(hasCommisionDiscount): |
| 49 | + """ Get the rate of commission for this account """ |
| 50 | + if hasCommisionDiscount: |
| 51 | + return 2.5 |
| 52 | + else: |
| 53 | + return 9 |
0 commit comments