简体   繁体   中英

How to implement transfer method for a account class in python

So, I'm making a Account class in python. It has the basic functions of deposit, withdrawing, and checking your balance. I'm having trouble with a transfer method though. This is my code(sorry for the code dump):

class Account:
    """simple account balance of bank"""
    def __init__ (self, name, balance):
        self.name = name
        self.balance = balance
        print('Account of ' + self.name)
    def deposit(self, amount):
        if amount > 0:
            self.balance += amount
            self.statement()
    def withdrawal(self, amount):
        if amount > 0 and self.balance > amount:
            self.balance -= amount
            self.statement()
        else:
            print("the ammount in your is not sufficent")
            self.statement()
    def statement(self):
        print("Hi {} your current balance is {}".format(self.name,self.balance))
    def transfer(self, amount, name):
        self.balance = self.balance - amount
        name.balance = name.balance + amount 
        return name.balance()

Now, it works for

abc = Account("abc", 0)
abc.deposit(1000)
siddharth = Account("siddharth", 159)

So how do I run following code:

siddharth.transfer(11, "abc")
siddharth.transfer(11, Account.abc)

also, how do I create account "abc" if account "abc" doesn't exist

Your code will be your best lesson about taking care of variables/parameters naming. Your method transfer(self, amount, name) should be transfer(self, amount, account) . I think that now, it will be obvious that the correct code is

abc = Account("abc", 0)
abc.deposit(1000)
siddharth = Account("siddharth", 159)
siddharth.transfer(11, abc)

Be really careful on misleading names.

Aside of your question, I don't think that an Account should have a transfer method. An Account only cares about deposits and withdraws, not about what is done with them. IMO Transfer should be a function with 2 Account parameters, withdrawing from the first, making a deposit on the second. This is just to follow the Single Responsibility principle.

Following the same principle, don't put print functions in an Account. Consider that you don't know the context in which your class will be used. If it is in a web app, prints are redirected to /dev/null…

Finally, always do what you said you'll do. If I have an account with a balance b, I expect that after the call to deposit with a value v, my account balance will be b + v. No matter the value of v. You are right to check the value and not adding a negative value (that is a withdraw) so you have to warn the caller that you'll not add the value, so rise an exception. Same for withdraw.

You can first have an array of all accounts somewhere declared. Then, you can first try to find if an account exists. If not, create an account and pass them.

allAccounts = []

#create bunch of accounts including 'abc'

siddharth = Account("siddharth", 159)

searchResult = [x for x in allAccounts if x.name == 'abc']
#assuming that account names are unique
if len(searchResult) == 0:
   acc = Account("abc", 11)
else:
   acc = searchResult[0]

siddarth.transfer(11, acc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM