From 5d0b3c64ec5000fba3810aff0c04a7cd0475c01e Mon Sep 17 00:00:00 2001 From: claragal Date: Tue, 1 Oct 2024 21:41:08 +0200 Subject: [PATCH] Lab extra Object Oriented Programming --- lab-python-oop.ipynb | 280 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 256 insertions(+), 24 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..658fa90 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "\n" + "class BankAccount:\n", + " # Class attribute to keep track of the number of accounts created\n", + " account_count = 0\n", + "\n", + " def __init__(self, initial_balance=0):\n", + " # Increment the account counter and assign a unique account number\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", + " print(f\"You have deposited {amount}. The new balance is {self.balance}\")\n", + " else:\n", + " print(\"The deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if 0 < amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"You have withdrew {amount}. The new balance is {self.balance}\")\n", + " else:\n", + " print(\"Insufficient funds or invalid amount.\")\n", + "\n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 1000\n", + "Account 1 number: 3\n", + "Account 2 balance: 500\n", + "Account 2 number: 4\n", + "You have deposited 500. The new balance is 1500\n", + "You have withdrew 200. The new balance is 1300\n", + "Account 1 balance after transactions: 1300\n", + "Insufficient funds or invalid amount.\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 +161,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You have deposited 50. The new balance is 150\n", + "You have withdrew 25. The new balance is 125\n", + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n" ] }, { @@ -151,12 +206,77 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You have deposited 50. The new balance is 150\n", + "You have withdrew 25. The new balance is 125\n", + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# Definition of the BankAccount class\n", + "class BankAccount:\n", + " # Class attribute to keep track of the number of accounts created\n", + " account_count = 0\n", + "\n", + " def __init__(self, initial_balance=0):\n", + " # Increment the account counter and assign a unique account number\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", + " print(f\"You have deposited {amount}. The new balance is {self.balance}\")\n", + " else:\n", + " print(\"The deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if 0 < amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"You have withdrew {amount}. The new balance is {self.balance}\")\n", + " else:\n", + " print(\"Insufficient funds or 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", + "\n", + "# Definition of the SavingsAccount class that inherits from BankAccount\n", + "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\n", + "\n", + "# Testing the SavingsAccount class\n", + "savings = SavingsAccount(100, 0.02) # Create a SavingsAccount object with a balance of $100 and an interest rate of 2%\n", + "\n", + "savings.deposit(50) # Deposit $50 into the savings account\n", + "savings.withdraw(25) # Withdraw $25 from the savings account\n", + "savings.add_interest() # Add interest to the savings account (using the provided 0.02)\n", + "\n", + "# Print the current balance and interest rate of the savings account\n", + "print(\"Current balance:\", savings.get_balance()) # This should print 127.5\n", + "print(\"Interest rate:\", savings.get_interest_rate()) # This should print 0.02" ] }, { @@ -187,16 +307,6 @@ "Note: To ensure that the transaction count is updated every time a deposit or withdrawal is made, we need to overwrite the deposit and withdraw methods inherited from the BankAccount class. " ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, { "cell_type": "markdown", "id": "8217ec46-3eae-4a7c-9c7c-d4a87aac7e6d", @@ -234,12 +344,134 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "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 after second deduction: 667\n", + "Transaction count after second deduction: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# Definition of the BankAccount class\n", + "class BankAccount:\n", + " # Class attribute to keep track of the number of accounts created\n", + " account_count = 0\n", + "\n", + " def __init__(self, initial_balance=0):\n", + " # Increment the account counter and assign a unique account number\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", + " print(f\"You have deposited {amount}. The new balance is {self.balance}\")\n", + " else:\n", + " print(\"The deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if 0 < amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"You have withdrew {amount}. The new balance is {self.balance}\")\n", + " else:\n", + " print(\"Insufficient funds or 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", + "\n", + "# Definition of the SavingsAccount class that inherits from BankAccount\n", + "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\n", + "\n", + "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", + " if amount > 0:\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + " else:\n", + " print(\"Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if 0 < amount <= self.balance:\n", + " self.balance -= amount\n", + " self.transaction_count += 1\n", + " else:\n", + " print(\"Insufficient funds or invalid amount.\")\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 funds to deduct transaction fees.\")\n", + " self.transaction_count = 0\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", + "# Example of testing CheckingAccount:\n", + "\n", + "# Create a new checking account with a balance of 500 dollars and a transaction fee of 2 dollars\n", + "checking = CheckingAccount(500, 2)\n", + "\n", + "# Deposit 100 dollars into the account \n", + "checking.deposit(100)\n", + "\n", + "# Withdraw 50 dollars from the account \n", + "checking.withdraw(50)\n", + "\n", + "# Deduct the transaction fees from the account\n", + "checking.deduct_fees()\n", + "\n", + "# Get the current balance and transaction count\n", + "print(\"Current balance:\", checking.get_balance()) # Should print the current balance after deducting fees\n", + "print(\"Transaction count:\", checking.get_transaction_count()) # Should print 0 as transaction count is reset\n", + "\n", + "# Deposit 200 dollars into the account\n", + "checking.deposit(200)\n", + "\n", + "# Withdraw 75 dollars from the account\n", + "checking.withdraw(75)\n", + "\n", + "# Deduct the transaction fees from the account\n", + "checking.deduct_fees()\n", + "\n", + "# Get the current balance and transaction count again\n", + "print(\"Current balance after second deduction:\", checking.get_balance()) # Should print the current balance after second deduction of fees\n", + "print(\"Transaction count after second deduction:\", checking.get_transaction_count()) # Should print 0 as transaction count is reset again" ] } ], @@ -259,7 +491,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.6" } }, "nbformat": 4,