From 5693890156f60e59eb81c5d80e8c9a1fdcf8bbc3 Mon Sep 17 00:00:00 2001 From: Claudia Palladino Date: Mon, 17 Nov 2025 19:19:00 +0000 Subject: [PATCH] Solved lab --- lab-python-oop.ipynb | 242 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 218 insertions(+), 24 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..cd6410f 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,39 +56,99 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "# Create a BankAccount class with attributes and methods\n", + "\n", + "class BankAccount:\n", + " \n", + " # Class attribute to keep track of tot number of accounts\n", + " account_count = 0 \n", + " def __init__(self, initial_balance=0):\n", + " BankAccount.account_count += 1\n", + " # Assign a unique account number based on the account count\n", + " self.account_number = BankAccount.account_count\n", + " # Balance is another class attribute; here the initial balance is set\n", + " self.balance = initial_balance\n", + " \n", + " # Add the specified amount to the account balance\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " return True\n", + " else:\n", + " print(\"Deposit should be positive\")\n", + "\n", + " # Subtracts the specified amount from the account balance\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"Deposited ${amount:.2f} to account {self.account_number}\")\n", + " else:\n", + " print(\"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\"Withdrew ${amount:.2f} from account {self.account_number}\")\n", + " else:\n", + " print(\"The balance insufficient\")\n", + " else:\n", + " print(\"Withdrawal amount must be positive\")\n", + "\n", + " # Returns the current balance of the account\n", + " def get_balance(self):\n", + " return self.balance\n", + " \n", + " # Returns the account number\n", + " def get_account_number(self):\n", + " return self.account_number" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 1000\n", + "Account 1 number: 14\n", + "Account 2 balance: 500\n", + "Account 2 number: 15\n", + "Deposited $300.00 to account 14\n", + "Withdrew $200.00 from account 14\n", + "Account 1 balance after transactions: $ 1100\n", + "The balance insufficient\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", "account1 = BankAccount(1000)\n", "account2 = BankAccount(500)\n", "\n", - "print(\"Account 1 balance:\", account1.get_balance()) # This should print 1000\n", - "print(\"Account 1 number:\", account1.get_account_number()) # This should print 1\n", + "print(\"Account 1 balance:\", account1.get_balance()) # this prints 1000\n", + "print(\"Account 1 number:\", account1.get_account_number()) # this prints 1\n", "\n", - "print(\"Account 2 balance:\", account2.get_balance()) #This should print 500\n", - "print(\"Account 2 number:\", account2.get_account_number()) #This should print 2\n", + "print(\"Account 2 balance:\", account2.get_balance()) # this prints 500\n", + "print(\"Account 2 number:\", account2.get_account_number()) # this prints 2\n", "\n", - "account1.deposit(500) # We depoist 500 in the first account\n", - "account1.withdraw(200) # We withdraw 200 in the first account\n", - "print(\"Account 1 balance after transactions:\", account1.get_balance()) # This should print 1300\n", + "account1.deposit(300) # depoist 300 in the first account\n", + "account1.withdraw(200) # withdraw 200 in the first account\n", + "print(\"Account 1 balance after transactions: $\", account1.get_balance()) # this prints 1100\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" + "account2.withdraw(600) # withdraw 600 in the 2nd account \n", + "print(\"Account 2 balance after transactions: $\", account2.get_balance()) # this prints insufficient balance, and still 500 in funds" ] }, { @@ -122,7 +182,29 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# Create a SavingsAccount class that inherits from the BankAccount class,\n", + "# and has the following additional attributes and methods:\n", + "\n", + "class SavingsAccount(BankAccount):\n", + "\n", + " # Define class attribute interest_rate (sets it to 0.01)\n", + " def __init__(self, initial_balance=0, interest_rate=0.01):\n", + " # Call the parent class constructor\n", + " super().__init__(initial_balance)\n", + " # Set interest rate\n", + " self.interest_rate = interest_rate\n", + "\n", + " # Define class methods: one method is used to add the interest earned to the account balance\n", + " def add_interest(self):\n", + " # Calculate interest\n", + " interest = self.balance * self.interest_rate\n", + " self.balance += interest\n", + " print(f\"Added ${interest:.2f} interest to account {self.account_number}\")\n", + "\n", + " # This method returns the interest rate for the account\n", + " def get_interest_rate(self):\n", + " return self.interest_rate\n", + "\n" ] }, { @@ -151,12 +233,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited $50.00 to account 13\n", + "Withdrew $25.00 from account 13\n", + "Added €2.50 interest to account 13\n", + "Balance: 127.5\n", + "Interest: 2.0%\n" + ] + } + ], "source": [ - "# your code goes here" + "# Test the SavingsAccount class by:\n", + "# creating a few instances of SavingsAccount,\n", + "# and making deposits and withdrawals,\n", + "# as well as adding interest.\n", + "\n", + "# Create SavingsAccount instances\n", + "savings_test = SavingsAccount(100, interest_rate=0.02) # 2% interest\n", + "\n", + "# Make deposits\n", + "savings_test.deposit(50)\n", + "\n", + "# Make withdraw\n", + "savings_test.withdraw(25)\n", + "\n", + "# Add interest\n", + "savings_test.add_interest()\n", + "\n", + "# Check balances\n", + "print(f\"Balance: {savings_test.get_balance():.1f}\")\n", + "\n", + "\n", + "# Check interest rates\n", + "print(f\"Interest: {savings_test.get_interest_rate()*100:.1f}%\")" ] }, { @@ -189,12 +305,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# Create a CheckingAccount class with attributes and methods\n", + "\n", + "class CheckingAccount(BankAccount):\n", + "\n", + " # Define attributes\n", + " def __init__(self, initial_balance=0, transaction_fee=1.0):\n", + " super().__init__(initial_balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + "\n", + " # Override deposit to count transactions\n", + " def deposit(self, amount):\n", + " super().deposit(amount)\n", + " self.transaction_count += 1\n", + "\n", + " # Override withdraw to count transactions\n", + " def withdraw(self, amount):\n", + " super().withdraw(amount)\n", + " self.transaction_count += 1\n", + "\n", + " # Deduct fees based on transaction count\n", + " def deduct_fees(self):\n", + " total_fee = self.transaction_count * self.transaction_fee\n", + " if self.balance >= total_fee:\n", + " self.balance -= total_fee\n", + " print(f\"Deducted ${total_fee:.2f} in transaction fees from account {self.account_number}.\")\n", + " else:\n", + " print(f\"Insufficient balance to deduct ${total_fee:.2f} in transaction fees.\")\n", + " # Reset transaction count after deduction\n", + " self.reset_transactions()\n", + "\n", + " # Reset transaction count\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " # Get transaction count\n", + " def get_transaction_count(self):\n", + " return self.transaction_count\n" ] }, { @@ -237,15 +390,56 @@ "execution_count": null, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited $100.00 to account 16\n", + "Withdrew $50.00 from account 16\n", + "Deducted $4.00 in transaction fees from account 16.\n", + "Balance after first fees deduction: $546.00\n", + "Transaction count after first deduction: 0\n", + "Deposited $200.00 to account 16\n", + "Withdrew $75.00 from account 16\n", + "Deducted $4.00 in transaction fees from account 16.\n", + "Balance after second fees deduction: $667.00\n", + "Transaction count after second deduction: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# Create a Testing CheckingAccount with $500 initial balance and $2 transaction fee\n", + "checking_test = CheckingAccount(initial_balance=500, transaction_fee=2)\n", + "\n", + "# Make some deposits and withdrawals\n", + "checking_test.deposit(100) # transaction_count = 1\n", + "checking_test.withdraw(50) # transaction_count = 2\n", + "\n", + "# Deduct the transaction fees from the account\n", + "checking_test.deduct_fees()\n", + "\n", + "\n", + "# Get current balance and transaction count\n", + "print(f\"Balance after first fees deduction: ${checking_test.get_balance():.2f}\")\n", + "print(f\"Transaction count after first deduction: {checking_test.get_transaction_count()}\")\n", + "\n", + "# Second set of transactions\n", + "checking_test.deposit(200) # transaction_count = 1\n", + "checking_test.withdraw(75) # transaction_count = 2\n", + "\n", + "# Deduct transaction fees\n", + "checking_test.deduct_fees() # 2 transactions × $2 = $4 deducted\n", + "\n", + "# Get current balance and transaction count\n", + "print(f\"Balance after second fees deduction: ${checking_test.get_balance():.2f}\")\n", + "print(f\"Transaction count after second deduction: {checking_test.get_transaction_count()}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -259,7 +453,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,