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
219 changes: 196 additions & 23 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,96 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here\n",
"class BankAccount:\n",
" # Class attribute shared by all BankAccount objects\n",
" account_count = 0\n",
"\n",
" def __init__(self, balance=0):\n",
" # Increase the count whenever a new account is created\n",
" BankAccount.account_count += 1\n",
"\n",
" # Give each account a unique number\n",
" self.account_number = BankAccount.account_count\n",
" self.balance = balance\n",
"\n",
" def deposit(self, amount):\n",
" self.balance += amount\n",
"\n",
" def withdraw(self, amount):\n",
" self.balance -= amount\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": 3,
"id": "68863d4f-dcef-44ff-85c5-62b3b06cb5e0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 1 number: 3\n",
"Account 1 balance: 150\n",
"Account 2 number: 4\n",
"Account 2 balance: 150\n",
"Account 3 number: 5\n",
"Account 3 balance: 500\n"
]
}
],
"source": [
"account_1 = BankAccount()\n",
"account_2 = BankAccount(100)\n",
"account_3 = BankAccount(500)\n",
"\n",
"account_1.deposit(200)\n",
"account_1.withdraw(50)\n",
"\n",
"account_2.deposit(75)\n",
"account_2.withdraw(25)\n",
"\n",
"print(\"Account 1 number:\", account_1.get_account_number())\n",
"print(\"Account 1 balance:\", account_1.get_balance())\n",
"\n",
"print(\"Account 2 number:\", account_2.get_account_number())\n",
"print(\"Account 2 balance:\", account_2.get_balance())\n",
"\n",
"print(\"Account 3 number:\", account_3.get_account_number())\n",
"print(\"Account 3 balance:\", account_3.get_balance())"
]
},
{
"cell_type": "code",
"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",
"Account 1 balance after transactions: 1300\n",
"Account 2 balance after transactions: -100\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,17 +192,58 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"class SavingsAccount(BankAccount):\n",
"\n",
" def __init__(self, balance=0, interest_rate=0.01):\n",
" # Initialize the inherited BankAccount attributes\n",
" super().__init__(balance)\n",
"\n",
" # Additional SavingsAccount attribute\n",
" self.interest_rate = interest_rate\n",
"\n",
" def add_interest(self):\n",
" interest = self.balance * self.interest_rate\n",
" self.balance += interest\n",
"\n",
" def get_interest_rate(self):\n",
" return self.interest_rate"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "92bc3e12-f628-4515-a453-2385bf8251e9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current balance: 127.5\n",
"Interest rate: 0.02\n"
]
}
],
"source": [
"# Create an account with $100 and a 2% interest rate\n",
"savings_account = SavingsAccount(100, 0.02)\n",
"\n",
"savings_account.deposit(50)\n",
"savings_account.withdraw(25)\n",
"savings_account.add_interest()\n",
"\n",
"print(\"Current balance:\", savings_account.get_balance())\n",
"print(\"Interest rate:\", savings_account.get_interest_rate())"
]
},
{
"cell_type": "markdown",
"id": "d2c84bdb-e7d1-491d-9b0e-194288702c02",
"id": "f2bf5be4-ca27-449c-8773-ab05c0c32bbd",
"metadata": {},
"source": [
"Example of testing the SavingsAccount\n",
Expand All @@ -149,16 +265,6 @@
" Interest rate: 0.02"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
]
},
{
"cell_type": "markdown",
"id": "a5455a88-a8d1-47a6-86b0-7c70647b4f31",
Expand Down Expand Up @@ -189,17 +295,84 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"class CheckingAccount(BankAccount):\n",
"\n",
" def __init__(self, balance=0, transaction_fee=1):\n",
" # Initialize account_number and balance from BankAccount\n",
" super().__init__(balance)\n",
"\n",
" self.transaction_fee = transaction_fee\n",
" self.transaction_count = 0\n",
"\n",
" # Override the inherited deposit method\n",
" def deposit(self, amount):\n",
" super().deposit(amount)\n",
" self.transaction_count += 1\n",
"\n",
" # Override the inherited withdraw method\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_count * self.transaction_fee\n",
"\n",
" if self.balance >= total_fees:\n",
" self.balance -= total_fees\n",
" print(f\"${total_fees} has been deducted in transaction fees.\")\n",
" self.transaction_count = 0\n",
" else:\n",
" print(\"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"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "52a390d8-7286-4153-a641-87a4d24f5e9d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Balance before fees: 140\n",
"Transaction count: 3\n",
"$3 has been deducted in transaction fees.\n",
"Balance after fees: 137\n",
"Transaction count after fees: 0\n"
]
}
],
"source": [
"checking_account = CheckingAccount(balance=100, transaction_fee=1)\n",
"\n",
"checking_account.deposit(50) # Transaction 1\n",
"checking_account.withdraw(20) # Transaction 2\n",
"checking_account.deposit(10) # Transaction 3\n",
"\n",
"print(\"Balance before fees:\", checking_account.get_balance())\n",
"print(\"Transaction count:\", checking_account.get_transaction_count())\n",
"\n",
"checking_account.deduct_fees()\n",
"\n",
"print(\"Balance after fees:\", checking_account.get_balance())\n",
"print(\"Transaction count after fees:\", checking_account.get_transaction_count())"
]
},
{
"cell_type": "markdown",
"id": "8217ec46-3eae-4a7c-9c7c-d4a87aac7e6d",
"id": "f6058e3a-ac76-45f3-950b-2e29001e4933",
"metadata": {},
"source": [
"Example of testing CheckingAccount:\n",
Expand Down Expand Up @@ -245,9 +418,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:anaconda3] *",
"language": "python",
"name": "python3"
"name": "conda-env-anaconda3-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -259,7 +432,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down