Python Please include comments explaining and screenshot of output thank you! De
ID: 3812689 • Letter: P
Question
Python
Please include comments explaining and screenshot of output thank you!
Define a class for a restricted saving account that only permits three withdraw per months
class RestrictedSavingsAccount(SavingsAccount):
"""This class represents a restricted savings account."""
MAX_WITHDRAWALS = 3
def __init__(self, name, pin, balance = 0.0):
"""Same attributes as SavingsAccount, but with a counter for withdrawals."""
def withdraw(self, amount):
"""Restricts number of withdrawals to MAX_WITHDRAWALS."""
def resetCounter(self): self._counter = 0
Explanation / Answer
As SavingsAccount is not provided I created a dummy one to demonstrate.
# pastebin link for code: https://pastebin.com/i9Y314Cn
class SavingsAccount(object):
def __init__(self, name, pin, balance = 0.0):
pass
def withdraw(self, amount):
pass
class RestrictedSavingsAccount(SavingsAccount):
"""This class represents a restricted savings account."""
MAX_WITHDRAWALS = 3
def __init__(self, name, pin, balance = 0.0):
"""Same attributes as SavingsAccount, but with a counter for withdrawals."""
super(RestrictedSavingsAccount,self).__init__(name, bin, balance)
self._counter = 0
def withdraw(self, amount):
"""Restricts number of withdrawals to MAX_WITHDRAWALS."""
if self._counter == self.MAX_WITHDRAWALS:
print("Exceeded " + str(self.MAX_WITHDRAWALS) + " allowed withdrawls")
return
self._counter += 1
super(RestrictedSavingsAccount, self).withdraw(amount)
def resetCounter(self): self._counter = 0
r = RestrictedSavingsAccount("name", 1234, 0)
r.withdraw(10)
r.withdraw(20)
r.withdraw(10)
r.withdraw(30)
Sample run
Exceeded 3 allowed withdrawls
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.