|
56 | 56 | }, |
57 | 57 | { |
58 | 58 | "cell_type": "code", |
59 | | - "execution_count": null, |
| 59 | + "execution_count": 35, |
60 | 60 | "id": "21625526-3fae-4c55-bab5-f91940070681", |
61 | 61 | "metadata": {}, |
62 | 62 | "outputs": [], |
63 | 63 | "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" |
66 | 84 | ] |
67 | 85 | }, |
68 | 86 | { |
69 | 87 | "cell_type": "code", |
70 | | - "execution_count": null, |
| 88 | + "execution_count": 37, |
71 | 89 | "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", |
72 | 90 | "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 | + ], |
74 | 106 | "source": [ |
75 | 107 | "# Testing the BankAccount class\n", |
76 | 108 | "# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n", |
|
117 | 149 | }, |
118 | 150 | { |
119 | 151 | "cell_type": "code", |
120 | | - "execution_count": null, |
| 152 | + "execution_count": 56, |
121 | 153 | "id": "4f8848b5-05d3-4259-9e24-914537926778", |
122 | 154 | "metadata": {}, |
123 | 155 | "outputs": [], |
124 | 156 | "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" |
126 | 167 | ] |
127 | 168 | }, |
128 | 169 | { |
|
151 | 192 | }, |
152 | 193 | { |
153 | 194 | "cell_type": "code", |
154 | | - "execution_count": null, |
| 195 | + "execution_count": 70, |
155 | 196 | "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", |
156 | 197 | "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 | + ], |
158 | 208 | "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())" |
160 | 218 | ] |
161 | 219 | }, |
162 | 220 | { |
|
189 | 247 | }, |
190 | 248 | { |
191 | 249 | "cell_type": "code", |
192 | | - "execution_count": null, |
| 250 | + "execution_count": 96, |
193 | 251 | "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", |
194 | 252 | "metadata": {}, |
195 | 253 | "outputs": [], |
196 | 254 | "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" |
198 | 283 | ] |
199 | 284 | }, |
200 | 285 | { |
|
234 | 319 | }, |
235 | 320 | { |
236 | 321 | "cell_type": "code", |
237 | | - "execution_count": null, |
| 322 | + "execution_count": 98, |
238 | 323 | "id": "faa5b148-c11b-4be0-b810-de8a7da81451", |
239 | 324 | "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 | + ], |
241 | 339 | "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())" |
243 | 357 | ] |
244 | 358 | } |
245 | 359 | ], |
|
259 | 373 | "name": "python", |
260 | 374 | "nbconvert_exporter": "python", |
261 | 375 | "pygments_lexer": "ipython3", |
262 | | - "version": "3.9.13" |
| 376 | + "version": "3.12.4" |
263 | 377 | } |
264 | 378 | }, |
265 | 379 | "nbformat": 4, |
|
0 commit comments