diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..b82659c 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "class BankAccount:\n", + " account_count = 0\n", + "\n", + " def __init__(self, initial_balance):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count \n", + " self.balance = initial_balance\n", + "\n", + " def deposit(self, amount):\n", + " self.balance += amount\n", + " print(f\"Deposited ${amount}.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if 0 < amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"Withdrew ${amount}.\")\n", + " elif amount > self.balance:\n", + " print(\"Insufficient funds for this withdrawal.\")\n", + " else:\n", + " print(\"Withdrawal amount must be positive.\")\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": 33, "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", + "Deposited $500.\n", + "Withdrew $200.\n", + "Account 1 balance after transactions: 1300\n", + "Insufficient funds for this withdrawal.\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 +157,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class SavingAccount(BankAccount):\n", + " def __init__(self, initial_balance, interest_rate=0.01):\n", + " super().__init__(initial_balance)\n", + " self.interest_rate = interest_rate\n", + " \n", + " def add_interest_rate(self):\n", + " interest = self.balance * self.interest_rate\n", + " self.balance += interest\n", + " print(f'Interest of ${interest:.2f} added.')\n", + " \n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -151,12 +202,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited $50.\n", + "Withdrew $25.\n", + "Interest of $2.50 added.\n", + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "savings_account_test = SavingAccount(initial_balance=100, interest_rate=0.02)\n", + "\n", + "savings_account_test.deposit(50)\n", + "\n", + "savings_account_test.withdraw(25)\n", + "\n", + "savings_account_test.add_interest_rate() \n", + "\n", + "current_balance = savings_account_test.get_balance()\n", + "current_interest_rate = savings_account_test.get_interest_rate()\n", + "\n", + "print(f\"Current balance: {current_balance}\")\n", + "print(f\"Interest rate: {current_interest_rate}\")\n", + "\n", + "\n" ] }, { @@ -189,12 +266,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class CheckingAccount(BankAccount):\n", + " def __init__(self, initial_balance, transaction_fee=1):\n", + " super().__init__(initial_balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + "\n", + " def deposit(self, amount):\n", + " \"\"\"Overrides the deposit method to include transaction count.\"\"\"\n", + " super().deposit(amount) \n", + " self.transaction_count += 1 \n", + "\n", + " def withdraw(self, amount):\n", + " \"\"\"Overrides the withdraw method to include transaction count.\"\"\"\n", + " super().withdraw(amount)\n", + " self.transaction_count += 1\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count *self.transaction_fee\n", + " if total_fees <= self.balance:\n", + " self.balance -= total_fees\n", + " print(f'Deducted {total_fees} in transaction fees.')\n", + " else:\n", + " print('Insufficient founds')\n", + " self.reset_transactions()\n", + " \n", + " def deposit(self, amount):\n", + " \"\"\"Overrides the deposit method to include transaction count.\"\"\"\n", + " super().deposit(amount) \n", + " self.transaction_count += 1 \n", + "\n", + " def withdraw(self, amount):\n", + " \"\"\"Overrides the withdraw method to include transaction count.\"\"\"\n", + " super().withdraw(amount)\n", + " self.transaction_count += 1\n", + " \n", + " def reset_transactions(self):\n", + " self.transaction_count=0\n", + " \n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -234,12 +349,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited $100.\n", + "Withdrew $50.\n", + "Deducted 4 in transaction fees.\n", + "Deposited $200.\n", + "Withdrew $75.\n", + "Deducted 4 in transaction fees.\n", + "Current balance after first deduction: $546\n", + "Transaction count after first deduction: 0\n", + "Current balance after second deduction: $667\n", + "Transaction count after second deduction: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "checking_account_test = CheckingAccount(initial_balance=500, transaction_fee=2)\n", + "\n", + "checking_account_test.deposit(100)\n", + "\n", + "checking_account_test.withdraw(50)\n", + "\n", + "checking_account_test.deduct_fees()\n", + "\n", + "current_balance_afd1 = checking_account_test.get_balance()\n", + "transaction_count_afd1 = checking_account_test.get_transaction_count()\n", + "\n", + "checking_account_test.deposit(200)\n", + "\n", + "checking_account_test.withdraw(75)\n", + "\n", + "checking_account_test.deduct_fees()\n", + "\n", + "current_balance_afd2 = checking_account_test.get_balance()\n", + "transaction_count_afd2 = checking_account_test.get_transaction_count()\n", + "\n", + "print(f\"Current balance 1: ${current_balance_afd1}\")\n", + "print(f\"Transaction count 1: {transaction_count_afd1}\")\n", + "print(f\"Current balance 2: ${current_balance_afd2}\")\n", + "print(f\"Transaction count 2: {transaction_count_afd2}\")\n" ] } ], @@ -259,7 +414,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.8.2" } }, "nbformat": 4,