Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,889 Bytes
164aab4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# Import necessary modules
import dataclasses
import pytest
# Define a dataclass for a simple BankAccount
@dataclasses.dataclass
class BankAccount:
"""Represents a simple bank account"""
account_number: int
account_holder: str
balance: float = 0.0
def deposit(self, amount: float) -> None:
"""Deposit money into the account"""
self.balance += amount
def withdraw(self, amount: float) -> None:
"""Withdraw money from the account"""
if amount > self.balance:
raise ValueError("Insufficient balance")
self.balance -= amount
def get_balance(self) -> float:
"""Get the current balance of the account"""
return self.balance
# Define a function to create a new BankAccount
def create_account(account_number: int, account_holder: str) -> BankAccount:
"""Create a new BankAccount instance"""
return BankAccount(account_number, account_holder)
# Define a function to perform a transaction
def perform_transaction(account: BankAccount, amount: float, is_deposit: bool) -> None:
"""Perform a transaction on the account"""
if is_deposit:
account.deposit(amount)
else:
account.withdraw(amount)
# Define a test function using pytest
def test_bank_account():
"""Test the BankAccount class"""
account = create_account(12345, "John Doe")
assert account.get_balance() == 0.0
perform_transaction(account, 100.0, True)
assert account.get_balance() == 100.0
perform_transaction(account, 50.0, False)
assert account.get_balance() == 50.0
# Run the test
pytest.main([__file__])
# Create a new BankAccount instance
account = create_account(12345, "John Doe")
# Perform some transactions
perform_transaction(account, 100.0, True)
perform_transaction(account, 50.0, False)
# Print the final balance
print("Final balance:", account.get_balance()) |