|
| 1 | +class Account: |
| 2 | + |
| 3 | + def __init__(self, owner, amount=0): |
| 4 | + self.owner = owner |
| 5 | + self.amount = amount |
| 6 | + self._transactions = [] |
| 7 | + |
| 8 | + @property |
| 9 | + def balance(self): |
| 10 | + return sum(self._transactions) + self.amount |
| 11 | + |
| 12 | + def handle_transaction(self, transaction_amount): |
| 13 | + if self.balance + transaction_amount < 0: |
| 14 | + raise ValueError("sorry cannot go in debt!") |
| 15 | + |
| 16 | + self._transactions.append(transaction_amount) |
| 17 | + |
| 18 | + return f"New balance: {self.balance}" |
| 19 | + |
| 20 | + def add_transaction(self, amount): |
| 21 | + if not isinstance(amount, int): |
| 22 | + raise ValueError("please use int for amount") |
| 23 | + |
| 24 | + return self.handle_transaction(amount) |
| 25 | + |
| 26 | + def __str__(self): |
| 27 | + return f"Account of {self.owner} with starting amount: {self.amount}" |
| 28 | + |
| 29 | + def __repr__(self): |
| 30 | + return f"Account({self.owner}, {self.amount})" |
| 31 | + |
| 32 | + def __len__(self): |
| 33 | + return len(self._transactions) |
| 34 | + |
| 35 | + def __getitem__(self, index): |
| 36 | + return self._transactions[index] |
| 37 | + |
| 38 | + def __reversed__(self): |
| 39 | + return self._transactions[::-1] |
| 40 | + |
| 41 | + def __gt__(self, other): |
| 42 | + return self.balance > other.balance |
| 43 | + |
| 44 | + def __lt__(self, other): |
| 45 | + return self.balance < other.balance |
| 46 | + |
| 47 | + def __ge__(self, other): |
| 48 | + return self.balance >= other.balance |
| 49 | + |
| 50 | + def __le__(self, other): |
| 51 | + return self.balance <= other.balance |
| 52 | + |
| 53 | + def __eq__(self, other): |
| 54 | + return self.balance == other.balance |
| 55 | + |
| 56 | + def __ne__(self, other): |
| 57 | + return self.balance != other.balance |
| 58 | + |
| 59 | + def __add__(self, other): |
| 60 | + new_account = Account(f"{self.owner}&{other.owner}", self.amount + other.amount) |
| 61 | + new_account._transactions = self._transactions + other._transactions |
| 62 | + |
| 63 | + return new_account |
| 64 | + |
0 commit comments