From bf9e4d64695e33f0eb38a29c2e6fbd3cc5f5b5c8 Mon Sep 17 00:00:00 2001 From: luispabloaiello-da Date: Sun, 7 Sep 2025 17:17:10 +0200 Subject: [PATCH 1/3] Challenge 1: Bank Account | completed --- lab-python-oop.ipynb | 51 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..d4adeae 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "# BankAccount class with unique account_number and balance attribute\n", + "class BankAccount:\n", + " account_number = 0 # Class attribute to keep track of number of accounts\n", + " \n", + " def __init__(self, balance=0):\n", + " BankAccount.account_number += 1\n", + " self.account_number = BankAccount.account_number\n", + " self.balance = balance\n", + " \n", + " def get_account_number(self):\n", + " return self.account_number\n", + " \n", + " def get_balance(self):\n", + " return self.balance\n", + " \n", + " def deposit(self, deposit):\n", + " self.new_deposit = deposit\n", + " return self.balance + self.new_deposit\n", + " \n", + " def withdraw(self, withdraw):\n", + " self.new_withdraw = withdraw\n", + " if self.new_withdraw <= self.balance:\n", + " return self.balance - self.new_withdraw\n", + " else:\n", + " print(\"Insufficient balance\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "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: 1000\n", + "Insufficient balance\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 +282,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -259,7 +296,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.6" } }, "nbformat": 4, From 16528662bcabbb0d992e8e0de771db3184e2310f Mon Sep 17 00:00:00 2001 From: luispabloaiello-da Date: Sun, 7 Sep 2025 20:36:49 +0200 Subject: [PATCH 2/3] Solved lab --- lab-python-oop.ipynb | 114 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 15 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index d4adeae..5d1c565 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,11 +56,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 86, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ + "# your code goes here\n", "# BankAccount class with unique account_number and balance attribute\n", "class BankAccount:\n", " account_number = 0 # Class attribute to keep track of number of accounts\n", @@ -78,19 +79,21 @@ " \n", " def deposit(self, deposit):\n", " self.new_deposit = deposit\n", - " return self.balance + self.new_deposit\n", + " self.balance += self.new_deposit\n", + " return self.balance\n", " \n", " def withdraw(self, withdraw):\n", " self.new_withdraw = withdraw\n", " if self.new_withdraw <= self.balance:\n", - " return self.balance - self.new_withdraw\n", + " self.balance -= self.new_withdraw\n", + " return self.balance\n", " else:\n", " print(\"Insufficient balance\")" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 87, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, "outputs": [ @@ -102,15 +105,14 @@ "Account 1 number: 1\n", "Account 2 balance: 500\n", "Account 2 number: 2\n", - "Account 1 balance after transactions: 1000\n", + "Account 1 balance after transactions: 1300\n", "Insufficient balance\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", + "# your code goes here\n", "account1 = BankAccount(1000)\n", "account2 = BankAccount(500)\n", "\n", @@ -154,12 +156,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 88, "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_earned = self.balance * self.interest_rate\n", + " self.balance += interest_earned\n", + " return self.balance\n", + " \n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -188,12 +202,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "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" + "# your code goes here\n", + "savings_account = SavingsAccount(balance=100, interest_rate=0.02)\n", + "\n", + "savings_account.deposit(50)\n", + "savings_account.withdraw(25)\n", + "savings_account.add_interest()\n", + "\n", + "print(\"Current balance:\", savings_account.get_balance())\n", + "print(\"Interest rate:\", savings_account.get_interest_rate())" ] }, { @@ -226,12 +257,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current balance: 667\n", + "Transaction count: 0\n" + ] + } + ], "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", + " def deposit(self, amount):\n", + " super().deposit(amount)\n", + " self.transaction_count += 1\n", + " \n", + " def withdraw(self, amount):\n", + " if 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 of {total_fees}$ have been deducted from your account balance.\")\n", + " else:\n", + " print(\"Insufficient balance to deduct transaction 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\n", + " \n", + "checking_account = CheckingAccount(balance=500, transaction_fee=2)\n", + "checking_account.deposit(100)\n", + "checking_account.withdraw(50)\n", + "checking_account.deduct_fees()\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())\n", + "checking_account.deposit(200)\n", + "checking_account.withdraw(75)\n", + "checking_account.deduct_fees()\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())" ] }, { From afd1ed5bfb63fa16fd94d44d34a7a2783a4efb3d Mon Sep 17 00:00:00 2001 From: luispabloaiello-da Date: Sun, 7 Sep 2025 20:38:03 +0200 Subject: [PATCH 3/3] Solved lab --- lab-python-oop.ipynb | 46 ++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 5d1c565..b5b16c9 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -257,7 +257,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": null, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [ @@ -303,19 +303,7 @@ " self.transaction_count = 0\n", " \n", " def get_transaction_count(self):\n", - " return self.transaction_count\n", - " \n", - "checking_account = CheckingAccount(balance=500, transaction_fee=2)\n", - "checking_account.deposit(100)\n", - "checking_account.withdraw(50)\n", - "checking_account.deduct_fees()\n", - "print(\"Current balance:\", checking_account.get_balance())\n", - "print(\"Transaction count:\", checking_account.get_transaction_count())\n", - "checking_account.deposit(200)\n", - "checking_account.withdraw(75)\n", - "checking_account.deduct_fees()\n", - "print(\"Current balance:\", checking_account.get_balance())\n", - "print(\"Transaction count:\", checking_account.get_transaction_count())" + " return self.transaction_count" ] }, { @@ -355,12 +343,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 92, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current balance: 667\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "checking_account = CheckingAccount(balance=500, transaction_fee=2)\n", + "checking_account.deposit(100)\n", + "checking_account.withdraw(50)\n", + "checking_account.deduct_fees()\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())\n", + "checking_account.deposit(200)\n", + "checking_account.withdraw(75)\n", + "checking_account.deduct_fees()\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())" ] } ],