From c79149c8dd1fd7415a19c1c8d1a626d0afd1a636 Mon Sep 17 00:00:00 2001 From: Eppu Henriksson Date: Sat, 26 Jul 2025 17:43:53 +0200 Subject: [PATCH] [FT-072025] Eppu Henriksson --- lab-python-oop.ipynb | 208 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 191 insertions(+), 17 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..68f514a 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", - "\n" + "\n", + "class BankAccount:\n", + " \"\"\"\n", + " Creates a BankAccount class with the following attributes and methods:\n", + "\n", + " Attributes:\n", + " - account_number (a unique identifier for the bank account)\n", + " - balance (the current balance of the account. By 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\n", + " - get_balance() - returns the current balance of the account\n", + " - get_account_number() - returns the account number of the account\n", + " \"\"\"\n", + " account_count = 0\n", + " def __init__(self, balance):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count\n", + " self.balance = balance\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " else:\n", + " print(\"Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount <= 0:\n", + " print(\"Withdrawal amount must be positive.\")\n", + " elif amount > self.balance:\n", + " print(\"Insufficient balance.\")\n", + " else:\n", + " self.balance -= 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": 35, "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", + "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", @@ -117,12 +170,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "class SavingsAccount(BankAccount):\n", + " \"\"\"\n", + " Creates a SavingsAccount class that inherits from the BankAccount class. \n", + " The SavingsAccount class has the following additional attributes and methods:\n", + "\n", + " Attributes:\n", + " - interest_rate (the annual interest rate for the savings account. \n", + " By default - if no value is provided - sets it to 0.01)\n", + "\n", + " Methods:\n", + " - add_interest() - adds the interest earned to the account balance\n", + " - get_interest_rate() - returns the interest rate for the account\n", + " \"\"\" \n", + " def __init__(self, balance, interest_rate = 0.01):\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", + " \n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -151,12 +228,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 3 balance after transactions: 125\n", + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "account3 = SavingsAccount(100, 0.02)\n", + "\n", + "account3.deposit(50)\n", + "account3.withdraw(25)\n", + "print(\"Account 3 balance after transactions:\", account3.get_balance()) # This should print 125\n", + "\n", + "account3.add_interest()\n", + "print(\"Current balance: \", account3.get_balance())\n", + "print(\"Interest rate: \", account3.get_interest_rate())" ] }, { @@ -189,12 +285,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "class CheckingAccount(BankAccount):\n", + " \"\"\"\n", + " Creates a CheckingAccount class that inherits from the BankAccount class. \n", + " The CheckingAccount class has the following additional attributes and methods:\n", + "\n", + " Attributes:\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", + "\n", + " Methods:\n", + " - deduct_fees() - deducts transaction fees from the account balance based on the number of transactions \n", + " made in the current month and the transaction fee per transaction. The method calculates the total \n", + " fees by multiplying the transaction count with the transaction fee, and deducts the fees from the \n", + " account balance if it's sufficient. If the balance is insufficient, it prints an error message. \n", + " Otherwise, it prints how much it's been deducted. After deducting the fees, the method resets the \n", + " transaction count to 0.\n", + " - reset_transactions() - resets the transaction count to 0\n", + " - get_transaction_count() - returns the current transaction count for the account\n", + " \"\"\"\n", + " def __init__(self, balance, 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", + " super().withdraw(amount)\n", + " self.transaction_count += 1\n", + "\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_fee * self.transaction_count\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", + " self.transaction_count = 0\n", + " else:\n", + " print(f\"Insufficient funds\")\n", + " \n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -234,20 +376,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 4 balance after transactions: 550\n", + "Transaction fees of 4 have been deducted from your account balance.\n", + "Account 4 balance after transactions: 546\n", + "Transaction count: 0\n", + "Account 4 balance after transactions: 671\n", + "Transaction fees of 4 have been deducted from your account balance.\n", + "Account 4 balance after transactions: 667\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "account4 = CheckingAccount(500, 2)\n", + "\n", + "account4.deposit(100)\n", + "account4.withdraw(50)\n", + "print(\"Account 4 balance after transactions:\", account4.get_balance()) # This should print 550\n", + "\n", + "account4.deduct_fees()\n", + "print(\"Account 4 balance after transactions:\", account4.get_balance()) # This should print 546\n", + "print(\"Transaction count:\", account4.get_transaction_count())\n", + "\n", + "account4.deposit(200)\n", + "account4.withdraw(75)\n", + "print(\"Account 4 balance after transactions:\", account4.get_balance()) # This should print 671\n", + "\n", + "account4.deduct_fees()\n", + "print(\"Account 4 balance after transactions:\", account4.get_balance()) # This should print 667\n", + "print(\"Transaction count:\", account4.get_transaction_count())" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -259,7 +433,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,