|
| 1 | +import uuid |
| 2 | +import datetime |
| 3 | + |
| 4 | +class BankAccountManager: |
| 5 | + def __init__(self): |
| 6 | + self.accounts = {} |
| 7 | + |
| 8 | + def create_account(self, owner_name): |
| 9 | + account_id = str(uuid.uuid4()) |
| 10 | + self.accounts[account_id] = { |
| 11 | + 'owner': owner_name, |
| 12 | + 'balance': 0.0, |
| 13 | + 'transactions': [], |
| 14 | + 'created_at': datetime.datetime.utcnow() |
| 15 | + } |
| 16 | + return account_id |
| 17 | + |
| 18 | + def deposit(self, account_id, amount): |
| 19 | + if amount <= 0: |
| 20 | + raise ValueError("Deposit amount must be positive") |
| 21 | + account = self._get_account(account_id) |
| 22 | + account['balance'] += amount |
| 23 | + account['transactions'].append({ |
| 24 | + 'type': 'deposit', |
| 25 | + 'amount': amount, |
| 26 | + 'timestamp': datetime.datetime.utcnow() |
| 27 | + }) |
| 28 | + return account['balance'] |
| 29 | + |
| 30 | + def withdraw(self, account_id, amount): |
| 31 | + if amount <= 0: |
| 32 | + raise ValueError("Withdrawal amount must be positive") |
| 33 | + account = self._get_account(account_id) |
| 34 | + if account['balance'] < amount: |
| 35 | + raise ValueError("Insufficient funds") |
| 36 | + account['balance'] -= amount |
| 37 | + account['transactions'].append({ |
| 38 | + 'type': 'withdrawal', |
| 39 | + 'amount': amount, |
| 40 | + 'timestamp': datetime.datetime.utcnow() |
| 41 | + }) |
| 42 | + return account['balance'] |
| 43 | + |
| 44 | + def transfer(self, from_account_id, to_account_id, amount): |
| 45 | + if from_account_id == to_account_id: |
| 46 | + raise ValueError("Cannot transfer to the same account") |
| 47 | + self.withdraw(from_account_id, amount) |
| 48 | + self.deposit(to_account_id, amount) |
| 49 | + return True |
| 50 | + |
| 51 | + def get_balance(self, account_id): |
| 52 | + return self._get_account(account_id)['balance'] |
| 53 | + |
| 54 | + def get_transaction_history(self, account_id): |
| 55 | + return list(self._get_account(account_id)['transactions']) |
| 56 | + |
| 57 | + def delete_account(self, account_id): |
| 58 | + if account_id not in self.accounts: |
| 59 | + raise ValueError("Account does not exist") |
| 60 | + del self.accounts[account_id] |
| 61 | + return True |
| 62 | + |
| 63 | + def _get_account(self, account_id): |
| 64 | + if account_id not in self.accounts: |
| 65 | + raise ValueError("Account not found") |
| 66 | + return self.accounts[account_id] |
0 commit comments