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
196 changes: 180 additions & 16 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here\n",
"\n"
"import uuid\n",
"class BankAccount():\n",
" def __init__(self, balance):\n",
" self.account_number = uuid.uuid4()\n",
" self.balance = balance\n",
"\n",
"\n",
" def deposit(self, amount):\n",
" self.balance += amount\n",
" return f\"Balance is {self.balance}\"\n",
" \n",
" def withdraw(self, amount):\n",
" self.balance -= amount\n",
" return f\"Balance is {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": 5,
"id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 1 balance: 1000\n",
"Account 1 number: 9eabf37e-2d86-4b02-9ddd-7efa811bb3d9\n",
"Account 2 balance: 500\n",
"Account 2 number: ce84280c-d5e1-4ab8-aaef-f18f6a0593f5\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 @@ -120,9 +152,58 @@
"execution_count": null,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1020.0\n",
"1040.4\n",
"1061.208\n",
"0.02\n",
"Account 1 balance after transactions: 1361.208\n",
"----------------------------\n",
"1010.0\n",
"1020.1\n",
"1030.301\n",
"0.01\n",
"----------------------------\n",
"127.5\n",
"0.02\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"class SavingsAccount(BankAccount):\n",
" def __init__(self, balance, interest_rate=0.01):\n",
" self.balance = balance\n",
" self.interest_rate = interest_rate\n",
"\n",
" def add_interest(self):\n",
" self.balance += self.balance * self.interest_rate\n",
" return self.balance\n",
"\n",
" def get_interest_rate(self):\n",
" return self.interest_rate\n",
"\n",
"\n",
"account1 = SavingsAccount(1000, 0.02)\n",
"print(account1.add_interest())\n",
"print(account1.add_interest())\n",
"print(account1.add_interest())\n",
"print(account1.get_interest_rate())\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()) \n",
"print(\"----------------------------\")\n",
"account2 = SavingsAccount(1000)\n",
"print(account2.add_interest())\n",
"print(account2.add_interest())\n",
"print(account2.add_interest())\n",
"print(account2.get_interest_rate())\n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -151,12 +232,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"127.5\n",
"0.02\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"account3 = SavingsAccount(100,0.02)\n",
"account3.deposit(50) \n",
"account3.withdraw(25) \n",
"account3.add_interest()\n",
"print(account3.get_balance())\n",
"print(account3.get_interest_rate())"
]
},
{
Expand Down Expand Up @@ -192,9 +289,55 @@
"execution_count": null,
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 € have been deducted from your bank account\n",
"546\n",
"4 € have been deducted from your bank account\n",
"667\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"class CheckingAccount(BankAccount):\n",
" \n",
" def __init__(self, balance, transaction_fee):\n",
" self.balance = balance\n",
" self.transaction_fee = transaction_fee\n",
" self.transaction_count = 0\n",
"\n",
" def deduct_fees(self):\n",
" sum_of_fees = self.transaction_fee * self.transaction_count\n",
" if self.balance >= sum_of_fees:\n",
" self.balance -= sum_of_fees\n",
" print(f\"{sum_of_fees} € have been deducted from your bank account\")\n",
" else:\n",
" print(\"Error: Not enough money in the account\")\n",
" \n",
" self.transaction_count = 0\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",
" def deposit(self, amount):\n",
" self.balance += amount\n",
" self.transaction_count += 1\n",
" return f\"Balance is {self.balance}\"\n",
" \n",
" def withdraw(self, amount):\n",
" self.balance -= amount\n",
" self.transaction_count += 1\n",
" return f\"Balance is {self.balance}\"\n",
" \n",
"\n"
]
},
{
Expand Down Expand Up @@ -234,18 +377,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"id": "faa5b148-c11b-4be0-b810-de8a7da81451",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 € have been deducted from your bank account\n",
"546\n",
"4 € have been deducted from your bank account\n",
"667\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"account3 = CheckingAccount(500,2)\n",
"account3.deposit(100) \n",
"account3.withdraw(50) \n",
"account3.deduct_fees()\n",
"print(account3.get_balance())\n",
"account3.deposit(200) \n",
"account3.withdraw(75) \n",
"account3.deduct_fees()\n",
"print(account3.get_balance())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -259,7 +423,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down