Skip to content
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
258 changes: 217 additions & 41 deletions lab-python-oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,66 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"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": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have saved 300 in your account.\n",
"Your current balance is 1300.\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"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",
"class BankAccount:\n",
"\n",
" account_count = 0 # class attribute \n",
"\n",
" def __init__(self,account_number,balance):\n",
" self.account_number = account_number\n",
" self.balance = balance\n",
" BankAccount.account_count += 1 # class attribute \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",
" def deposit(self,amount):\n",
" self.balance += amount\n",
" print(f\"You have saved {amount} in your account.\")\n",
" \n",
" def withdraw(self,amount):\n",
" self.balance -= amount\n",
" print(f\"You have withdrawed {amount} from your account!\")\n",
" \n",
" def get_balance(self):\n",
" print(f\"Your current balance is {self.balance}.\") \n",
" \n",
" def get_account_number(self):\n",
" return self.account_number\n",
" \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",
"# create an object \n",
"account1 = BankAccount(1,1000)\n",
"account1.balance\n",
"account1.deposit(300)\n",
"account1_balance = account1.get_balance()\n",
"account1_balance\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",
"# create another object \n",
"account2 = BankAccount(2,100)\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"
"# check number of objects created\n",
"BankAccount.account_count\n"
]
},
{
Expand Down Expand Up @@ -120,9 +147,52 @@
"execution_count": null,
"id": "4f8848b5-05d3-4259-9e24-914537926778",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have saved 50 in your account.\n",
"You have withdrawed 25 from your account!\n",
"Your current balance is 125.\n",
"Interest rate 0.02 has been added!\n",
"Your current balance is 127.5.\n",
"Your current interest rate is :0.02.\n"
]
}
],
"source": [
"# your code goes here"
"class SavingsAccount(BankAccount):\n",
" \n",
" def __init__(self,account_number,balance,interest_rate = 0.01):\n",
" super().__init__(account_number, balance) \n",
" self.interest_rate = interest_rate\n",
"\n",
" def add_interest(self):\n",
" self.balance = self.balance * (1+self.interest_rate)\n",
" print(f\"Interest rate {self.interest_rate} has been added!\")\n",
" \n",
" def get_interest_rate(self):\n",
" print(f\"Your current interest rate is :{self.interest_rate}.\")\n",
"\n",
"# create saving object\n",
"Saving1 = SavingsAccount(1,100,0.02)\n",
"\n",
"# call parent class method - deposit\n",
"Saving1.deposit(50)\n",
"\n",
"# call parent class method - withdraw, get_balance\n",
"Saving1.withdraw(25)\n",
"Saving1.get_balance()\n",
"\n",
"# call child class method - add_interest\n",
"Saving1.add_interest()\n",
"\n",
"# call parent class method - get_balance\n",
"Saving1.get_balance()\n",
"\n",
"# call child class method - get_interest_rate\n",
"Saving1.get_interest_rate()"
]
},
{
Expand All @@ -149,16 +219,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 @@ -194,9 +254,125 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"class CheckingAccount(BankAccount):\n",
" \n",
" def __init__(self,account_number,balance,transaction_fee = 1):\n",
" super().__init__(account_number,balance)\n",
" self.transaction_fee = transaction_fee\n",
" self.transaction_count = 0\n",
" \n",
" def deposit(self,amount):\n",
" self.transaction_count += 1 \n",
" self.balance += amount\n",
" print(f\"You have deposited {amount} in your account!\")\n",
" \n",
" def withdraw (self,amount):\n",
" self.transaction_count += 1\n",
" self.balance -= amount\n",
" print(f\"You have withdraw {amount} from your account!\")\n",
"\n",
" def deduct_fees(self):\n",
" print(f\"Transaction fees of {self.transaction_fee * self.transaction_count} have been deducted from your account balance.\")\n",
" self.balance = self.balance - self.transaction_fee * self.transaction_count\n",
" \n",
" def reset_transactions(self):\n",
" self.transaction_count = 0\n",
" \n",
" def get_transaction_count(self):\n",
" print(f\"Your current transaction count is {self.transaction_count}!\")\n"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "f0c3021d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have deposited 100 in your account!\n",
"You have withdraw 50 from your account!\n",
"Your current balance is 550.\n",
"Transaction fees of 4 have been deducted from your account balance.\n",
"Your current balance is 546.\n",
"Your current transaction count is 0!\n",
"You have deposited 200 in your account!\n",
"You have withdraw 75 from your account!\n",
"Your current balance is 671.\n",
"Transaction fees of 4 have been deducted from your account balance.\n",
"Your current balance is 667.\n"
]
}
],
"source": [
"# create checking object \n",
"checking = CheckingAccount(1,500,2)\n",
"\n",
"# call deposit method from child class \n",
"checking.deposit(100)\n",
"\n",
"# call withdraw method from child class \n",
"checking.withdraw(50)\n",
"\n",
"# call get_balance method from parent class\n",
"checking.get_balance()\n",
"\n",
"# call deduct_fees method from child class\n",
"checking.deduct_fees()\n",
"\n",
"# call get_balance method from parent class\n",
"checking.get_balance()\n",
"\n",
"# call reset_transactions method from child class\n",
"checking.reset_transactions()\n",
"\n",
"# call get_transaction_count method from child class\n",
"checking.get_transaction_count()\n",
"\n",
"# call deposit method from child class \n",
"checking.deposit(200)\n",
"\n",
"# call withdraw method from child class \n",
"checking.withdraw(75)\n",
"\n",
"# call get_balance method from parent class\n",
"checking.get_balance()\n",
"\n",
"# call deduct_fees method from child class\n",
"checking.deduct_fees()\n",
"\n",
"# call get_balance method from parent class\n",
"checking.get_balance()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa1730c6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have deposited 100 in your account!\n",
"You have withdraw 50 from your account!\n",
"Your current balance is 550.\n",
"Transaction fees of 4 have been deducted from your account balance.\n",
"Your current balance is 546.\n",
"Your current transaction count is 0!\n",
"You have deposited 200 in your account!\n",
"You have withdraw 75 from your account!\n",
"Your current balance is 671.\n",
"Transaction fees of 4 have been deducted from your account balance.\n",
"Your current balance is 667.\n"
]
}
],
"source": []
},
{
"cell_type": "markdown",
"id": "8217ec46-3eae-4a7c-9c7c-d4a87aac7e6d",
Expand Down Expand Up @@ -245,7 +421,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "py310",
"language": "python",
"name": "python3"
},
Expand All @@ -259,7 +435,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.18"
}
},
"nbformat": 4,
Expand Down