diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..fb96681 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", + "class BankAccount:\n", + " account_count = 0\n", + "\n", + " def __init__(self, balance = 0, overdraft_limit = 0):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count\n", + " self.balance = balance\n", + " self.overdraft_limit = overdraft_limit\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " return True\n", + " else:\n", + " print(\"El deposito debe ser positivo\")\n", + " \n", + " def withdraw(self, amount):\n", + " if amount <= 0:\n", + " print(\"La retirada debe ser positiva\")\n", + " elif self.balance + self.overdraft_limit >= amount:\n", + " self.balance -= amount\n", + " else:\n", + " print(f\"Cantidad de tu balance no es suficiente: {self.balance}\")\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", + "\n", "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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", + "Account 1 balance after transactions: 1300\n", + "Cantidad de tu balance no es suficiente: 500\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 +163,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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", + "\n", + " def add_interest(self):\n", + " interest = self.balance * self.interest_rate\n", + " self.balance += interest\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate\n", + " " ] }, { @@ -151,12 +209,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Saldo actual: 127.5\n", + "Tasa de interes: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "savings = SavingsAccount(balance = 100, interest_rate = 0.02)\n", + "\n", + "savings.deposit(50)\n", + "\n", + "savings.withdraw(25)\n", + "\n", + "savings.add_interest()\n", + "\n", + "print(\"Saldo actual:\", savings.get_balance())\n", + "print(\"Tasa de interes:\", savings.get_interest_rate())" ] }, { @@ -189,12 +266,55 @@ }, { "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, balance=0, transaction_fee = 1):\n", + " super().__init__(balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + "\n", + "# Sobrescribimos el deposito para contar transacciones\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + " else:\n", + " print(\"El desposito debe de ser positivo\")\n", + "\n", + "# Sobrescribimos el retiro para contar transacciones\n", + " def withdraw(self, amount):\n", + " if amount <= 0:\n", + " print(\"La retirada debe de ser positiva\")\n", + " elif self.balance + self.overdraft_limit >= amount:\n", + " self.balance -= amount\n", + " self.transaction_count += 1\n", + " else:\n", + " print(f\"Cantidad insuficiente. Saldo disponible {self.balance}\")\n", + "\n", + "# Deducimos las comisiones de las transacciones\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if self.balance >= total_fees:\n", + " self.balance -= total_fees\n", + " print(f\"Se han deducido {total_fees}$ en comisiones\")\n", + " else:\n", + " print(\"Saldo insuficiente para deducir las comisioes\")\n", + " self.transaction_count = 0\n", + "\n", + "# Resetear contador de transacciones\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + "# Obtener numero actual de transacciones\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -234,18 +354,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Se han deducido 4$ en comisiones\n", + "Saldo actual: 546\n", + "Cantidad de transacciones: 0\n", + "Se han deducido 4$ en comisiones\n", + "Saldo actual: 667\n", + "Cantidad de transacciones: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "new_checking_account = CheckingAccount(balance = 500, transaction_fee = 2)\n", + "\n", + "new_checking_account.deposit(100)\n", + "\n", + "new_checking_account.withdraw(50)\n", + "\n", + "new_checking_account.deduct_fees()\n", + "\n", + "print(\"Saldo actual:\", new_checking_account.get_balance())\n", + "print(\"Cantidad de transacciones:\", new_checking_account.get_transaction_count())\n", + "\n", + "new_checking_account.deposit(200)\n", + "\n", + "new_checking_account.withdraw(75)\n", + "\n", + "new_checking_account.deduct_fees()\n", + "\n", + "print(\"Saldo actual:\", new_checking_account.get_balance())\n", + "print(\"Cantidad de transacciones:\", new_checking_account.get_transaction_count())" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -259,7 +412,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,