From 0bcfaf2aa77f8f6140e9404d237bc53c4db0227b Mon Sep 17 00:00:00 2001 From: Benja022 <113633123+Benja022@users.noreply.github.com> Date: Wed, 2 Oct 2024 09:30:55 +0200 Subject: [PATCH] Solved lab --- lab-python-oop.ipynb | 179 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 162 insertions(+), 17 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..a5c31a9 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,63 @@ }, { "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", + " account_count = 0\n", + "\n", + " def __init__(self, initial_balance=0):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count\n", + " self.balance = initial_balance\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " else:\n", + " print(\"Invalid amount\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " else:\n", + " print(\"Insufficient balance\")\n", + " else:\n", + " print(\"Invalid amount\")\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": 6, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 1000\n", + "Account 1 number: 5\n", + "Account 2 balance: 500\n", + "Account 2 number: 6\n", + "Account 1 balance after transactions: 1300\n", + "Insufficient balance\n", + "Account 2 balance after transactions: 500\n", + "Account 1 - Número: 5, Saldo: 1300\n", + "Account 2 - Número: 6, Saldo: 500\n" + ] + } + ], "source": [ "# Testing the BankAccount class\n", "# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n", @@ -88,7 +130,10 @@ "print(\"Account 1 balance after transactions:\", account1.get_balance()) # This should print 1300\n", "\n", "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" + "print(\"Account 2 balance after transactions:\", account2.get_balance())# This should print insufficient balance, and still 500 in funds\n", + "\n", + "print(f\"Account 1 - Número: {account1.get_account_number()}, Saldo: {account1.get_balance()}\")\n", + "print(f\"Account 2 - Número: {account2.get_account_number()}, Saldo: {account2.get_balance()}\")" ] }, { @@ -117,12 +162,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class SavingsAccount(BankAccount):\n", + " def __init__(self, initial_balance=0, interest_rate=0.01):\n", + " super().__init__(initial_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" ] }, { @@ -151,12 +206,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# Creating an instance of the SavingsAccount class\n", + "savings_account = SavingsAccount(100, 0.02)\n", + "\n", + "# Depositing 50 in the savings account\n", + "savings_account.deposit(50)\n", + "\n", + "# Withdrawing 25 from the savings account\n", + "savings_account.withdraw(25)\n", + "\n", + "# Adding interest to the savings account (use the default 0.01)\n", + "savings_account.add_interest()\n", + "\n", + "# Printing the current balance and interest rate of the savings account\n", + "print(f'Current balance: {savings_account.get_balance()}')\n", + "print(f'Interest rate: {savings_account.get_interest_rate()}')" ] }, { @@ -189,12 +267,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "class CheckingAccount(BankAccount):\n", + " def __init__(self, initial_balance=0, transaction_fee=1):\n", + " super().__init__(initial_balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\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 self.balance >= total_fees:\n", + " self.balance -= total_fees\n", + " print(f\"Transaction fees: {total_fees}$ have been deducted from your account balance.\")\n", + " else:\n", + " print(\"Insufficient funds to deduct fees\")\n", + " self.reset_transactions()\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -234,12 +339,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction fees: 4$ have been deducted from your account balance.\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "Transaction fees: 4$ have been deducted from your account balance.\n", + "Current balance: 667\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# Create a new checking account with a balance of 500 dollars and a transaction fee of 2 dollars\n", + "checking_account = CheckingAccount(500, 2)\n", + "\n", + "# Deposit 100 dollars into the account \n", + "checking_account.deposit(100)\n", + "\n", + "# Withdraw 50 dollars from the account \n", + "checking_account.withdraw(50)\n", + "\n", + "# Deduct the transaction fees from the account\n", + "checking_account.deduct_fees()\n", + "\n", + "# Get the current balance and transaction count\n", + "print(f\"Current balance: {checking_account.get_balance()}\")\n", + "print(f\"Transaction count: {checking_account.get_transaction_count()}\")\n", + "\n", + "# Deposit 200 dollars into the account\n", + "checking_account.deposit(200)\n", + "\n", + "# Withdraw 75 dollars from the account\n", + "checking_account.withdraw(75)\n", + "\n", + "# Deduct the transaction fees from the account\n", + "checking_account.deduct_fees()\n", + "\n", + "# Get the current balance and transaction count again\n", + "print(f\"Current balance: {checking_account.get_balance()}\")\n", + "print(f\"Transaction count: {checking_account.get_transaction_count()}\")" ] } ], @@ -259,7 +404,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.9" } }, "nbformat": 4,