Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab extra 1 #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 133 additions & 15 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here\n",
"\n"
"class BankAccount:\n",
" account_count = 1\n",
"\n",
" def __init__(self, balance = 0):\n",
" self.account_count = BankAccount.account_count\n",
" self.balance = balance\n",
" BankAccount.account_count +=1\n",
" \n",
" def deposit(self,amount):\n",
" self.balance += amount\n",
" def withdraw(self,amount):\n",
" if amount > self.balance:\n",
" print (f\"insufficient balance. Current balance {self.balance}€\" )\n",
" else:\n",
" self.balance -= amount\n",
" def get_balance(self):\n",
" return self.balance\n",
" def get_account_number(self):\n",
" return self.account_count"
]
},
{
"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: 1\n",
"Account 2 balance: 500\n",
"Account 2 number: 2\n",
"Account 1 balance after transactions: 1300\n",
"insufficient balance. Current balance 500€\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",
Expand Down Expand Up @@ -117,12 +149,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"class SavingsAccount(BankAccount):\n",
" def __init__(self, balance=0, interest_rate=0.01):\n",
" super().__init__(balance)\n",
" self.interest_rate = interest_rate\n",
"\n",
" def add_interest(self):\n",
" self.balance += self.balance*self.interest_rate\n",
" def get_interest_rate(self):\n",
" return self.interest_rate\n",
" "
]
},
{
Expand Down Expand Up @@ -151,12 +194,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 1 balance: 127.5 0.02\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"account1 = SavingsAccount(balance=100, interest_rate=0.02)\n",
"account1.deposit(50)\n",
"account1.withdraw(25)\n",
"account1.get_interest_rate\n",
"account1.add_interest()\n",
"print(\"Account 1 balance:\", account1.get_balance(), account1.get_interest_rate())"
]
},
{
Expand Down Expand Up @@ -189,12 +246,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"class CheckingAccount(BankAccount):\n",
" transaction_count = 0\n",
" \n",
" def __init__(self, balance=0,transaction_fee=1):\n",
" super().__init__(balance)\n",
" self.transaction_fee = transaction_fee\n",
" def withdraw(self, amount):\n",
" if amount > self.balance:\n",
" print (f\"insufficient balance. Current balance {self.balance}€\" )\n",
" else:\n",
" self.balance -= amount\n",
" CheckingAccount.transaction_count += 1\n",
" def deposit(self,amount):\n",
" self.balance += amount\n",
" def deduct_fees(self):\n",
" print(self.transaction_count)\n",
" print(self.transaction_fee)\n",
" self.balance -= self.transaction_fee*self.transaction_count\n",
" def reset_transactions(transaction_count):\n",
" transaction_count = 0\n",
" def get_transaction_count(transaction_count):\n",
" print(transaction_count)\n",
" "
]
},
{
Expand Down Expand Up @@ -234,13 +314,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "faa5b148-c11b-4be0-b810-de8a7da81451",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"Account 1 balance: 548 1\n",
"2\n",
"2\n",
"Account 1 balance: 669 2\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"cuenta1 = CheckingAccount(balance=500, transaction_fee=2)\n",
"cuenta1.deposit(100)\n",
"cuenta1.withdraw(50)\n",
"cuenta1.deduct_fees()\n",
"print(\"Account 1 balance:\", cuenta1.get_balance(), cuenta1.transaction_count)\n",
"cuenta1.deposit(200)\n",
"cuenta1.withdraw(75)\n",
"cuenta1.deduct_fees()\n",
"print(\"Account 1 balance:\", cuenta1.get_balance(), cuenta1.transaction_count)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e3e7abc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "883a231c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -259,7 +377,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.19"
}
},
"nbformat": 4,
Expand Down