-2

我为银行 ATM 编写了一个简单的程序。该程序运行正常,只是我在提款或存款后不知道如何更新新余额。因为在菜单功能中,我总是在输入密码后设置 self._balance 。问题必须在银行数据库中,解决此问题的最佳方法是什么?任何帮助表示赞赏。

4

2 回答 2

1

跟踪是否已经输入了有效的 PIN,如果输入了有效的 PIN,则不提示输入 PIN/重新初始化余额。

于 2014-03-22T02:06:01.307 回答
0

我对你的代码结构感到困惑。对我来说,这看起来像是许多不同类别的工作!我没有时间写一些更完整的东西,但考虑重构你的代码,特别是考虑到该__init__方法应该初始化一个类的所有属性,其中包括self.balance. 不要使用它来调用前端例程,然后使用以下方法执行此操作:

if __name__ == "__main__":
     ATM = Savingaccount()
     ATM.menu()

如果我是你,我会为提示用户的 ATM 写一个最低级别,并为其中有余额的帐户写一个级别。我的代码将类似于:

class Account(object):
    def __init__(self,acctholder,balance,pin):
        self.acctholder = acctholder # name
        self._balance = balance # amount as a decimal.Decimal
        self.pin = pin # a hashed pin number
    @staticmethod
    def confirmPIN(pin, hash):
        # hash a given PIN and check it against the hash
    def deposit(self,amount):
        self._balance += amount
    def withdraw(self,amount,pin):
        # hash the pin and check against self.pin
        # if False, raise PermissionError
        else:
            self._balance -= amount
    @property
    def balance(self):
        return self._balance

class ATM(object):
    def __init__(self):
        self.accounts = {#hash: <class Account>, ... }
    def menu(self):
        print("All the user options, and handle input and routing here")
于 2014-03-22T01:57:45.533 回答