diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..aea060a 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,39 +56,81 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 262, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "class BankAccount:\n", + " # keep a class-level counter for account numbers\n", + " account_count = 0\n", + "\n", + " def __init__(self, balance: int = 0) -> None:\n", + " # Increment the class attribute for every new instance to get unique account numbers\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 self.balance <= 0:\n", + " print(f\"Your balance is negative: {self.balance}\")\n", + " elif self.balance - amount < 0:\n", + " print(\n", + " f\"in your account are {self.balance}.\\nYou don't have enough money in the account to complete this transaction.\"\n", + " )\n", + " else:\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" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 263, "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", + "Your balance is negative: -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", "account1 = BankAccount(1000)\n", - "account2 = BankAccount(500)\n", + "account2 = BankAccount(-500)\n", "\n", - "print(\"Account 1 balance:\", account1.get_balance()) # This should print 1000\n", - "print(\"Account 1 number:\", account1.get_account_number()) # This should print 1\n", + "print(\"Account 1 balance:\", account1.get_balance()) # This should print 1000\n", + "print(\"Account 1 number:\", account1.get_account_number()) # This should print 1\n", "\n", - "print(\"Account 2 balance:\", account2.get_balance()) #This should print 500\n", - "print(\"Account 2 number:\", account2.get_account_number()) #This should print 2\n", + "print(\"Account 2 balance:\", account2.get_balance()) # This should print 500\n", + "print(\"Account 2 number:\", account2.get_account_number()) # This should print 2\n", "\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()) # This should print 1300\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()) # This should print 1300\n", "\n", - "account2.withdraw(600) # We withdraw 600 in the 2nd account \n", - "print(\"Account 2 balance after transactions:\", account2.get_balance())# This should print insufficient balance, and still 500 in funds" + "account2.withdraw(600) # We withdraw 600 in the 2nd account\n", + "print(\n", + " \"Account 2 balance after transactions:\", account2.get_balance()\n", + ") # This should print insufficient balance, and still 500 in funds" ] }, { @@ -117,12 +159,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 264, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class SavingsAccount(BankAccount):\n", + "\n", + " def __init__(self, balance: int = 0, interest_rate=0.01, year=1) -> None:\n", + " super().__init__(balance)\n", + " self.interest_rate = interest_rate\n", + " self.year = year\n", + " pass\n", + "\n", + " def add_interest(self):\n", + " self.balance += round(self.balance * (((1 + self.interest_rate) ** self.year) - 1), 2)\n", + "\n", + " return self.balance\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -151,12 +207,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 265, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 100\n", + "The current balance is: 127.5\n", + "The interest rate is: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "account1 = SavingsAccount(100, 0.02)\n", + "print(\"Account 1 balance:\", account1.get_balance())\n", + "account1.deposit(50)\n", + "account1.withdraw(25)\n", + "account1.add_interest()\n", + "print(\n", + " f\"The current balance is: {account1.get_balance()}\\nThe interest rate is: {account1.get_interest_rate()}\"\n", + ")" ] }, { @@ -194,7 +267,44 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class CheckingAccount(BankAccount):\n", + "\n", + " def __init__(\n", + " self,\n", + " balance,\n", + " transaction_fee=1,\n", + " ):\n", + " super().__init__(balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + "\n", + " def deduct_fees(self):\n", + " deduct_fee = self.transaction_fee * self.transaction_count\n", + " if deduct_fee < self.balance:\n", + " self.balance -= deduct_fee\n", + " print(f\"The total fees are: {deduct_fee}$\")\n", + " self.transaction_count = 0\n", + " else:\n", + " print(f\"Insufficient balance for fees {deduct_fee} on balance {self.balance}\")\n", + "\n", + " # Overriding deposit to track transactions\n", + " def deposit(self, amount):\n", + " self.transaction_count += 1\n", + " return super().deposit(amount)\n", + "\n", + " # Overriding withdraw to track transactions\n", + " def withdraw(self, amount):\n", + " self.transaction_count += 1\n", + " return super().withdraw(amount)\n", + "\n", + " def disp_fee(self):\n", + " return self.transaction_fee\n", + "\n", + " def reset_transaction(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self) -> int:\n", + " return self.transaction_count" ] }, { @@ -234,18 +344,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 281, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total fees are: 4$\n", + "The currenct balance is 546\n", + "Transcation count: 0\n", + "\n", + "The total fees are: 4$\n", + "The currenct balance is 667\n", + "Transcation count: 0\n", + "\n" + ] + } + ], "source": [ - "# your code goes here" + "account_s = CheckingAccount(500, 2)\n", + "account_s.deposit(100)\n", + "account_s.withdraw(50)\n", + "account_s.deduct_fees()\n", + "print(\n", + " f\"The currenct balance is {account_s.get_balance()}\\nTranscation count: {account_s.get_transaction_count()}\\n\"\n", + ")\n", + "\n", + "account_s.deposit(200)\n", + "account_s.withdraw(75)\n", + "account_s.deduct_fees()\n", + "print(\n", + " f\"The currenct balance is {account_s.get_balance()}\\nTranscation count: {account_s.get_transaction_count()}\\n\"\n", + ")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -259,7 +397,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,