|
| 1 | +# src/user_interface/wallet.py |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +# Set up logging for the wallet module |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | +class Wallet: |
| 9 | + def __init__(self, user_id): |
| 10 | + """ |
| 11 | + Initialize the Wallet. |
| 12 | +
|
| 13 | + Parameters: |
| 14 | + - user_id (str): The ID of the user associated with the wallet. |
| 15 | + """ |
| 16 | + self.user_id = user_id |
| 17 | + self.balance = 0.0 # Initial balance |
| 18 | + logger.info(f"Wallet initialized for user: {self.user_id}") |
| 19 | + |
| 20 | + def deposit(self, amount): |
| 21 | + """ |
| 22 | + Deposit an amount into the wallet. |
| 23 | +
|
| 24 | + Parameters: |
| 25 | + - amount (float): The amount to deposit. |
| 26 | +
|
| 27 | + Raises: |
| 28 | + - ValueError: If the amount is negative. |
| 29 | + """ |
| 30 | + if amount < 0: |
| 31 | + logger.error("Deposit amount must be positive.") |
| 32 | + raise ValueError("Deposit amount must be positive.") |
| 33 | + |
| 34 | + self.balance += amount |
| 35 | + logger.info(f"Deposited {amount} to wallet. New balance: {self.balance}") |
| 36 | + |
| 37 | + def withdraw(self, amount): |
| 38 | + """ |
| 39 | + Withdraw an amount from the wallet. |
| 40 | +
|
| 41 | + Parameters: |
| 42 | + - amount (float): The amount to withdraw. |
| 43 | +
|
| 44 | + Raises: |
| 45 | + - ValueError: If the amount is negative or exceeds the balance. |
| 46 | + """ |
| 47 | + if amount < 0: |
| 48 | + logger.error("Withdrawal amount must be positive.") |
| 49 | + raise ValueError("Withdrawal amount must be positive.") |
| 50 | + if amount > self.balance: |
| 51 | + logger.error("Insufficient balance for withdrawal.") |
| 52 | + raise ValueError("Insufficient balance for withdrawal.") |
| 53 | + |
| 54 | + self.balance -= amount |
| 55 | + logger.info(f"Withdrew {amount} from wallet. New balance: {self.balance}") |
| 56 | + |
| 57 | + def get_balance(self): |
| 58 | + """ |
| 59 | + Get the current balance of the wallet. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + - float: The current balance. |
| 63 | + """ |
| 64 | + logger.info(f"Current balance for user {self.user_id}: {self.balance}") |
| 65 | + return self.balance |
0 commit comments