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 object Oriented Programming Pilar Escrig #56

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
162 changes: 141 additions & 21 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,54 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here\n",
"\n"
"class BankAccount:\n",
" account_count = 0\n",
" \n",
" def __init__(self, balance = 0):\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",
" \n",
" def withdraw(self, amount):\n",
" if 0 < amount <= self.balance:\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\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"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",
"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 +150,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"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",
" interes = self.balance * self.interest_rate\n",
" self.balance += interes\n",
" \n",
" def get_interest_rate(self):\n",
" return self.interest_rate"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "46cc82c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current balance: 127.5\n",
"Interest rate: 0.02\n"
]
}
],
"source": [
"objeto = SavingsAccount(100, 0.02)\n",
"objeto.deposit(50) \n",
"objeto.withdraw(25) \n",
"objeto.add_interest()\n",
"print(\"Current balance:\", objeto.get_balance())\n",
"print(\"Interest rate:\", objeto.get_interest_rate())"
]
},
{
Expand All @@ -149,16 +216,6 @@
" 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",
Expand Down Expand Up @@ -189,12 +246,75 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"class CheckingAccount(BankAccount):\n",
" def __init__ (self, balance = 0, transaction_fee = 1):\n",
" super().__init__(balance)\n",
" self.transaction_fee = transaction_fee \n",
" self.transaction_count = 0\n",
"\n",
" def deduct_fees(self):\n",
" total_fees = self.transaction_fee * self.transaction_count \n",
" if total_fees > self.balance:\n",
" print(\"No hay suficiente dinero en la cuenta\")\n",
" else:\n",
" self.balance -= total_fees\n",
" self.transaction_count = 0\n",
" print(f\"Transaction fees of {total_fees} have been deducted form your account balance\")\n",
"\n",
" def reset_transactins(self):\n",
" self.transaction_count = 0\n",
" \n",
" def get_transaction_count(self):\n",
" return self.transaction_count\n",
" \n",
" def deposit(self, amount):\n",
" if amount > 0:\n",
" self.transaction_count += 1\n",
" super().deposit(amount) \n",
" \n",
" def withdraw(self, amount):\n",
" if 0 < amount <= self.balance:\n",
" self.transaction_count += 1\n",
" super().withdraw(amount) "
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "5c7a365e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Transaction fees of 4 have been deducted form your account balance\n",
"Current balance: 546\n",
"Transaction count: 0\n",
"Transaction fees of 4 have been deducted form your account balance\n",
"Current balance: 667\n",
"Transaction count: 0\n"
]
}
],
"source": [
"cuenta = CheckingAccount(500, 2)\n",
"cuenta.deposit(100) \n",
"cuenta.withdraw(50)\n",
"cuenta.deduct_fees()\n",
"print(\"Current balance:\", cuenta.get_balance()) \n",
"print(\"Transaction count:\", cuenta.get_transaction_count()) \n",
"\n",
"cuenta.deposit(200) \n",
"cuenta.withdraw(75)\n",
"cuenta.deduct_fees()\n",
"print(\"Current balance:\", cuenta.get_balance()) \n",
"print(\"Transaction count:\", cuenta.get_transaction_count()) \n"
]
},
{
Expand Down Expand Up @@ -245,7 +365,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -259,7 +379,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.6.1"
}
},
"nbformat": 4,
Expand Down