diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..b5256d5 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", + "class BankAccount: \n", + " account_counter = 0 # Class variable to keep track of account numbers\n", + " \n", + " def __init__(self, initial_balance):\n", + " self.balance = initial_balance\n", + " BankAccount.account_counter += 1 # Increment the counter for each new account\n", + " self.account_number = BankAccount.account_counter # Assign unique account number\n", + " \n", + " def get_balance(self):\n", + " return self.balance\n", + " \n", + " def get_account_number(self):\n", + " return self.account_number\n", + " \n", + " def deposit(self, amount):\n", + " self.balance += amount\n", + " return self.balance\n", + " \n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " return \"insufficient balance\"\n", + " self.balance -= amount\n", + " return self.balance\n", + "\n", "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "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", + "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 +153,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "\n", + "class SavingsAccount(BankAccount):\n", + " def __init__(self, initial_balance, interest_rate=0.01):\n", + " # Initialize the parent class (BankAccount)\n", + " super().__init__(initial_balance)\n", + " # Add the interest_rate attribute specific to SavingsAccount\n", + " self.interest_rate = interest_rate\n", + " \n", + " def add_interest(self):\n", + " # Add interest to the balance\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\n", + "\n", + "\n", + " " ] }, { @@ -151,12 +205,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "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" + "# Create a SavingsAccount object with a balance of $100 and interest rate of 2%\n", + "savings = SavingsAccount(100, 0.02)\n", + "\n", + "# Deposit $50 into the savings account\n", + "savings.deposit(50)\n", + "\n", + "# Withdraw $25 from the savings account\n", + "savings.withdraw(25)\n", + "\n", + "# Add interest to the savings account (interest rate is already set to 0.02)\n", + "savings.add_interest()\n", + "\n", + "# Print the current balance and interest rate of the savings account\n", + "print(\"Current balance:\", savings.get_balance()) # Expected: 127.5\n", + "print(\"Interest rate:\", savings.get_interest_rate()) # Expected: 0.02" ] }, { @@ -189,12 +266,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# Create a CheckingAccount class that inherits from the BankAccount class. The CheckingAccount class should have the following additional attributes and methods:\n", + "\n", + "# Attributes:\n", + "\n", + "# transaction_fee (the fee charged per transaction. By default is 1$)\n", + "# transaction_count (the number of transactions made in the current month)\n", + "class CheckingAccount(BankAccount):\n", + " def __init__(self, initial_balance, transaction_fee=1):\n", + " # Initialize the parent class (BankAccount)\n", + " super().__init__(initial_balance)\n", + " # Add the transaction_fee attribute specific to CheckingAccount\n", + " self.transaction_fee = transaction_fee\n", + " # Initialize the transaction_count attribute to 0\n", + " self.transaction_count = 0\n", + " def deduct_fee(self):\n", + " self.balance -= self.transaction_fee\n", + " \n", + " def reset_transaction(self):\n", + " self.transaction_count = 0\n", + " \n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -234,12 +332,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction fees of 2 have been deducted from your account balance.\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "Transaction fees of 2 have been deducted from your account balance.\n", + "Current balance: 667\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "checkAccount = CheckingAccount(500, 2)\n", + "\n", + "checkAccount.deduct_fee()\n", + "print(f'Transaction fees of {checkAccount.transaction_fee} have been deducted from your account balance.')\n", + "# Deposit 100 dollars into the account \n", + "checkAccount.deposit(100) \n", + "# Withdraw 50 dollars from the account\n", + "checkAccount.withdraw(50)\n", + "# Deduct the transaction fees from the account\n", + "checkAccount.deduct_fee()\n", + "# Reset the transaction count\n", + "checkAccount.reset_transaction()\n", + "# Get the current balance and transaction count\n", + "print(\"Current balance:\", checkAccount.get_balance())\n", + "print(\"Transaction count:\", checkAccount.get_transaction_count())\n", + "# Deposit 200 dollars into the account\n", + "checkAccount.deposit(200)\n", + "# Withdraw 75 dollars from the account\n", + "checkAccount.deduct_fee()\n", + "print(f'Transaction fees of {checkAccount.transaction_fee} have been deducted from your account balance.')\n", + "checkAccount.withdraw(75)\n", + "# Deduct the transaction fees from the account\n", + "checkAccount.deduct_fee()\n", + "# Reset the transaction count\n", + "checkAccount.reset_transaction()\n", + "# Get the current balance and transaction count\n", + "print(\"Current balance:\", checkAccount.get_balance())\n", + "print(\"Transaction count:\", checkAccount.get_transaction_count())\n" ] } ], @@ -259,7 +397,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.9" } }, "nbformat": 4,