0

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.

3
  • I believe the problem is that accountType is undefined in your __str__() method. You need to extract the account type from your dictionary of accounts. Commented Sep 30, 2018 at 1:57
  • AccountType(accountType).name isn't syntactically correct anyway. What are you trying to print there? Commented Sep 30, 2018 at 2:05
  • Typically, you use __str__ to return some human-friendly representation of the class instance itself. You don't use it for general messages. Commented Sep 30, 2018 at 2:17

1 Answer 1

1

You will need to move the accounts dictionary to be owned by a single bank user instance, not the bank user class (move to the __init__)

Then, all your methods have accountType except __str__, so you cannot just access it as a variable, but you could return the whole dictionary instead.

class BankUser():

  def __init__(self, owner):
    self.owner = owner
    #initialize an accounts dictionary. 
    self.accounts = {}

 ...

 def __str__(self):
   return "Accounts: {}".format(self.accounts)

Or you can do [AccountType(type).name for type in self.accounts]

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way for it to return the type of account? e.g SAVINGS or CHECKINGS?
You have multiple accounts for a BankUser, what do you mean?
So the output to the code you've suggested is: ``` Accounts: {1: <__main__.BankAccount object at 0x10f033898>}```
Looks like your __str__ on the BankAccount class is not being called. Or you actually need to define __repr__ for it as well, but as I mentioned below the code, you're welcome to iterate over the accounts and get the types

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.