I have a bank account OOP project I am working on. And I can't seem to figure out this one bug. In brief, I would like to return a users bank account after they have added it. I have included all the code because I've solved this in an object-oriented way. The str() method seems to be the problem.
from enum import Enum
class AccountType(Enum):
SAVINGS = 1
CHECKING = 2
class BankAccount():
def __init__(self, owner, accountType):
self.owner = owner
self.accountType = AccountType(accountType)
self.balance = 0
def withdraw(self, amount):
if amount > self.balance:
raise Exception('Your balance is' + self.balance + 'cannot take this much money!')
else:
self.balance -= amount
def deposit(self,amount):
self.balance += amount
def __str__(self):
return "Owner: {}. Account type is: {} ".format(self.owner, AccountType(self.accountType).name)
def __len__(self):
return self.balance
#This class is responsible for returning information about the user and their account type.
class BankUser():
#initialize an accounts dictionary.
accounts = {}
def __init__(self, owner):
self.owner = owner
def addAccount(self, accountType):
if self.accounts.get(accountType) != None:
raise Exception('Cannot have more than 1 ' + AccountType(accountType).name + ' account!')
self.accounts[accountType] = BankAccount(self.owner, accountType)
#test if user inputs an account, otherwise throw an error
def getBalance(self, accountType):
return len(self.accounts[accountType])
def deposit(self, accountType, amount):
if (accountType in self.accounts and isinstance(self.accounts[accountType], BankAccount)):
self.accounts[accountType].deposit(amount)
else:
raise Exception(self.owner + ' does not have a ' + AccountType(accountType).name + ' account!')
def withdraw(self, accountType, amount):
self.accounts[accountType].withdraw(amount)
def __str__(self):
return "Your account is {}".format(AccountType(accountType).name)
user = BankUser("David")
user.addAccount(1)
print(user)
#OUTPUT
TypeError: __str__() missing 1 required positional argument: 'accountType'
I would like to return a users account. How do I do that? Everything I have tried has ended up with this error.
accountTypeis undefined in your__str__()method. You need to extract the account type from your dictionary of accounts.AccountType(accountType).nameisn't syntactically correct anyway. What are you trying to print there?__str__to return some human-friendly representation of the class instance itself. You don't use it for general messages.