Skip to content

Commit 50bbdc5

Browse files
committed
Solved lab
1 parent daaaff4 commit 50bbdc5

1 file changed

Lines changed: 130 additions & 16 deletions

File tree

lab-python-oop.ipynb

Lines changed: 130 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,53 @@
5656
},
5757
{
5858
"cell_type": "code",
59-
"execution_count": null,
59+
"execution_count": 35,
6060
"id": "21625526-3fae-4c55-bab5-f91940070681",
6161
"metadata": {},
6262
"outputs": [],
6363
"source": [
64-
"# your code goes here\n",
65-
"\n"
64+
"class BankAccount:\n",
65+
" account_number = 0\n",
66+
"\n",
67+
" def __init__(self, balance = 0):\n",
68+
" BankAccount.account_number += 1\n",
69+
" self.balance = balance\n",
70+
"\n",
71+
" def deposit(self, amount):\n",
72+
" self.balance += amount\n",
73+
"\n",
74+
" def withdraw(self, amount):\n",
75+
" if (amount > self.balance): print(\"Insufficient balance\")\n",
76+
" else: self.balance -= amount\n",
77+
"\n",
78+
" def get_balance(self):\n",
79+
" return self.balance\n",
80+
"\n",
81+
" @classmethod\n",
82+
" def get_account_number(self):\n",
83+
" return BankAccount.account_number"
6684
]
6785
},
6886
{
6987
"cell_type": "code",
70-
"execution_count": null,
88+
"execution_count": 37,
7189
"id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c",
7290
"metadata": {},
73-
"outputs": [],
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"Account 1 balance: 1000\n",
97+
"Account 1 number: 2\n",
98+
"Account 2 balance: 500\n",
99+
"Account 2 number: 2\n",
100+
"Account 1 balance after transactions: 1300\n",
101+
"Insufficient balance\n",
102+
"Account 2 balance after transactions: 500\n"
103+
]
104+
}
105+
],
74106
"source": [
75107
"# Testing the BankAccount class\n",
76108
"# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n",
@@ -117,12 +149,21 @@
117149
},
118150
{
119151
"cell_type": "code",
120-
"execution_count": null,
152+
"execution_count": 56,
121153
"id": "4f8848b5-05d3-4259-9e24-914537926778",
122154
"metadata": {},
123155
"outputs": [],
124156
"source": [
125-
"# your code goes here"
157+
"class SavingsAccount(BankAccount):\n",
158+
" def __init__(self, balance, interest_rate = 0.01):\n",
159+
" super().__init__(balance)\n",
160+
" self.interest_rate = interest_rate\n",
161+
"\n",
162+
" def add_interest(self):\n",
163+
" self.balance += self.balance * self.interest_rate\n",
164+
"\n",
165+
" def get_interest_rate(self):\n",
166+
" return self.interest_rate"
126167
]
127168
},
128169
{
@@ -151,12 +192,29 @@
151192
},
152193
{
153194
"cell_type": "code",
154-
"execution_count": null,
195+
"execution_count": 70,
155196
"id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30",
156197
"metadata": {},
157-
"outputs": [],
198+
"outputs": [
199+
{
200+
"name": "stdout",
201+
"output_type": "stream",
202+
"text": [
203+
"Current balance: 127.50\n",
204+
"Interest rate: 0.02\n"
205+
]
206+
}
207+
],
158208
"source": [
159-
"# your code goes here"
209+
"savings_account = SavingsAccount(100, 0.02)\n",
210+
"\n",
211+
"savings_account.deposit(50)\n",
212+
"savings_account.withdraw(25)\n",
213+
"\n",
214+
"savings_account.add_interest()\n",
215+
"\n",
216+
"print(\"Current balance: %.2f\" % savings_account.get_balance())\n",
217+
"print(\"Interest rate: %.2f\" % savings_account.get_interest_rate())"
160218
]
161219
},
162220
{
@@ -189,12 +247,39 @@
189247
},
190248
{
191249
"cell_type": "code",
192-
"execution_count": null,
250+
"execution_count": 96,
193251
"id": "3c883c6e-3cb8-4043-92d3-12409668a28e",
194252
"metadata": {},
195253
"outputs": [],
196254
"source": [
197-
"# your code goes here"
255+
"class CheckingAccount(BankAccount):\n",
256+
" def __init__(self, balance, transaction_fee = 1):\n",
257+
" super().__init__(balance)\n",
258+
" self.transaction_fee = transaction_fee\n",
259+
" self.transaction_count = 0\n",
260+
"\n",
261+
" def deposit(self, amount):\n",
262+
" super().deposit(amount)\n",
263+
" self.transaction_count += 1\n",
264+
"\n",
265+
" def withdraw(self, amount):\n",
266+
" super().withdraw(amount)\n",
267+
" self.transaction_count += 1\n",
268+
"\n",
269+
" def deduct_fees(self):\n",
270+
" transaction_fees = self.transaction_count * self.transaction_fee\n",
271+
" if (transaction_fees > self.balance):\n",
272+
" print(\"Insufficient balance\")\n",
273+
" else:\n",
274+
" print(\"Transaction fees of $%.2f have been deducted from your account balance\" % transaction_fees)\n",
275+
" self.balance -= transaction_fees\n",
276+
" self.reset_transactions()\n",
277+
"\n",
278+
" def reset_transactions(self):\n",
279+
" self.transaction_count = 0\n",
280+
"\n",
281+
" def get_transaction_count(self):\n",
282+
" return self.transaction_count"
198283
]
199284
},
200285
{
@@ -234,12 +319,41 @@
234319
},
235320
{
236321
"cell_type": "code",
237-
"execution_count": null,
322+
"execution_count": 98,
238323
"id": "faa5b148-c11b-4be0-b810-de8a7da81451",
239324
"metadata": {},
240-
"outputs": [],
325+
"outputs": [
326+
{
327+
"name": "stdout",
328+
"output_type": "stream",
329+
"text": [
330+
"Transaction fees of $4.00 have been deducted from your account balance\n",
331+
"Current balance: 546.00\n",
332+
"Transaction count: 0\n",
333+
"Transaction fees of $4.00 have been deducted from your account balance\n",
334+
"Current balance: 667.00\n",
335+
"Transaction count: 0\n"
336+
]
337+
}
338+
],
241339
"source": [
242-
"# your code goes here"
340+
"checking_account = CheckingAccount(500, 2)\n",
341+
"\n",
342+
"checking_account.deposit(100)\n",
343+
"checking_account.withdraw(50)\n",
344+
"\n",
345+
"checking_account.deduct_fees()\n",
346+
"\n",
347+
"print(\"Current balance: %.2f\" % checking_account.get_balance())\n",
348+
"print(\"Transaction count: %i\" % checking_account.get_transaction_count())\n",
349+
"\n",
350+
"checking_account.deposit(200)\n",
351+
"checking_account.withdraw(75)\n",
352+
"\n",
353+
"checking_account.deduct_fees()\n",
354+
"\n",
355+
"print(\"Current balance: %.2f\" % checking_account.get_balance())\n",
356+
"print(\"Transaction count: %i\" % checking_account.get_transaction_count())"
243357
]
244358
}
245359
],
@@ -259,7 +373,7 @@
259373
"name": "python",
260374
"nbconvert_exporter": "python",
261375
"pygments_lexer": "ipython3",
262-
"version": "3.9.13"
376+
"version": "3.12.4"
263377
}
264378
},
265379
"nbformat": 4,

0 commit comments

Comments
 (0)