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
197 changes: 180 additions & 17 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,68 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here\n",
"\n"
"class BankAccount:\n",
" # Class attribute to track total accounts created\n",
" account_count = 0\n",
"\n",
" def __init__(self, balance=0):\n",
" # Increment the class counter every time a new instance is created\n",
" BankAccount.account_count += 1\n",
" \n",
" # Instance attributes\n",
" self.account_number = BankAccount.account_count\n",
" self.balance = balance\n",
"\n",
" def deposit(self, amount):\n",
" if amount > 0:\n",
" self.balance += amount\n",
" print(f\"Deposited {amount}. New balance: {self.balance}\")\n",
" else:\n",
" print(\"Deposit amount must be positive.\")\n",
"\n",
" def withdraw(self, amount):\n",
" if amount > self.balance:\n",
" print(f\"Insufficient balance! Current funds: {self.balance}\")\n",
" elif amount <= 0:\n",
" print(\"Withdrawal amount must be positive.\")\n",
" else:\n",
" self.balance -= amount\n",
" print(f\"Withdrew {amount}. Remaining balance: {self.balance}\")\n",
"\n",
" def get_balance(self):\n",
" return self.balance\n",
"\n",
" def get_account_number(self):\n",
" return self.account_number\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 1 balance: 1000\n",
"Account 1 number: 1\n",
"Account 2 balance: 500\n",
"Account 2 number: 2\n",
"Deposited 500. New balance: 1500\n",
"Withdrew 200. Remaining balance: 1300\n",
"Account 1 balance after transactions: 1300\n",
"Insufficient balance! Current funds: 500\n",
"Account 2 balance after transactions: 500\n"
]
}
],
"source": [
"# Testing the BankAccount class\n",
"# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n",
Expand Down Expand Up @@ -117,12 +164,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"class SavingsAccount(BankAccount):\n",
" def __init__(self, balance=0, interest_rate=0.01):\n",
" # Initialize the parent class (BankAccount)\n",
" super().__init__(balance)\n",
" # Add the specific attribute for Savings\n",
" self.interest_rate = interest_rate\n",
"\n",
" def add_interest(self):\n",
" interest_earned = self.balance * self.interest_rate\n",
" self.balance += interest_earned\n",
" # We don't necessarily need to return anything, just update self.balance\n",
"\n",
" def get_interest_rate(self):\n",
" return self.interest_rate"
]
},
{
Expand Down Expand Up @@ -151,12 +211,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deposited 50. New balance: 150\n",
"Withdrew 25. Remaining balance: 125\n",
"Current balance: 127.5\n",
"Interest rate: 0.02\n"
]
}
],
"source": [
"# your code goes here"
"# 1. Create a SavingsAccount with $100 and 2% (0.02) interest\n",
"savings = SavingsAccount(100, 0.02)\n",
"\n",
"# 2. Deposit $50\n",
"savings.deposit(50) # Balance is now 150\n",
"\n",
"# 3. Withdraw $25\n",
"savings.withdraw(25) # Balance is now 125\n",
"\n",
"# 4. Add interest \n",
"# Note: The object was created with 0.02, so it will use 2%\n",
"savings.add_interest() # 125 * 0.02 = 2.5. New balance: 127.5\n",
"\n",
"# 5. Print results\n",
"print(f\"Current balance: {savings.get_balance()}\")\n",
"print(f\"Interest rate: {savings.get_interest_rate()}\")"
]
},
{
Expand Down Expand Up @@ -189,12 +275,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"class CheckingAccount(BankAccount):\n",
" def __init__(self, balance=0, transaction_fee=1):\n",
" # Initialize parent class attributes\n",
" super().__init__(balance)\n",
" # Add CheckingAccount specific attributes\n",
" self.transaction_fee = transaction_fee\n",
" self.transaction_count = 0\n",
"\n",
" def deposit(self, amount):\n",
" # Call the parent deposit logic\n",
" super().deposit(amount)\n",
" # Increment transaction count\n",
" self.transaction_count += 1\n",
"\n",
" def withdraw(self, amount):\n",
" # Call the parent withdraw logic\n",
" # Note: We only count the transaction if it was successful (optional but realistic)\n",
" # For this exercise, we follow the instruction to update count upon call\n",
" super().withdraw(amount)\n",
" self.transaction_count += 1\n",
"\n",
" def deduct_fees(self):\n",
" total_fees = self.transaction_count * self.transaction_fee\n",
" if self.balance >= total_fees:\n",
" self.balance -= total_fees\n",
" print(f\"Transaction fees of {total_fees}$ have been deducted from your account balance.\")\n",
" self.reset_transactions()\n",
" else:\n",
" print(\"Error: Insufficient balance to deduct transaction fees.\")\n",
"\n",
" def reset_transactions(self):\n",
" self.transaction_count = 0\n",
"\n",
" def get_transaction_count(self):\n",
" return self.transaction_count"
]
},
{
Expand Down Expand Up @@ -234,18 +354,61 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "faa5b148-c11b-4be0-b810-de8a7da81451",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deposited 100. New balance: 600\n",
"Withdrew 50. Remaining balance: 550\n",
"Transaction fees of 4$ have been deducted from your account balance.\n",
"Current balance: 546\n",
"Transaction count: 0\n",
"--------------------\n",
"Deposited 200. New balance: 746\n",
"Withdrew 75. Remaining balance: 671\n",
"Transaction fees of 4$ have been deducted from your account balance.\n",
"Current balance: 667\n",
"Transaction count: 0\n"
]
}
],
"source": [
"# your code goes here"
"# 1. Create a new checking account ($500 balance, $2 fee)\n",
"checking = CheckingAccount(500, 2)\n",
"\n",
"# 2. Deposit $100 (Transaction count: 1)\n",
"checking.deposit(100) \n",
"\n",
"# 3. Withdraw $50 (Transaction count: 2)\n",
"checking.withdraw(50) \n",
"\n",
"# 4. Deduct fees ($2 * 2 transactions = $4)\n",
"checking.deduct_fees()\n",
"\n",
"# 5. Get balance and count\n",
"print(f\"Current balance: {checking.get_balance()}\")\n",
"print(f\"Transaction count: {checking.get_transaction_count()}\")\n",
"\n",
"print(\"-\" * 20)\n",
"\n",
"# 6. Second round of transactions\n",
"checking.deposit(200) # (Count: 1)\n",
"checking.withdraw(75) # (Count: 2)\n",
"checking.deduct_fees() # ($2 * 2 transactions = $4)\n",
"\n",
"# 7. Final status\n",
"print(f\"Current balance: {checking.get_balance()}\")\n",
"print(f\"Transaction count: {checking.get_transaction_count()}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -259,7 +422,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down