From e956d7d23ec53d146f8b1b8fb7ad001978875045 Mon Sep 17 00:00:00 2001 From: Jonathan Sada Date: Sun, 11 May 2025 20:56:25 +0200 Subject: [PATCH 1/2] Solved lab --- lab-python-oop.ipynb | 140 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 124 insertions(+), 16 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..19f5515 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "\n" + "class BankAccount:\n", + " account_count = 0\n", + " def __init__(self, balance=0):\n", + " BankAccount.account_count += 1\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", + " if amount <=self.balance:\n", + " self.balance -= amount\n", + " else:\n", + " print(\"insufficient balance\")\n", + "\n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "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", + "insufficient balance\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", @@ -117,12 +151,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "class SavingAccount(BankAccount):\n", + " def __init__(self, balance=0, interest_rate = 0.01):\n", + " super().__init__(balance)\n", + " self.interest_rate = interest_rate\n", + " \n", + " def add_interest(self):\n", + " self.balance *= 1 + self.interest_rate\n", + " \n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -151,12 +195,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "s_account = SavingAccount(100, 0.02)\n", + "s_account.deposit(50)\n", + "s_account.withdraw(25)\n", + "s_account.add_interest()\n", + "print(f\"Current balance: {s_account.get_balance()}\")\n", + "print(f\"Interest rate: {s_account.get_interest_rate()}\")" ] }, { @@ -189,12 +248,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "class CheckingAccount(BankAccount):\n", + " def __init__(self, balance = 0, transaction_fee = 1):\n", + " super().__init__(balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + " \n", + " def deduct_fees(self):\n", + " fees = self.transaction_fee * self.transaction_count\n", + " if fees <= self.balance:\n", + " self.balance -= fees\n", + " self.reset_transactions()\n", + " print(f\"${fees} where deducted from the balance due to fees.\")\n", + " else:\n", + " print(\"ERROR: Insuficient balance\")\n", + "\n", + " def deposit(self, amount):\n", + " self.transaction_count += 1\n", + " super().deposit(amount)\n", + "\n", + " def withdraw(self, amount):\n", + " if amount <= self.balance:\n", + " self.transaction_count += 1\n", + " super().withdraw(amount)\n", + " \n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -234,18 +322,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "$4 where deducted from the balance due to fees.\n", + "Current Balance: 546 | Transaction count: 0\n", + "$4 where deducted from the balance due to fees.\n", + "Current Balance: 667 | Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "c_account = CheckingAccount(500, 2)\n", + "c_account.deposit(100)\n", + "c_account.withdraw(50)\n", + "c_account.deduct_fees()\n", + "print(f\"Current Balance: {c_account.get_balance()} | Transaction count: {c_account.get_transaction_count()}\")\n", + "c_account.deposit(200)\n", + "c_account.withdraw(75)\n", + "c_account.deduct_fees()\n", + "print(f\"Current Balance: {c_account.get_balance()} | Transaction count: {c_account.get_transaction_count()}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -259,7 +367,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.12" } }, "nbformat": 4, From c51ad4adaab88f8cd34c171ccf83191d31a9fa7d Mon Sep 17 00:00:00 2001 From: Jonathan Sada Date: Sun, 11 May 2025 21:03:02 +0200 Subject: [PATCH 2/2] Fixed typo and updated print to fit with the examples --- lab-python-oop.ipynb | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 19f5515..2f7d64b 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -151,13 +151,13 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 55, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "class SavingAccount(BankAccount):\n", + "class SavingsAccount(BankAccount):\n", " def __init__(self, balance=0, interest_rate = 0.01):\n", " super().__init__(balance)\n", " self.interest_rate = interest_rate\n", @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 56, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, "outputs": [ @@ -210,7 +210,7 @@ ], "source": [ "# your code goes here\n", - "s_account = SavingAccount(100, 0.02)\n", + "s_account = SavingsAccount(100, 0.02)\n", "s_account.deposit(50)\n", "s_account.withdraw(25)\n", "s_account.add_interest()\n", @@ -248,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 57, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], @@ -265,7 +265,7 @@ " if fees <= self.balance:\n", " self.balance -= fees\n", " self.reset_transactions()\n", - " print(f\"${fees} where deducted from the balance due to fees.\")\n", + " print(f\"Transaction fees of {fees}$ have been deducted from your account balance.\")\n", " else:\n", " print(\"ERROR: Insuficient balance\")\n", "\n", @@ -322,7 +322,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 59, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, "outputs": [ @@ -330,10 +330,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "$4 where deducted from the balance due to fees.\n", - "Current Balance: 546 | Transaction count: 0\n", - "$4 where deducted from the balance due to fees.\n", - "Current Balance: 667 | Transaction count: 0\n" + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current Balance: 546)\n", + "Transaction count: 0\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current Balance: 667)\n", + "Transaction count: 0\n" ] } ], @@ -343,11 +345,13 @@ "c_account.deposit(100)\n", "c_account.withdraw(50)\n", "c_account.deduct_fees()\n", - "print(f\"Current Balance: {c_account.get_balance()} | Transaction count: {c_account.get_transaction_count()}\")\n", + "print(f\"Current Balance: {c_account.get_balance()})\")\n", + "print(f\"Transaction count: {c_account.get_transaction_count()}\")\n", "c_account.deposit(200)\n", "c_account.withdraw(75)\n", "c_account.deduct_fees()\n", - "print(f\"Current Balance: {c_account.get_balance()} | Transaction count: {c_account.get_transaction_count()}\")" + "print(f\"Current Balance: {c_account.get_balance()})\")\n", + "print(f\"Transaction count: {c_account.get_transaction_count()}\")" ] } ],