From c6fd89470db84d8d2df63332c8348a4dca402f1e Mon Sep 17 00:00:00 2001 From: Moses209 Date: Fri, 11 Apr 2025 15:25:43 +0200 Subject: [PATCH 1/2] python_oop --- .../lab-python-oop-checkpoint.ipynb | 267 ++++++++++++++++++ lab-python-oop.ipynb | 2 +- 2 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-oop-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-oop-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-oop-checkpoint.ipynb new file mode 100644 index 0000000..c13bc58 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-oop-checkpoint.ipynb @@ -0,0 +1,267 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Object Oriented Programming" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: Practice how to work with OOP using classes, objects, and inheritance to create robust, maintainable, and scalable code." + ] + }, + { + "cell_type": "markdown", + "id": "20c3e882-9625-471e-afb4-48a4f40c5d1b", + "metadata": { + "tags": [] + }, + "source": [ + "## Challenge 1: Bank Account" + ] + }, + { + "cell_type": "markdown", + "id": "fe982bf8-9610-4de3-aa93-256db5a05903", + "metadata": {}, + "source": [ + "Create a BankAccount class with the following attributes and methods:\n", + "\n", + "Attributes:\n", + "\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", + "\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", + "Instructions:\n", + "\n", + "- Create a BankAccount class with the above attributes and methods.\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", + "*Hint: create a class attribute account_count. The account_count class attribute is used to keep track of the total number of bank accounts that have been created using the BankAccount class. Every time a new BankAccount object is created, the account_count attribute is incremented by one. This can be useful for various purposes, such as generating unique account numbers or monitoring the growth of a bank's customer base.*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21625526-3fae-4c55-bab5-f91940070681", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", + "metadata": {}, + "outputs": [], + "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", + "\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", + "\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", + "\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" + ] + }, + { + "cell_type": "markdown", + "id": "929305ed-67cb-4094-8af2-4fa9b751832a", + "metadata": {}, + "source": [ + "## Challenge 2: Savings Account\n", + "\n", + "Create a SavingsAccount class that inherits from the BankAccount class. The SavingsAccount class should have the following additional attributes and methods:\n", + "\n", + "Attributes:\n", + "\n", + "- interest_rate (the annual interest rate for the savings account. By default - if no value is provided - sets it to 0.01)\n", + "\n", + "Methods:\n", + "\n", + "- add_interest() - adds the interest earned to the account balance\n", + "- get_interest_rate() - returns the interest rate for the account\n", + "\n", + "Instructions:\n", + "\n", + "- Create a SavingsAccount class that inherits from the BankAccount class and has the above attributes and methods.\n", + "- Test the class by creating a few instances of SavingsAccount and making deposits and withdrawals, as well as adding interest." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f8848b5-05d3-4259-9e24-914537926778", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "d2c84bdb-e7d1-491d-9b0e-194288702c02", + "metadata": {}, + "source": [ + "Example of testing the SavingsAccount\n", + "\n", + "- Create a SavingsAccount object with a balance of $100 and interest rate of 2%\n", + "\n", + "- Deposit $50 into the savings account\n", + "\n", + "- Withdraw $25 from the savings account\n", + "\n", + "- Add interest to the savings account (use the default 0.01)\n", + "\n", + "- Print the current balance and interest rate of the savings account\n", + "\n", + "Expected output:\n", + " \n", + " Current balance: 127.5\n", + " \n", + " Interest rate: 0.02" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "a5455a88-a8d1-47a6-86b0-7c70647b4f31", + "metadata": {}, + "source": [ + "### Challenge 3: Checking Account\n", + "\n", + "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", + "\n", + "Methods:\n", + "\n", + "- deduct_fees() - deducts transaction fees from the account balance based on the number of transactions made in the current month and the transaction fee per transaction. The method calculates the total fees by multiplying the transaction count with the transaction fee, and deducts the fees from the account balance if it's sufficient. If the balance is insufficient, it prints an error message. Otherwise, it prints how much it's been deducted. After deducting the fees, the method resets the 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", + "Instructions:\n", + "\n", + "- Create a CheckingAccount class that inherits from the BankAccount class and has the above attributes and methods.\n", + "- Test the class by creating a few instances of CheckingAccount and making deposits, withdrawals, and transactions to deduct fees.\n", + "\n", + "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", + "metadata": {}, + "source": [ + "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", + " - Deposit 100 dollars into the account \n", + " - Withdraw 50 dollars from the account \n", + " - Deduct the transaction fees from the account\n", + " - Get the current balance and transaction count\n", + " - Deposit 200 dollars into the account\n", + " - Withdraw 75 dollars from the account\n", + " - Deduct the transaction fees from the account\n", + " - Get the current balance and transaction count again\n", + " \n", + "\n", + "Expected output:\n", + " \n", + " Transaction fees of 4$ have been deducted from your account balance.\n", + " \n", + " Current balance: 546\n", + " \n", + " Transaction count: 0\n", + " \n", + " Transaction fees of 4$ have been deducted from your account balance.\n", + " \n", + " Current balance: 667\n", + " \n", + " Transaction count: 0\n", + "\n", + "Note: *the print \"Transaction fees of 4$ have been deducted from your account balance\" is done in the method deduct_fees*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "faa5b148-c11b-4be0-b810-de8a7da81451", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..07794b6 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -259,7 +259,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From f778b95300c424d48f4494cfd5e535a539ed6ce1 Mon Sep 17 00:00:00 2001 From: Moses209 Date: Fri, 11 Apr 2025 17:34:11 +0200 Subject: [PATCH 2/2] oop assignment --- .../lab-python-oop-checkpoint.ipynb | 118 +++++++++++++++++- lab-python-oop.ipynb | 116 ++++++++++++++++- 2 files changed, 229 insertions(+), 5 deletions(-) diff --git a/.ipynb_checkpoints/lab-python-oop-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-oop-checkpoint.ipynb index c13bc58..f4f7f7f 100644 --- a/.ipynb_checkpoints/lab-python-oop-checkpoint.ipynb +++ b/.ipynb_checkpoints/lab-python-oop-checkpoint.ipynb @@ -61,10 +61,122 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "# your code goes here\n" ] }, + { + "cell_type": "code", + "execution_count": 11, + "id": "9cef8c5a-e783-400d-83a8-b5672e7cea14", + "metadata": {}, + "outputs": [], + "source": [ + "class BankAccount:\n", + " _account_counter_ = 0\n", + " def __init__(self):\n", + " \n", + " self.account_number = BankAccount._account_counter_\n", + " BankAccount._account_counter_ += 1\n", + " self.balance = 0\n", + " \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", + " \n", + " def withdraw(self, amount):\n", + " if amount <= self.balance and amount > 0:\n", + " self.balance -= amount\n", + " print(f\"Withdrew {amount:.2f} from account {self.account_number}\")\n", + " elif amount <= 0:\n", + " print(\"Withdrawal amount must be positive\")\n", + " else:\n", + " print(\"Insufficient funds\")\n", + " \n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1b63231-22dd-483b-8f48-6834797237c7", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f8ca9b0d-89c7-43ee-9fdf-f2224cd08c9b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 500.00 to account 0\n", + "Withdrew 43.00 from account 0\n", + "Account 0 balance: 457.00\n" + ] + } + ], + "source": [ + "account1 = BankAccount()\n", + "#account2 = BankAccount(500)\n", + "account1.deposit(500)\n", + "account1.withdraw(43)\n", + "print(f\"Account {account1.get_account_number()} balance: {account1.get_balance():.2f}\")\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4fdd229-e522-4c8e-9aea-68e9087d2692", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "2afb4f1d-d498-4abf-bcf4-b8c57ea33590", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 500.00 to account 1\n", + "Withdrew 170.00 from account 1\n", + "Account 1 balance: 330.00\n" + ] + } + ], + "source": [ + "account2 = BankAccount()\n", + "#account2 = BankAccount(500)\n", + "account2.deposit(500)\n", + "account2.withdraw(170)\n", + "print(f\"Account {account2.get_account_number()} balance: {account2.get_balance():.2f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7336314d-0e14-4971-b692-a05d10f59a92", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -259,7 +371,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 07794b6..f4f7f7f 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -61,10 +61,122 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here\n", - "\n" + "# your code goes here\n" ] }, + { + "cell_type": "code", + "execution_count": 11, + "id": "9cef8c5a-e783-400d-83a8-b5672e7cea14", + "metadata": {}, + "outputs": [], + "source": [ + "class BankAccount:\n", + " _account_counter_ = 0\n", + " def __init__(self):\n", + " \n", + " self.account_number = BankAccount._account_counter_\n", + " BankAccount._account_counter_ += 1\n", + " self.balance = 0\n", + " \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", + " \n", + " def withdraw(self, amount):\n", + " if amount <= self.balance and amount > 0:\n", + " self.balance -= amount\n", + " print(f\"Withdrew {amount:.2f} from account {self.account_number}\")\n", + " elif amount <= 0:\n", + " print(\"Withdrawal amount must be positive\")\n", + " else:\n", + " print(\"Insufficient funds\")\n", + " \n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1b63231-22dd-483b-8f48-6834797237c7", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f8ca9b0d-89c7-43ee-9fdf-f2224cd08c9b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 500.00 to account 0\n", + "Withdrew 43.00 from account 0\n", + "Account 0 balance: 457.00\n" + ] + } + ], + "source": [ + "account1 = BankAccount()\n", + "#account2 = BankAccount(500)\n", + "account1.deposit(500)\n", + "account1.withdraw(43)\n", + "print(f\"Account {account1.get_account_number()} balance: {account1.get_balance():.2f}\")\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4fdd229-e522-4c8e-9aea-68e9087d2692", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "2afb4f1d-d498-4abf-bcf4-b8c57ea33590", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited 500.00 to account 1\n", + "Withdrew 170.00 from account 1\n", + "Account 1 balance: 330.00\n" + ] + } + ], + "source": [ + "account2 = BankAccount()\n", + "#account2 = BankAccount(500)\n", + "account2.deposit(500)\n", + "account2.withdraw(170)\n", + "print(f\"Account {account2.get_account_number()} balance: {account2.get_balance():.2f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7336314d-0e14-4971-b692-a05d10f59a92", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null,