Skip to content
Open

Lab #116

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
304 changes: 281 additions & 23 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@
"*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,
Expand All @@ -91,6 +80,94 @@
"print(\"Account 2 balance after transactions:\", account2.get_balance())# This should print insufficient balance, and still 500 in funds"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 1 balance: 1000\n",
"Account 1 number: 1zq\n",
"Account 1 balance after transactions: 1300\n"
]
}
],
"source": [
"account_number = \"1zq\"\n",
"\n",
"class BankAccount: \n",
" def __init__(self, account_number, balance = 0):\n",
" self.account_number = account_number\n",
" self.balance = balance\n",
"\n",
" def deposit(self, amount):\n",
" self.balance += amount\n",
"\n",
" def withdraw(self, amount):\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",
"\n",
"\n",
"account1 = BankAccount(\"1zq\", 1000)\n",
"print(\"Account 1 balance:\", account1.get_balance())\n",
"print(\"Account 1 number:\", account1.get_account_number())\n",
"account1.deposit(500) \n",
"account1.withdraw(200) \n",
"print(\"Account 1 balance after transactions:\", account1.get_balance()) \n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4759dc7b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 2 balance: 500\n",
"Account 2 number: 1zq\n",
"Account 2 balance after transactions: -100\n"
]
}
],
"source": [
"account_number = \"1zq\"\n",
"\n",
"class BankAccount: \n",
" def __init__(self, account_number, balance = 0):\n",
" self.account_number = account_number\n",
" self.balance = balance\n",
"\n",
" def deposit(self, amount):\n",
" self.balance += amount\n",
"\n",
" def withdraw(self, amount):\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",
" \n",
"account2 = BankAccount(\"1zq\", 500)\n",
"print(\"Account 2 balance:\", account2.get_balance()) \n",
"print(\"Account 2 number:\", account2.get_account_number())\n",
"account2.withdraw(600) \n",
"print(\"Account 2 balance after transactions:\", account2.get_balance())\n"
]
},
{
"cell_type": "markdown",
"id": "929305ed-67cb-4094-8af2-4fa9b751832a",
Expand All @@ -117,12 +194,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"months = 12\n",
"year = 1\n",
"interest = 0.01\n",
"\n",
"class SavingsAccount: \n",
" def __init__(self, account_number, balance = 0, interest_rate = 0.02):\n",
" self.account_number = account_number\n",
" self.balance = balance\n",
" self.interest_rate = self.balance * (1 + interest_rate/months) ** (months * year)\n",
"\n",
" def deposit(self, amount):\n",
" self.balance += amount\n",
"\n",
" def withdraw(self, amount):\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",
" \n",
" def add_interest(self): \n",
" return self.balance * (1 + interest/months) ** (months * year)\n",
" \n",
" def get_interest_rate(self):\n",
" return self.interest_rate"
]
},
{
Expand Down Expand Up @@ -151,12 +254,67 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 0 balance: 100\n",
"After deposit: 150\n",
"After withdrawal: 125\n",
"After interest: 126.26\n"
]
}
],
"source": [
"# your code goes here"
"months = 12\n",
"year = 1\n",
"interest\n",
"\n",
"class SavingsAccount: \n",
" def __init__(self, account_number, balance=0, interest_rate=0.02):\n",
" self.account_number = account_number\n",
" self.balance = balance\n",
" self.interest_rate = interest_rate\n",
"\n",
" def deposit(self, amount):\n",
" self.balance += amount\n",
"\n",
" def withdraw(self, amount):\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",
" \n",
" def add_interest(self): \n",
" self.balance = self.balance * (1 + interest/months) ** (months * year)\n",
"\n",
" def get_interest_rate(self):\n",
" return self.interest_rate\n",
"\n",
"\n",
"# Create account\n",
"account0 = SavingsAccount(\"1zq\", 100, 0.02)\n",
"\n",
"print(\"Account 0 balance:\", account0.get_balance())\n",
"\n",
"account0.deposit(50)\n",
"print(\"After deposit:\", account0.get_balance())\n",
"\n",
"account0.withdraw(25)\n",
"print(\"After withdrawal:\", account0.get_balance())\n",
"\n",
"account0.add_interest()\n",
"print(\"After interest:\", round(account0.get_balance(), 2))\n",
" \n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -192,9 +350,62 @@
"execution_count": null,
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 0 balance: 500\n",
"After deposit: 600\n",
"After withdrawal: 550\n",
"Transaction count: 4\n",
"After deducting fees: 546\n",
"Start Transaction fee\n",
"After deposit: 746\n",
"After withdrawal: 671\n",
"After deducting fees: 667\n",
"Transaction count: 4\n"
]
}
],
"source": [
"# your code goes here"
"months = 12\n",
"year = 1\n",
"fee = 2\n",
"\n",
"\n",
"class CheckingAccount: \n",
" def __init__(self, account_number, balance=0, transaction_fee=2):\n",
" self.account_number = account_number\n",
" self.balance = balance\n",
" self.transaction_fee = transaction_fee\n",
" self.transaction_count = 0 # start counter at 0\n",
"\n",
" def deposit(self, amount):\n",
" self.balance += amount\n",
" self.transaction_count += 1 # increase counter\n",
"\n",
" def withdraw(self, amount):\n",
" self.balance -= amount\n",
" self.transaction_count += 1 # increase counter\n",
" \n",
" def get_balance(self):\n",
" return self.balance\n",
"\n",
" def get_account_number(self):\n",
" return self.account_number\n",
" \n",
" def get_transaction_count(self):\n",
" return self.transaction_fee * self.transaction_count\n",
" \n",
" def deduct_fees(self):\n",
" total_fees = self.transaction_fee * self.transaction_count\n",
" self.balance -= total_fees\n",
" \n",
" def reset_transactions(self):\n",
" self.transaction_count = 0\n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -234,18 +445,65 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "faa5b148-c11b-4be0-b810-de8a7da81451",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account 0 balance: 500\n",
"After deposit: 600\n",
"After withdrawal: 550\n",
"Transaction count: 4\n",
"After deducting fees: 546\n",
"Start Transaction fee\n",
"After deposit: 746\n",
"After withdrawal: 671\n",
"After deducting fees: 667\n",
"Transaction count: 4\n"
]
}
],
"source": [
"# your code goes here"
"# Create account\n",
"account0 = CheckingAccount(\"1zq\", 500, 2)\n",
"\n",
"print(\"Account 0 balance:\", account0.get_balance())\n",
"\n",
"account0.deposit(100)\n",
"print(\"After deposit:\", account0.get_balance())\n",
"\n",
"account0.withdraw(50)\n",
"print(\"After withdrawal:\", account0.get_balance())\n",
"\n",
"account0.get_transaction_count()\n",
"print(\"Transaction count:\", account0.get_transaction_count())\n",
"\n",
"account0.deduct_fees()\n",
"print(\"After deducting fees:\", account0.get_balance())\n",
"\n",
"account0.reset_transactions()\n",
"print(\"Start Transaction fee\")\n",
"\n",
"account0.deposit(200)\n",
"print(\"After deposit:\", account0.get_balance())\n",
"\n",
"account0.withdraw(75)\n",
"print(\"After withdrawal:\", account0.get_balance())\n",
"\n",
"account0.deduct_fees()\n",
"print(\"After deducting fees:\", account0.get_balance())\n",
"\n",
"account0.get_transaction_count()\n",
"print(\"Transaction count:\", account0.get_transaction_count())\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -259,7 +517,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down