diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..6cd5346 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,192 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Creating bank accounts and performing transactions:\n", + "Account 1 Number: 1\n", + "Account 2 Number: 2\n", + "[Account 1] Deposited: 100. New balance: 100\n", + "[Account 1] Withdrew: 50. New balance: 50\n", + "Account 1 Balance: 50\n", + "[Account 2] Deposited: 200. New balance: 200\n", + "[Account 2] Insufficient funds. Current balance: 200, attempted withdrawal: 300\n", + "Account 2 Balance: 200\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "\n", + "# Version 1\n", + "\n", + "# Create a BankAccount class with the following attributes and methods:\n", + "# Attributes: account_number (a unique identifier for the bank account), balance (the current balance of the account. By default, is 0)\n", + "# Methods: deposit(amount) - adds the specified amount to the account balance, withdraw(amount) - subtracts the specified amount from the account balance,\n", + "# get_balance() - returns the current balance of the account, get_account_number() - returns the account number of the account\n", + "# Test the class by creating a few instances of BankAccount and making deposits and withdrawals.\n", + "# Ensure that the account_number attribute is unique for each instance of BankAccount.\n", + "\n", + "class BankAccount:\n", + " account_count = 0 # Class attribute to keep track of the number of accounts\n", + "\n", + " def __init__(self):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count # Unique account number\n", + " self.balance = 0 # Default balance\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"[Account {self.account_number}] Deposited: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"[Account {self.account_number}] Withdrew: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(\n", + " f\"[Account {self.account_number}] Insufficient funds. \"\n", + " f\"Current balance: {self.balance}, attempted withdrawal: {amount}\"\n", + " )\n", + "\n", + " else:\n", + " print(f\"[Account {self.account_number}] Withdrawal amount must be positive.\")\n", + "\n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number\n", + "print(\"Creating bank accounts and performing transactions:\") \n", + "\n", + "# Test the BankAccount class\n", + "account1 = BankAccount()\n", + "account2 = BankAccount()\n", + "print(f\"Account 1 Number: {account1.get_account_number()}\")\n", + "print(f\"Account 2 Number: {account2.get_account_number()}\")\n", + "account1.deposit(100)\n", + "account1.withdraw(50)\n", + "print(f\"Account 1 Balance: {account1.get_balance()}\")\n", + "account2.deposit(200)\n", + "account2.withdraw(300) # Should show insufficient funds\n", + "print(f\"Account 2 Balance: {account2.get_balance()}\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "852e997f", + "metadata": {}, + "source": [ + "**Note for Version 1 (default balance = 0, no initial balance parameter):**\n", + ">\n", + "*Note:*\n", + ">This implementation follows the original requirement strictly: each BankAccount is created with a default balance of 0 and the account number is automatically generated using the account_count class attribute. A limitation of this version is that it does not allow setting an initial balance at instantiation, so any starting funds must be added later using deposit()." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "0abf5db3", + "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", + "\n", + "# Version 2\n", + "\n", + "# Create a BankAccount class with the following attributes and methods:\n", + "# Attributes:\n", + "# - account_number: a unique identifier for the bank account\n", + "# - balance: the current balance of the account, which can be set at initialization (default is 0)\n", + "#\n", + "# Methods:\n", + "# - deposit(amount): adds the specified amount to the account balance\n", + "# - withdraw(amount): subtracts the specified amount from the account balance if sufficient funds are available\n", + "# - get_balance(): returns the current balance of the account\n", + "# - get_account_number(): returns the account number of the account\n", + "#\n", + "# Ensure that the account_number attribute is unique for each instance of BankAccount.\n", + "\n", + "\n", + "\n", + "class BankAccount:\n", + " account_count = 0\n", + "\n", + " def __init__(self, balance=0): # allow initial balance\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count\n", + " self.balance = balance # set initial balance\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"[Account {self.account_number}] Deposited: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"[Account {self.account_number}] Withdrew: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Insufficient funds. Balance: {self.balance}, attempted: {amount}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Withdrawal amount must be positive.\")\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" ] }, + { + "cell_type": "markdown", + "id": "1c97d6cc", + "metadata": {}, + "source": [ + "**Note for Version 2 (supports initial balance):**\n", + ">\n", + "*Note:*\n", + "> This implementation extends the original requirement by allowing an optional initial balance through __init__(self, balance=0). This makes it compatible with the lab’s provided testing code, which creates accounts with starting balances (e.g., BankAccount(1000)). The trade-off is that it slightly goes beyond the “balance is 0 by default” statement, although it still respects it (the default remains 0 when no value is passed)." + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "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] Deposited: 500. New balance: 1500\n", + "[Account 1] Withdrew: 200. New balance: 1300\n", + "Account 1 balance after transactions: 1300\n", + "[Account 2] Insufficient funds. Balance: 500, attempted: 600\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", @@ -122,7 +293,29 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Create a SavingsAccount class that inherits from the BankAccount class\n", + "# Additional attribute:\n", + "# - interest_rate (annual interest rate, default is 0.01)\n", + "# Additional methods:\n", + "# - add_interest() -> adds earned interest to the balance\n", + "# - get_interest_rate() -> returns the interest rate\n", + "\n", + "class SavingsAccount(BankAccount):\n", + "\n", + " def __init__(self, balance=0, interest_rate=0.01):\n", + " # Initialize attributes from the parent class\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", + " print(f\"[Account {self.account_number}] Added interest: {interest}. New balance: {self.balance}\")\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -151,12 +344,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[Account 1] Deposited: 50. New balance: 150\n", + "[Account 1] Withdrew: 25. New balance: 125\n", + "[Account 1] Added interest: 2.5. New balance: 127.5\n", + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Create a SavingsAccount object with a balance of $100 and interest rate of 2%\n", + "savings_account = SavingsAccount(balance=100, interest_rate=0.02)\n", + "\n", + "# Deposit $50\n", + "savings_account.deposit(50)\n", + "\n", + "# Withdraw $25\n", + "savings_account.withdraw(25)\n", + "\n", + "# Add interest\n", + "savings_account.add_interest()\n", + "\n", + "# Print current balance and interest rate\n", + "print(\"Current balance:\", savings_account.get_balance())\n", + "print(\"Interest rate:\", savings_account.get_interest_rate())" ] }, { @@ -189,12 +410,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Create a CheckingAccount class that inherits from the BankAccount class.\n", + "# Additional attributes:\n", + "# - transaction_fee (fee charged per transaction, default is 1)\n", + "# - transaction_count (number of transactions made in the current month)\n", + "# Additional methods:\n", + "# - deduct_fees(): deducts total transaction fees and resets the transaction count\n", + "# - reset_transactions(): resets the transaction count to 0\n", + "# - get_transaction_count(): returns the current transaction count\n", + "# Note: deposit and withdraw methods are overwritten to track transactions.\n", + "\n", + "class CheckingAccount(BankAccount):\n", + "\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", + " if amount > 0:\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + " print(f\"[Account {self.account_number}] Deposited: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " self.transaction_count += 1\n", + " print(f\"[Account {self.account_number}] Withdrew: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Insufficient funds.\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Withdrawal amount must be positive.\")\n", + "\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if total_fees <= self.balance:\n", + " self.balance -= total_fees\n", + " print(f\"Transaction fees of {total_fees}$ have been deducted from your account balance.\")\n", + " self.reset_transactions()\n", + " else:\n", + " print(\"Insufficient balance to deduct transaction fees.\")\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -234,18 +506,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[Account 2] Deposited: 100. New balance: 600\n", + "[Account 2] Withdrew: 50. New balance: 550\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "[Account 2] Deposited: 200. New balance: 746\n", + "[Account 2] Withdrew: 75. New balance: 671\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", + "\n", + "# Testing the CheckingAccount class\n", + "\n", + "checking_account = CheckingAccount(balance=500, transaction_fee=2)\n", + "\n", + "checking_account.deposit(100)\n", + "checking_account.withdraw(50)\n", + "checking_account.deduct_fees()\n", + "\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())\n", + "\n", + "checking_account.deposit(200)\n", + "checking_account.withdraw(75)\n", + "checking_account.deduct_fees()\n", + "\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -259,7 +566,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,