diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..39a67fc 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,66 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 165, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "\n" + "class BankAccount:\n", + " account_count=0\n", + " account_numbers=[]\n", + " def __init__(self,balance=0):\n", + " # if account_num in BankAccount.account_numbers:\n", + " # print('Account Already exists, we have created a new account, account number ',(account_num+1))\n", + " # self.account_number = account_num+1\n", + " # #raise TypeError\n", + " # else: \n", + " # self.account_number = account_num\n", + " if not BankAccount.account_numbers:\n", + " self.account_number=1\n", + " BankAccount.account_numbers.append(self.account_number)\n", + " else:\n", + " self.account_number=BankAccount.account_numbers[-1]+1\n", + " BankAccount.account_numbers.append(self.account_number)\n", + " self.balance=balance\n", + " BankAccount.account_count += 1\n", + " #BankAccount.account_numbers.append(account_num)\n", + " def deposit(self,amount):\n", + " self.balance=self.balance+amount\n", + " return self.balance+amount\n", + " def withdraw(self,amount):\n", + " if self.balance>=amount:\n", + " self.balance = self.balance-amount\n", + " return self.balance\n", + " else:\n", + " print('Insufficient funds')\n", + " def get_balance(self):\n", + " return self.balance\n", + " def get_account_number(self):\n", + " return self.account_number\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 195, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 1000\n", + "Account 1 number: 10\n", + "Account 2 balance: 500\n", + "Account 2 number: 11\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 +162,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 181, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\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.balance*self.interest_rate\n", + " return self.balance\n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -151,12 +205,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 182, "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", + "acc1=SavingsAccount(100,0.02)\n", + "acc1.deposit(50)\n", + "#print('balance is ',acc1.balance)\n", + "acc1.withdraw(25)\n", + "#print('balance is ',acc1.balance)\n", + "acc1.add_interest()\n", + "#print('balance is ',acc1.balance)\n", + "acc1.get_balance()\n", + "print('Current balance: ',acc1.balance)\n", + "int_rate=acc1.get_interest_rate()\n", + "print('Interest rate: ',(int_rate))\n" ] }, { @@ -189,12 +263,43 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", + "execution_count": 194, + "id": "9a4b422e-301a-4576-a392-1bbf4f609db7", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "class CheckingAccount(BankAccount):\n", + " \n", + " def __init__(self,balance=0,transaction_fee=1, transaction_count=0):\n", + " super().__init__(balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = transaction_count\n", + "\n", + " def deposit(self,amount):\n", + " self.transaction_count+=1\n", + " new_balance = super().deposit(amount)\n", + " return new_balance\n", + "\n", + " def withdraw(self,amount):\n", + " self.transaction_count+=1\n", + " new_balance = super().withdraw(amount)\n", + " return new_balance\n", + "\n", + " def deduct_fees(self):\n", + " resulting_balance = self.balance-self.transaction_count*self.transaction_fee\n", + " if resulting_balance>=0:\n", + " self.balance = resulting_balance\n", + " print(f\"We have deducted ${self.transaction_count*self.transaction_fee} from your account\")\n", + " self.transaction_count = 0\n", + " else:\n", + " print(\"Error: Insufficient funds\")\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 +339,40 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "faa5b148-c11b-4be0-b810-de8a7da81451", + "execution_count": 193, + "id": "2223acbb-53da-49da-86df-95797ea4e899", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We have deducted $4 from your account\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "We have deducted $4 from your account\n", + "Current balance: 667\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "acc3=CheckingAccount(500,2)\n", + "acc3.deposit(100)\n", + "acc3.withdraw(50)\n", + "acc3.deduct_fees()\n", + "new_bal=acc3.get_balance()\n", + "transact_count=acc3.get_transaction_count()\n", + "print('Current balance: ',(new_bal))\n", + "print('Transaction count: ',(transact_count))\n", + "acc3.deposit(200)\n", + "acc3.withdraw(75)\n", + "acc3.deduct_fees()\n", + "new_bal=acc3.get_balance()\n", + "transact_count=acc3.get_transaction_count()\n", + "print('Current balance: ',(new_bal))\n", + "print('Transaction count: ',(transact_count))" ] } ], @@ -259,7 +392,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,