From 04f30114bbcd5849f3e1d9cce2644a1318fec49e Mon Sep 17 00:00:00 2001 From: gcastellano3 Date: Mon, 20 Oct 2025 21:20:10 +0200 Subject: [PATCH] Updated lab version --- lab-python-oop.ipynb | 705 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 651 insertions(+), 54 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..f611588 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,37 +56,122 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "\n" + "class BankAccount:\n", + " accounts_list = []\n", + "\n", + " def __init__(self, account_number, balance=0, transaction_count=0):\n", + " if account_number in BankAccount.accounts_list:\n", + " print(\"Account number already exists\")\n", + " else:\n", + " self.account_number = account_number\n", + " BankAccount.accounts_list.append(account_number)\n", + " self.balance = balance\n", + " self.transaction_count = 0\n", + " \n", + " def deposit(self, amount):\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + " print(f\"Deposited {amount} in {self.account_number}\")\n", + " return self.balance\n", + "\n", + " def withdraw(self, amount):\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " self.transaction_count += 1\n", + " print(f\"Withdrawed {amount} from {self.account_number}\")\n", + " return self.balance\n", + " else:\n", + " print(f\"Current balance is insufficient: {self.balance}.\")\n", + " \n", + " def get_balance(self):\n", + " return f\"Current balance: {self.balance}\"\n", + " \n", + " def get_account_number(self):\n", + " return f\"Account number: {self.account_number}\"\n" ] }, { "cell_type": "code", - "execution_count": null, - "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", + "execution_count": 6, + "id": "d79c4672", "metadata": {}, "outputs": [], "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", - "\n", + "account1 = BankAccount('ES123456', 1000)\n", + "account2 = BankAccount('ES987654', 500)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e34827a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: Current balance: 1000\n", + "Account 1 number: Account number: ES123456\n", + "Account 2 balance: Current balance: 500\n", + "Account 2 number: Account number: ES987654\n" + ] + } + ], + "source": [ "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", - "\n", + "print(\"Account 2 number:\", account2.get_account_number()) #This should print 2" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 500 in ES123456\n", + "Withdrawed 200 from ES123456\n", + "Account 1 balance after transactions: Current balance: 1300\n" + ] + } + ], + "source": [ "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", + "print(\"Account 1 balance after transactions:\", account1.get_balance()) # This should print 1300" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5b24a41f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current balance is insufficient: 500.\n", + "Account 2 balance after transactions: Current balance: 500\n" + ] + } + ], + "source": [ "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" ] @@ -117,12 +202,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class Savings(BankAccount):\n", + " def __init__(self, account_number, balance=0, transaction_count=0, interest_rate=0.01):\n", + " super().__init__(account_number, balance, transaction_count)\n", + " self.interest_rate = interest_rate\n", + " \n", + " def add_interest(self):\n", + " self.balance += self.balance * self.interest_rate\n", + " return self.balance\n", + " \n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -130,17 +225,136 @@ "id": "d2c84bdb-e7d1-491d-9b0e-194288702c02", "metadata": {}, "source": [ - "Example of testing the SavingsAccount\n", - "\n", - "- Create a SavingsAccount object with a balance of $100 and interest rate of 2%\n", - "\n", - "- Deposit $50 into the savings account\n", - "\n", - "- Withdraw $25 from the savings account\n", - "\n", - "- Add interest to the savings account (use the default 0.01)\n", + "Example of testing the SavingsAccount" + ] + }, + { + "cell_type": "markdown", + "id": "b685fd1b", + "metadata": {}, + "source": [ + "- Create a SavingsAccount object with a balance of $100 and interest rate of 2%" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", + "metadata": {}, + "outputs": [], + "source": [ + "savings1 = Savings('ES856421', 100, 0.02)" + ] + }, + { + "cell_type": "markdown", + "id": "a0481802", + "metadata": {}, + "source": [ + "- Deposit $50 into the savings account" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "bed1bccd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 50 in ES856421\n" + ] + }, + { + "data": { + "text/plain": [ + "150" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "savings1.deposit(50)" + ] + }, + { + "cell_type": "markdown", + "id": "41076820", + "metadata": {}, + "source": [ + "- Withdraw $25 from the savings account" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "b982d7bc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Withdrawed 25 from ES856421\n" + ] + }, + { + "data": { + "text/plain": [ + "125" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "savings1.withdraw(25)" + ] + }, + { + "cell_type": "markdown", + "id": "7a682831", + "metadata": {}, + "source": [ + "- Add interest to the savings account (use the default 0.01)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "ba163681", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "126.25" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "savings1.interest_rate = 0.01\n", "\n", - "- Print the current balance and interest rate of the savings account\n", + "savings1.add_interest()" + ] + }, + { + "cell_type": "markdown", + "id": "81874bd3", + "metadata": {}, + "source": [ + "Print the current balance and interest rate of the savings account\n", "\n", "Expected output:\n", " \n", @@ -151,12 +365,52 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", + "execution_count": 15, + "id": "a9c3bd66", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Current balance: 126.25'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "savings1.get_balance()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e9820b46", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.01" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "savings1.get_interest_rate()" + ] + }, + { + "cell_type": "markdown", + "id": "d6e8ea23", + "metadata": {}, + "source": [ + "** Current balance doesn't coincide, as the interest rate requested to use is 0.01 and the expected output has another values. **" ] }, { @@ -189,12 +443,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class CheckingAccount(BankAccount):\n", + " def __init__(self, account_number, balance=0, transaction_count=0, transaction_fee=1):\n", + " super().__init__(account_number, balance, transaction_count)\n", + " self.transaction_fee = transaction_fee\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + " print(\"Transactions reseted to 0.\")\n", + " return self.transaction_count\n", + "\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if total_fees > self.balance:\n", + " print(f\"Current balance is {self.balance}, so the total fees of {total_fees} can't be discounted.\")\n", + " else:\n", + " self.balance = self.balance - total_fees\n", + " print(f\"You made {self.transaction_count} transactions.\")\n", + " print(f\"A fee of {self.transaction_fee}$ is applied to each transaction.\")\n", + " print(f\"{total_fees}$ are discounted from your balance.\")\n", + " print(f\"Current actual balance: {self.balance}\")\n", + " self.transaction_count = 0\n", + " return self.balance\n", + "\n", + " def get_transaction_count(self):\n", + " print(f\"You made {self.transaction_count} this month.\")\n" ] }, { @@ -202,19 +480,348 @@ "id": "8217ec46-3eae-4a7c-9c7c-d4a87aac7e6d", "metadata": {}, "source": [ - "Example of testing CheckingAccount:\n", - "\n", - " - Create a new checking account with a balance of 500 dollars and a transaction fee of 2 dollars\n", - " - Deposit 100 dollars into the account \n", - " - Withdraw 50 dollars from the account \n", - " - Deduct the transaction fees from the account\n", - " - Get the current balance and transaction count\n", - " - Deposit 200 dollars into the account\n", - " - Withdraw 75 dollars from the account\n", - " - Deduct the transaction fees from the account\n", - " - Get the current balance and transaction count again\n", - " \n", - "\n", + "Example of testing CheckingAccount:" + ] + }, + { + "cell_type": "markdown", + "id": "59ba61a3", + "metadata": {}, + "source": [ + " - Create a new checking account with a balance of 500 dollars and a transaction fee of 2 dollars" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "5356e7e7", + "metadata": {}, + "outputs": [], + "source": [ + "checking1 = CheckingAccount('ES785469', 500, 0, 2)" + ] + }, + { + "cell_type": "markdown", + "id": "c8f816a0", + "metadata": {}, + "source": [ + " - Deposit 100 dollars into the account " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "db4d1ba3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 100 in ES785469\n" + ] + }, + { + "data": { + "text/plain": [ + "600" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.deposit(100)" + ] + }, + { + "cell_type": "markdown", + "id": "d10619eb", + "metadata": {}, + "source": [ + " - Withdraw 50 dollars from the account " + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "152b4bdd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Withdrawed 50 from ES785469\n" + ] + }, + { + "data": { + "text/plain": [ + "550" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.withdraw(50)" + ] + }, + { + "cell_type": "markdown", + "id": "a6e8ef4d", + "metadata": {}, + "source": [ + " - Deduct the transaction fees from the account" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "b113750f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You made 2 transactions.\n", + "A fee of 2$ is applied to each transaction.\n", + "4$ are discounted from your balance.\n", + "Current actual balance: 546\n" + ] + }, + { + "data": { + "text/plain": [ + "546" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.deduct_fees()" + ] + }, + { + "cell_type": "markdown", + "id": "b6ad01ac", + "metadata": {}, + "source": [ + " - Get the current balance and transaction count" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "b1b8cc44", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Current balance: 546'" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.get_balance()" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "9eb1a6f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You made 0 this month.\n" + ] + } + ], + "source": [ + "checking1.get_transaction_count()" + ] + }, + { + "cell_type": "markdown", + "id": "9f4ff9f0", + "metadata": {}, + "source": [ + " - Deposit 200 dollars into the account" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "5d5eaae0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 200 in ES785469\n" + ] + }, + { + "data": { + "text/plain": [ + "746" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.deposit(200)" + ] + }, + { + "cell_type": "markdown", + "id": "78f1b4a9", + "metadata": {}, + "source": [ + " - Withdraw 75 dollars from the account" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "aa434fb6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Withdrawed 75 from ES785469\n" + ] + }, + { + "data": { + "text/plain": [ + "671" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.withdraw(75)" + ] + }, + { + "cell_type": "markdown", + "id": "d1eb2435", + "metadata": {}, + "source": [ + " - Deduct the transaction fees from the account" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "fb819054", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You made 2 transactions.\n", + "A fee of 2$ is applied to each transaction.\n", + "4$ are discounted from your balance.\n", + "Current actual balance: 667\n" + ] + }, + { + "data": { + "text/plain": [ + "667" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.deduct_fees()" + ] + }, + { + "cell_type": "markdown", + "id": "7c80ed06", + "metadata": {}, + "source": [ + " - Get the current balance and transaction count again" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "01c3fefb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Current balance: 667'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checking1.get_balance()" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "e0665ab4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You made 0 this month.\n" + ] + } + ], + "source": [ + "checking1.get_transaction_count()" + ] + }, + { + "cell_type": "markdown", + "id": "3f3f2944", + "metadata": {}, + "source": [ "Expected output:\n", " \n", " Transaction fees of 4$ have been deducted from your account balance.\n", @@ -231,21 +838,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 +856,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,