From 129f88d85d5e4b795c5272f2984d1bb4d1834ef1 Mon Sep 17 00:00:00 2001 From: martacarballo Date: Tue, 14 Oct 2025 21:19:52 +0200 Subject: [PATCH] oopsolved --- lab-python-oop.ipynb | 209 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 177 insertions(+), 32 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..5196a75 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,26 +56,70 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 117, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "\n" + "class BankAccount:\n", + " \n", + " def __init__(self, account_number, balance=0):\n", + " self.account_number = account_number\n", + " self.balance = balance\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"Deposited {amount}. New balance is {self.balance}.\")\n", + " else:\n", + " print(\"Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"Withdrew {amount}. New balance is {self.balance}.\")\n", + " else:\n", + " print(\"Insufficient funds.\")\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\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 118, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 1000\n", + "Account 1 number: abc\n", + "Account 2 balance: 500\n", + "Account 2 number: cdf\n", + "Deposited 500. New balance is 1500.\n", + "Withdrew 200. New balance is 1300.\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", - "account1 = BankAccount(1000)\n", - "account2 = BankAccount(500)\n", + "account1 = BankAccount(\"abc\", 1000)\n", + "account2 = BankAccount(\"cdf\", 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", @@ -117,12 +161,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 119, "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, account_number, balance=0, interest_rate=0.01):\n", + " super().__init__(account_number, balance)\n", + " self.interest_rate = interest_rate\n", + "\n", + " def add_interest(self):\n", + " interest = self.balance + (self.balance * self.interest_rate)\n", + " self.balance = interest\n", + " print(f\"Applied interest of {interest}. New balance is {self.balance}.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "5ed4007f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 50. New balance is 150.\n", + "Withdrew 25. New balance is 125.\n", + "Applied interest of 127.5. New balance is 127.5.\n", + "Current balance: 127.5\n" + ] + } + ], + "source": [ + "SavingsAccount1 = SavingsAccount(\"xyz\", 100, 0.02)\n", + "Deposit1 = SavingsAccount1.deposit(50)\n", + "Withdraw1 = SavingsAccount1.withdraw(25) \n", + "SavingsAccount1.add_interest()\n", + "print (\"Current balance:\", SavingsAccount1.get_balance())\n", + "\n" ] }, { @@ -149,16 +228,6 @@ " Interest rate: 0.02" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, { "cell_type": "markdown", "id": "a5455a88-a8d1-47a6-86b0-7c70647b4f31", @@ -189,12 +258,98 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 213, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "class CheckingAccount(BankAccount):\n", + " transaction_count=0\n", + " \n", + "\n", + " def __init__(self, account_number, balance=0, transaction_fee=1):\n", + " super().__init__(account_number, balance)\n", + " self.transaction_fee = transaction_fee\n", + " \n", + "\n", + " def deposit(self, amount):\n", + " super().deposit(amount)\n", + " self.transaction_count += 1\n", + "\n", + " def withdraw(self, amount):\n", + " super().withdraw(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", + " self.balance -= total_fees\n", + " print(f\"Deducted total fees of {total_fees}. New balance is {self.balance}.\")\n", + " else:\n", + " print(\"Insufficient funds to cover transaction fees.\")\n", + " self.transaction_count = 0 # Reset transaction count after deducting fees\n", + " \n", + " def reset_transaction(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count\n", + " \n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 217, + "id": "91921d99", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 100. New balance is 600.\n", + "Withdrew 50. New balance is 550.\n", + "Deducted total fees of 4. New balance is 546.\n", + "Current balance: 546\n", + "Transaction count 0\n", + "Deposited 200. New balance is 746.\n", + "Withdrew 75. New balance is 671.\n", + "Deducted total fees of 4. New balance is 667.\n", + "Current balance: 667\n", + "Transaction count 0\n", + "Deposited 10. New balance is 677.\n", + "Deposited 10. New balance is 687.\n", + "Withdrew 5. New balance is 682.\n", + "Deducted total fees of 6. New balance is 676.\n", + "Current balance: 676\n", + "Transaction count 0\n" + ] + } + ], + "source": [ + "CheckingAccount1 = CheckingAccount(\"hola\", 500, 2)\n", + "Deposit1 = CheckingAccount1.deposit(100)\n", + "Withdraw1 = CheckingAccount1.withdraw(50) \n", + "Deductfees1 = CheckingAccount1.deduct_fees()\n", + "print (\"Current balance:\", CheckingAccount1.get_balance())\n", + "print (\"Transaction count\", CheckingAccount1.get_transaction_count())\n", + "\n", + "Deposit2 = CheckingAccount1.deposit(200)\n", + "Withdraw2= CheckingAccount1.withdraw(75)\n", + "Deductfees2 = CheckingAccount1.deduct_fees()\n", + "print (\"Current balance:\", CheckingAccount1.get_balance())\n", + "print (\"Transaction count\", CheckingAccount1.get_transaction_count())\n", + "\n", + "Deposit3 = CheckingAccount1.deposit(10)\n", + "Deposit4 = CheckingAccount1.deposit(10)\n", + "Withdraw3= CheckingAccount1.withdraw(5)\n", + "Deductfees3 = CheckingAccount1.deduct_fees()\n", + "print (\"Current balance:\", CheckingAccount1.get_balance())\n", + "print (\"Transaction count\", CheckingAccount1.get_transaction_count())\n", + "\n" ] }, { @@ -231,21 +386,11 @@ "\n", "Note: *the print \"Transaction fees of 4$ have been deducted from your account balance\" is done in the method deduct_fees*" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "faa5b148-c11b-4be0-b810-de8a7da81451", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -259,7 +404,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,