Domanda

I wrote a simple program for a banking ATM. The program is working properly, just I don't know how to update the new balance after I made a withdraw or deposit. Because in the menu function, i always set the self._balance back after inputting a pin number. The problem must be in the Bankdatabase, What is the best way to fix this? Any help is appreciated.

È stato utile?

Soluzione 2

I'm confused by your code structure. That looks like many MANY different classes worth of work to me! I don't have time to write up something more fully-formed, but consider refactoring your code, especially consider that the __init__ method should initialize all the ATTRIBUTES of a class, which would include things like self.balance. Don't use it to invoke front-end routines, do that afterwards with something like:

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

If I were you I'd write at minimum class for the ATM that prompts the user and a class for the account that has a balance within it. My code would be something like:

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")

Altri suggerimenti

Keep track of whether or not a valid PIN has already been entered, and don't prompt for PIN / reinitialize balance if a valid pin has been entered.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top