In: Computer Science
Python 3.5:
Consider the Account class that we developed previously.
Task 1:
Review the code in the constructor in the Account class. What happens if we attempt to make a new account with a negative balance? Use exception handling to raise an exception if a balance less that 0 is given. Add a message to the constructor saying "Warning! Your balance is equal to or less than 0!".
Task 2:
Next, in the Account class's withdraw() method, add code to check if the amount given exceeds the account's balance. If it does, then raise a ValueError. Add a message to the withdraw() method saying "ERROR: Insufficient funds!". The balance in the account should remain unchanged.
Also, add code to check if the amount of withdrawal is zero. You should raise a ValueError if the amount is less than or equal to zero.
Note: you must submit the entire class definition.
For example:
Test | Output |
try: account1 = Account(1122, 0, 3.35) except ValueError as x: print (x) |
Warning! Your balance is equal to or less than 0. |
try: account2 = Account(1122, 100, 3.35) account2.withdraw(200) except ValueError as x: print (x) print(account2.get_balance()) |
ERROR: Insufficient funds! 100 |
try: account2 = Account(1122, 100, 3.35) account2.withdraw(0) except ValueError as x: print (x) print(account2.get_balance()) |
ERROR: Invalid amount! 100 |
Account Code:
------------------------------------------------------
class Account:
def __init__(self, id, balance: int=None, annual_interest_rate:
int=None):
if balance > 0:
self.__id = id
if balance == None:
self.__balance = 100
else:
self.__balance = balance
if annual_interest_rate == None:
self.__annualInterest = 0
else:
self.__annualInterest = annual_interest_rate
else:
raise ("Warning! Your balance is equal to or less than 0!")
def get_id(self):
return self.__id
def get_balance(self):
return self.__balance
def get_annual_interest_rate(self):
return self.__annualInterest
def get_monthly_interest_rate(self):
return (self.__annualInterest/12)
def get_monthly_interest(self):
return (self.__balance * (self.__annualInterest/12)/100)
def deposit(self, amount: int=None):
if amount == None:
deposit = 0
else:
deposit = amount
self.__balance = self.__balance + amount
return
def withdraw(self, amount: int=None):
if amount == None:
withdraw = 0
else:
withdraw = amount
self.__balance = self.__balance - withdraw
return
def get_balance(self):
return self.__balance
# link for code in case indentation mess up: https://repl.it/Gp0u/11
class Account:
def __init__(self, id, balance: int=None, annual_interest_rate:
int=None):
if balance > 0:
self.__id = id
if balance == None:
self.__balance = 100
else:
self.__balance = balance
if annual_interest_rate == None:
self.__annualInterest = 0
else:
self.__annualInterest = annual_interest_rate
else:
raise ValueError("Warning! Your balance is equal to or less than
0!")
def get_id(self):
return self.__id
def get_balance(self):
return self.__balance
def get_annual_interest_rate(self):
return self.__annualInterest
def get_monthly_interest_rate(self):
return (self.__annualInterest/12)
def get_monthly_interest(self):
return (self.__balance * (self.__annualInterest/12)/100)
def deposit(self, amount: int=None):
if amount == None:
deposit = 0
else:
deposit = amount
self.__balance = self.__balance + amount
return
def withdraw(self, amount: int=None):
if amount == None:
withdraw = 0
elif amount <= 0:
raise ValueError("ERROR: Invalid amount!")
elif amount > self.__balance:
raise ValueError("ERROR: Insufficient funds!")
withdraw = amount
self.__balance = self.__balance - withdraw
return
def get_balance(self):
return self.__balance
try:
account1 = Account(1122, 0, 3.35)
except ValueError as x:
print (x)