Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 242 additions & 28 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,86 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here\n",
"class BankAccount:\n",
" _next_account_number = 1 # Class variable to ensure unique account numbers\n",
"\n",
" def __init__(self):\n",
" self._account_number = BankAccount._next_account_number\n",
" BankAccount._next_account_number += 1\n",
" self._balance = 0.0\n",
"\n",
" def deposit(self, amount):\n",
" if amount > 0:\n",
" self._balance += amount\n",
" print(f\"Deposited ${amount:.2f} to Account #{self._account_number}\")\n",
" else:\n",
" print(\"Deposit amount must be positive.\")\n",
"\n",
" def withdraw(self, amount):\n",
" if amount > 0:\n",
" if amount <= self._balance:\n",
" self._balance -= amount\n",
" print(f\"Withdrew ${amount:.2f} from Account #{self._account_number}\")\n",
" else:\n",
" print(\"Insufficient balance for withdrawal.\")\n",
" else:\n",
" print(\"Withdrawal amount must be positive.\")\n",
"\n",
" def get_balance(self):\n",
" return self._balance\n",
"\n",
" def get_account_number(self):\n",
" return self._account_number\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deposited $500.00 to Account #4\n",
"Withdrew $150.00 from Account #4\n",
"Account #4 Balance: $350.00\n",
"Deposited $1218.00 to Account #5\n",
"Withdrew $225.00 from Account #5\n",
"Withdrew $300.00 from Account #5\n",
"Account #5 Balance: $693.00\n",
"Deposited $50.00 to Account #6\n",
"Withdrew $25.00 from Account #6\n",
"Account #6 Balance: $25.00\n"
]
}
],
"source": [
"# Testing the BankAccount class\n",
"# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n",
"account1 = BankAccount(1000)\n",
"account2 = BankAccount(500)\n",
"\n",
"print(\"Account 1 balance:\", account1.get_balance()) # This should print 1000\n",
"print(\"Account 1 number:\", account1.get_account_number()) # This should print 1\n",
"\n",
"print(\"Account 2 balance:\", account2.get_balance()) #This should print 500\n",
"print(\"Account 2 number:\", account2.get_account_number()) #This should print 2\n",
"\n",
"account1.deposit(500) # We depoist 500 in the first account\n",
"account1.withdraw(200) # We withdraw 200 in the first account\n",
"print(\"Account 1 balance after transactions:\", account1.get_balance()) # This should print 1300\n",
"\n",
"account2.withdraw(600) # We withdraw 600 in the 2nd account \n",
"print(\"Account 2 balance after transactions:\", account2.get_balance())# This should print insufficient balance, and still 500 in funds"
"account1 = BankAccount()\n",
"account2 = BankAccount()\n",
"account3 = BankAccount()\n",
"\n",
"# Do some Transactions\n",
"account1.deposit(500)\n",
"account1.withdraw(150)\n",
"print(f\"Account #{account1.get_account_number()} Balance: ${account1.get_balance():.2f}\")\n",
"\n",
"account2.deposit(1218)\n",
"account2.withdraw(225) # Should give insufficient funds warning\n",
"account2.withdraw(300)\n",
"print(f\"Account #{account2.get_account_number()} Balance: ${account2.get_balance():.2f}\")\n",
"\n",
"account3.deposit(50)\n",
"account3.withdraw(25)\n",
"print(f\"Account #{account3.get_account_number()} Balance: ${account3.get_balance():.2f}\")"
]
},
{
Expand Down Expand Up @@ -117,12 +164,78 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deposited $56.00 to Account #1\n",
"Withdrew $28.00 from Account #1\n",
"Added interest of $8.28 to Account #1\n",
"Account #1 Final Balance: $836.28\n",
"Account #1 Interest Rate: 1.00%\n"
]
}
],
"source": [
"# your code goes here"
"class BankAccount:\n",
" _next_account_number = 1 # Class variable to ensure unique account numbers\n",
"\n",
" def __init__(self):\n",
" self._account_number = BankAccount._next_account_number\n",
" BankAccount._next_account_number += 1\n",
" self._balance = 0.0\n",
"\n",
" def deposit(self, amount):\n",
" if amount > 0:\n",
" self._balance += amount\n",
" print(f\"Deposited ${amount:.2f} to Account #{self._account_number}\")\n",
" else:\n",
" print(\"Deposit amount must be positive.\")\n",
"\n",
" def withdraw(self, amount):\n",
" if amount > 0:\n",
" if amount <= self._balance:\n",
" self._balance -= amount\n",
" print(f\"Withdrew ${amount:.2f} from Account #{self._account_number}\")\n",
" else:\n",
" print(\"Insufficient balance for withdrawal.\")\n",
" else:\n",
" print(\"Withdrawal amount must be positive.\")\n",
"\n",
" def get_balance(self):\n",
" return self._balance\n",
"\n",
" def get_account_number(self):\n",
" return self._account_number\n",
"\n",
"# SavingsAccount inherits from BankAccount\n",
"class SavingsAccount(BankAccount):\n",
" def __init__(self, interest_rate=0.01, initial_balance=0.0):\n",
" super().__init__()\n",
" self._interest_rate = interest_rate\n",
" self._balance = initial_balance\n",
"\n",
" def add_interest(self):\n",
" interest = self._balance * self._interest_rate\n",
" self._balance += interest\n",
" print(f\"Added interest of ${interest:.2f} to Account #{self._account_number}\")\n",
"\n",
" def get_interest_rate(self):\n",
" return self._interest_rate\n",
"\n",
"# Test the SavingsAccount class\n",
"savings = SavingsAccount(interest_rate=0.03, initial_balance=800)\n",
"savings.deposit(56)\n",
"savings.withdraw(28)\n",
"savings._interest_rate = 0.01 # Use default rate before adding interest\n",
"savings.add_interest()\n",
"\n",
"print(f\"Account #{savings.get_account_number()} Final Balance: ${savings.get_balance():.2f}\")\n",
"print(f\"Account #{savings.get_account_number()} Interest Rate: {savings.get_interest_rate() * 100:.2f}%\")\n"
]
},
{
Expand Down Expand Up @@ -189,12 +302,113 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deposited $100.00 to Account #1\n",
"Withdrew $50.00 from Account #1\n",
"Deducted $4.00 in transaction fees from Account #1\n",
"\n",
"Account #1 Balance: $846.00\n",
"Transaction Count: 0\n",
"\n",
"Deposited $200.00 to Account #1\n",
"Withdrew $75.00 from Account #1\n",
"Deducted $4.00 in transaction fees from Account #1\n",
"\n",
"Account #1 Balance: $967.00\n",
"Transaction Count: 0\n"
]
}
],
"source": [
"# your code goes here"
"class BankAccount:\n",
" _next_account_number = 1 # Class variable to ensure unique account numbers\n",
"\n",
" def __init__(self):\n",
" self._account_number = BankAccount._next_account_number\n",
" BankAccount._next_account_number += 1\n",
" self._balance = 0.0\n",
"\n",
" def deposit(self, amount):\n",
" if amount > 0:\n",
" self._balance += amount\n",
" print(f\"Deposited ${amount:.2f} to Account #{self._account_number}\")\n",
" else:\n",
" print(\"Deposit amount must be positive.\")\n",
"\n",
" def withdraw(self, amount):\n",
" if amount > 0:\n",
" if amount <= self._balance:\n",
" self._balance -= amount\n",
" print(f\"Withdrew ${amount:.2f} from Account #{self._account_number}\")\n",
" else:\n",
" print(\"Insufficient balance for withdrawal.\")\n",
" else:\n",
" print(\"Withdrawal amount must be positive.\")\n",
"\n",
" def get_balance(self):\n",
" return self._balance\n",
"\n",
" def get_account_number(self):\n",
" return self._account_number\n",
"\n",
"\n",
"class CheckingAccount(BankAccount):\n",
" def __init__(self, transaction_fee=1.0, initial_balance=0.0):\n",
" super().__init__()\n",
" self._transaction_fee = transaction_fee\n",
" self._transaction_count = 0\n",
" self._balance = initial_balance\n",
"\n",
" def deposit(self, amount):\n",
" super().deposit(amount)\n",
" self._transaction_count += 1\n",
"\n",
" def withdraw(self, amount):\n",
" super().withdraw(amount)\n",
" self._transaction_count += 1\n",
"\n",
" def deduct_fees(self):\n",
" total_fees = self._transaction_fee * self._transaction_count\n",
" if total_fees <= self._balance:\n",
" self._balance -= total_fees\n",
" print(f\"Deducted ${total_fees:.2f} in transaction fees from Account #{self._account_number}\")\n",
" self._transaction_count = 0\n",
" else:\n",
" print(\"Sadly, your balance is too low for this transaction.\")\n",
"\n",
" def reset_transactions(self):\n",
" self._transaction_count = 0\n",
"\n",
" def get_transaction_count(self):\n",
" return self._transaction_count\n",
"\n",
"\n",
"# === Testing the CheckingAccount class ===\n",
"\n",
"account = CheckingAccount(transaction_fee=2.0, initial_balance=800)\n",
"\n",
"# First set of transactions\n",
"account.deposit(100)\n",
"account.withdraw(50)\n",
"account.deduct_fees()\n",
"print(f\"\\nAccount #{account.get_account_number()} Balance: ${account.get_balance():.2f}\")\n",
"print(f\"Transaction Count: {account.get_transaction_count()}\")\n",
"\n",
"print() # Empty line for clarity\n",
"\n",
"# Second set of transactions\n",
"account.deposit(200)\n",
"account.withdraw(75)\n",
"account.deduct_fees()\n",
"print(f\"\\nAccount #{account.get_account_number()} Balance: ${account.get_balance():.2f}\")\n",
"print(f\"Transaction Count: {account.get_transaction_count()}\")\n"
]
},
{
Expand Down Expand Up @@ -245,7 +459,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -259,7 +473,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down