From ffd77a2e4e8dfbaef4828d2ef1f19ef636892807 Mon Sep 17 00:00:00 2001 From: Prabhsheenkaur Date: Sat, 11 Apr 2026 10:55:59 +0100 Subject: [PATCH] Update lab-python-oop.ipynb --- lab-python-oop.ipynb | 248 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 241 insertions(+), 7 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..b1ad0ee 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -61,8 +61,29 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "class BankAccount:\n", + " account_count = 0\n", + "\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", + " if amount > 0:\n", + " self.balance += amount\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient balance\")\n", + " elif amount > 0:\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" ] }, { @@ -122,7 +143,64 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class BankAccount:\n", + " account_count = 0\n", + "\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", + " if amount > 0:\n", + " self.balance += amount\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient balance\")\n", + " elif amount > 0:\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\n", + "\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", + "\n", + " def add_interest(self):\n", + " self.balance += self.balance * self.interest_rate\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate\n", + "\n", + "\n", + "# Testing SavingsAccount\n", + "savings1 = SavingsAccount(1000)\n", + "savings2 = SavingsAccount(500, 0.05)\n", + "\n", + "print(\"Savings 1 balance:\", savings1.get_balance())\n", + "print(\"Savings 1 number:\", savings1.get_account_number())\n", + "print(\"Savings 1 interest rate:\", savings1.get_interest_rate())\n", + "\n", + "print(\"Savings 2 balance:\", savings2.get_balance())\n", + "print(\"Savings 2 number:\", savings2.get_account_number())\n", + "print(\"Savings 2 interest rate:\", savings2.get_interest_rate())\n", + "\n", + "savings1.add_interest()\n", + "print(\"Savings 1 balance after interest:\", savings1.get_balance()) # 1010.0\n", + "\n", + "savings2.deposit(200)\n", + "savings2.add_interest()\n", + "print(\"Savings 2 balance after deposit and interest:\", savings2.get_balance()) # 735.0\n", + "\n", + "savings2.withdraw(800) # Should print insufficient balance\n", + "print(\"Savings 2 final balance:\", savings2.get_balance())" ] }, { @@ -156,7 +234,51 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class BankAccount:\n", + " account_count = 0\n", + "\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", + " if amount > 0:\n", + " self.balance += amount\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient balance\")\n", + " elif amount > 0:\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\n", + "\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", + "\n", + " def add_interest(self):\n", + " self.balance += self.balance * self.interest_rate\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate\n", + "\n", + "\n", + "# Test example from the screenshot\n", + "account = SavingsAccount(100, 0.02)\n", + "account.deposit(50)\n", + "account.withdraw(25)\n", + "account.add_interest()\n", + "\n", + "print(\"Current balance:\", account.get_balance())\n", + "print(\"Interest rate:\", account.get_interest_rate())" ] }, { @@ -190,11 +312,67 @@ { "cell_type": "code", "execution_count": null, - "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", + "id": "805800e9", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class BankAccount:\n", + " account_count = 0\n", + "\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", + " if amount > 0:\n", + " self.balance += amount\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient balance\")\n", + " elif amount > 0:\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\n", + "\n", + "\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 deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient balance\")\n", + " elif amount > 0:\n", + " self.balance -= amount\n", + " self.transaction_count += 1\n", + "\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if total_fees > self.balance:\n", + " print(\"Insufficient balance to deduct fees\")\n", + " else:\n", + " self.balance -= total_fees\n", + " print(f\"{total_fees} deducted in transaction fees\")\n", + " self.transaction_count = 0\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -239,7 +417,63 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class BankAccount:\n", + " account_count = 0\n", + "\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", + " if amount > 0:\n", + " self.balance += amount\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient balance\")\n", + " elif amount > 0:\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\n", + "\n", + "\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 deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient balance\")\n", + " elif amount > 0:\n", + " self.balance -= amount\n", + " self.transaction_count += 1\n", + "\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if total_fees > self.balance:\n", + " print(\"Insufficient balance to deduct fees\")\n", + " else:\n", + " self.balance -= total_fees\n", + " print(f\"Transaction fees of {total_fees}$ have been deducted from your account balance.\")\n", + " self.transaction_count = 0\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] } ],