diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..4b5b128 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", + "class BankAccount():\n", + " account_count = 0\n", + " transaction_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", + " def deposit(self, dep,):\n", + " self.balance += dep\n", + " self.transaction_count += 1\n", + " def withdraw(self, wit):\n", + " if wit > self.balance:\n", + " print(\"Insufficient funds\")\n", + " else:\n", + " self.balance -= wit\n", + " self.transaction_count += 1\n", + " def get_balance(self):\n", + " print(\"Current balance:\", self.balance)\n", + " return self.balance\n", + " def get_account_number(self):\n", + " return self.account_number\n", + "\n", "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "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 funds\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 +153,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\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", + " def add_interest(self):\n", + " self.balance += self.balance * self.interest_rate\n", + " def get_interest_rate(self):\n", + " return self.interest_rate\n", + "\n", + " " ] }, { @@ -151,12 +198,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Expected Output\n", + "Current balance 127.5\n", + "Interest rate 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "my_account = SavingsAccount(100, 0.02)\n", + "my_account.deposit(50)\n", + "my_account.withdraw(25)\n", + "my_account.add_interest()\n", + "print(\"Expected Output\")\n", + "print(\"Current balance\", my_account.get_balance())\n", + "print(\"Interest rate\", my_account.get_interest_rate())" ] }, { @@ -189,12 +253,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "class CheckingAccount(BankAccount):\n", + " def __init__(self, balance, transaction_fee=1, transaction_count=0):\n", + " super().__init__(balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = transaction_count\n", + " def deduct_fees(self,):\n", + " total_fees = self.transaction_fee * self.transaction_count\n", + " if total_fees > self.balance:\n", + " print(\"Insufficient funds to cover transaction fees\")\n", + " else:\n", + " print(f\"Transaction fees of {total_fees}$ have been deducted from your account balance\")\n", + " self.balance -= total_fees\n", + " self.transaction_count = 0\n", + " def reset_transaction_count(self):\n", + " self.transaction_count = 0\n", + " def get_transaction_count(self):\n", + " print(\"Transaction count\", self.transaction_count)\n", + " return self.transaction_count" ] }, { @@ -234,18 +317,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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" + ] + }, + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "my_account2 = CheckingAccount(500, 2)\n", + "my_account2.deposit(100)\n", + "my_account2.withdraw(50)\n", + "my_account2.deduct_fees()\n", + "my_account2.get_balance()\n", + "my_account2.get_transaction_count()\n", + "my_account2.deposit(200)\n", + "my_account2.withdraw(75)\n", + "my_account2.deduct_fees()\n", + "my_account2.get_balance()\n", + "my_account2.get_transaction_count()\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -259,7 +377,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,