From befb56c333b9af31c0c2fe34b94acf8d2b281200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Erro?= Date: Sat, 8 Feb 2025 12:28:15 +0100 Subject: [PATCH 1/3] chore: commit save point --- lab-python-oop.ipynb | 62 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..1951cbd 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,71 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "\n" + "class BankAccount:\n", + " # Atributo de clase para llevar la cuenta de los IDs únicos\n", + " account_count = 0\n", + "\n", + " def __init__(self, initial_balance=0):\n", + " \"\"\"Inicializa la cuenta con un número único y un saldo inicial.\"\"\"\n", + " BankAccount.account_count += 1 # Incrementa el contador global\n", + " self.account_number = BankAccount.account_count # Asigna número de cuenta único\n", + " self.balance = initial_balance # Establece el saldo inicial\n", + "\n", + " def deposit(self, amount):\n", + " \"\"\"Añade una cantidad al saldo de la cuenta.\"\"\"\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"Depositado: {amount}. Nuevo saldo: {self.balance}\")\n", + " else:\n", + " print(\"El monto a depositar debe ser mayor que 0.\")\n", + "\n", + " def withdraw(self, amount):\n", + " \"\"\"Retira una cantidad del saldo de la cuenta si hay fondos suficientes.\"\"\"\n", + " if amount > self.balance:\n", + " print(\"Fondos insuficientes.\")\n", + " elif amount > 0:\n", + " self.balance -= amount\n", + " print(f\"Retirado: {amount}. Nuevo saldo: {self.balance}\")\n", + " else:\n", + " print(\"El monto a retirar debe ser mayor que 0.\")\n", + "\n", + " def get_balance(self):\n", + " \"\"\"Devuelve el saldo actual de la cuenta.\"\"\"\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " \"\"\"Devuelve el número único de la cuenta.\"\"\"\n", + " return self.account_number" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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", + "Depositado: 500. Nuevo saldo: 1500\n", + "Retirado: 200. Nuevo saldo: 1300\n", + "Account 1 balance after transactions: 1300\n", + "Fondos insuficientes.\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", @@ -245,7 +295,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -259,7 +309,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From d2f2a8ec3b09575266b3fd3f83b1e4fce94d44fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Erro?= Date: Sat, 8 Feb 2025 12:30:00 +0100 Subject: [PATCH 2/3] chore: commit save point --- lab-python-oop.ipynb | 59 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 1951cbd..580da6b 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -170,9 +170,47 @@ "execution_count": null, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Savings Account 1\n", + "Saldo inicial: 2000\n", + "Tasa de interés: 0.05\n", + "Depositado: 500. Nuevo saldo: 2500\n", + "Interés añadido: 125.00. Nuevo saldo: 2625.00\n", + "\n", + "Savings Account 2\n", + "Saldo inicial: 1500\n", + "Tasa de interés: 0.01\n", + "Retirado: 100. Nuevo saldo: 1400\n", + "Interés añadido: 14.00. Nuevo saldo: 1414.00\n", + "\n", + "Balances finales:\n", + "Savings Account 1 balance: 2625.0\n", + "Savings Account 2 balance: 1414.0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "class SavingsAccount(BankAccount):\n", + " def __init__(self, initial_balance=0, interest_rate=0.01):\n", + " \"\"\"Inicializa la cuenta de ahorros con saldo y tasa de interés.\"\"\"\n", + " super().__init__(initial_balance) # Llama al constructor de BankAccount\n", + " self.interest_rate = interest_rate # Establece la tasa de interés\n", + "\n", + " def add_interest(self):\n", + " \"\"\"Añade los intereses ganados al saldo de la cuenta.\"\"\"\n", + " interest = self.balance * self.interest_rate\n", + " self.balance += interest\n", + " print(f\"Interés añadido: {interest:.2f}. Nuevo saldo: {self.balance:.2f}\")\n", + "\n", + " def get_interest_rate(self):\n", + " \"\"\"Devuelve la tasa de interés de la cuenta.\"\"\"\n", + " return self.interest_rate\n" ] }, { @@ -206,7 +244,22 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "# Crear una cuenta de ahorros con saldo inicial de $100 y tasa de interés del 2% (0.02)\n", + "savings_test = SavingsAccount(100, 0.02)\n", + "\n", + "# Depositar $50\n", + "savings_test.deposit(50)\n", + "\n", + "# Retirar $25\n", + "savings_test.withdraw(25)\n", + "\n", + "# Agregar interés\n", + "savings_test.add_interest()\n", + "\n", + "# Imprimir el saldo actual y la tasa de interés\n", + "print(\"\\nCurrent balance:\", savings_test.get_balance()) # Debería imprimir 127.5\n", + "print(\"Interest rate:\", savings_test.get_interest_rate()) # Debería imprimir 0.02\n" ] }, { From d463cda3f29a17e82e678e89087d117123bdbf61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Erro?= Date: Sat, 8 Feb 2025 12:32:56 +0100 Subject: [PATCH 3/3] chore: commit save point --- lab-python-oop.ipynb | 94 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 5 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 580da6b..6eacf54 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -292,12 +292,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "class CheckingAccount(BankAccount):\n", + " def __init__(self, initial_balance=0, transaction_fee=1):\n", + " \"\"\"Inicializa la cuenta corriente con saldo y tarifa por transacción.\"\"\"\n", + " super().__init__(initial_balance) # Llama al constructor de BankAccount\n", + " self.transaction_fee = transaction_fee # Establece la tarifa por transacción\n", + " self.transaction_count = 0 # Contador de transacciones\n", + "\n", + " def deposit(self, amount):\n", + " \"\"\"Sobreescribe el método deposit para registrar la transacción.\"\"\"\n", + " super().deposit(amount) # Llama al método de la clase base\n", + " self.transaction_count += 1 # Incrementa el contador de transacciones\n", + "\n", + " def withdraw(self, amount):\n", + " \"\"\"Sobreescribe el método withdraw para registrar la transacción.\"\"\"\n", + " if self.balance >= amount:\n", + " super().withdraw(amount) # Llama al método de la clase base\n", + " self.transaction_count += 1 # Incrementa el contador de transacciones\n", + " else:\n", + " print(\"Fondos insuficientes para la retirada.\")\n", + "\n", + " def deduct_fees(self):\n", + " \"\"\"Deduce las tarifas acumuladas de la cuenta basado en las transacciones realizadas.\"\"\"\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if self.balance >= total_fees:\n", + " self.balance -= total_fees\n", + " print(f\"Transaction fees of {total_fees}$ have been deducted from your account balance.\")\n", + " else:\n", + " print(\"Fondos insuficientes para deducir las tarifas.\")\n", + " self.transaction_count = 0 # Reinicia el contador de transacciones\n", + "\n", + " def reset_transactions(self):\n", + " \"\"\"Reinicia el contador de transacciones a 0.\"\"\"\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " \"\"\"Devuelve el número actual de transacciones realizadas.\"\"\"\n", + " return self.transaction_count" ] }, { @@ -337,12 +374,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Depositado: 100. Nuevo saldo: 600\n", + "Retirado: 50. Nuevo saldo: 550\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "Depositado: 200. Nuevo saldo: 746\n", + "Retirado: 75. Nuevo saldo: 671\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "\n", + "Current balance: 667\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# Crear una nueva cuenta corriente con un saldo de 500 dólares y una tarifa de transacción de 2 dólares\n", + "checking_test = CheckingAccount(500, 2)\n", + "\n", + "# Depositar 100 dólares en la cuenta\n", + "checking_test.deposit(100)\n", + "\n", + "# Retirar 50 dólares de la cuenta\n", + "checking_test.withdraw(50)\n", + "\n", + "# Deducir las tarifas de transacción\n", + "checking_test.deduct_fees()\n", + "\n", + "# Obtener el saldo actual y el conteo de transacciones\n", + "print(\"\\nCurrent balance:\", checking_test.get_balance()) # Debería imprimir 546\n", + "print(\"Transaction count:\", checking_test.get_transaction_count()) # Debería imprimir 0\n", + "\n", + "# Depositar 200 dólares en la cuenta\n", + "checking_test.deposit(200)\n", + "\n", + "# Retirar 75 dólares de la cuenta\n", + "checking_test.withdraw(75)\n", + "\n", + "# Deducir las tarifas de transacción nuevamente\n", + "checking_test.deduct_fees()\n", + "\n", + "# Obtener el saldo actual y el conteo de transacciones nuevamente\n", + "print(\"\\nCurrent balance:\", checking_test.get_balance()) # Debería imprimir 667\n", + "print(\"Transaction count:\", checking_test.get_transaction_count()) # Debería imprimir 0\n" ] } ],