简体   繁体   English

如何在python中实现帐户类的转账方法

[英]How to implement transfer method for a account class in python

So, I'm making a Account class in python. 因此,我正在用python创建一个Account类。 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 另外,如果帐户“ abc”不存在,如何创建帐户“ abc”

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) . 您的方法transfer(self, amount, name)应该是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. IMO Transfer应该是一个具有2个Account参数的功能,从第一个Account撤出,在第二个Account进行存款。 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… 如果在Web应用程序中,则打印将重定向到/ 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. 如果我有一个余额为b的帐户,我希望在调用存入值为v的存款后,我的帐户余额将为b + v。无论v的值为多少。您都可以检查该值而不添加a负值(即提现),因此您必须警告调用者不要添加该值,因此会引发异常。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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