Exemplo n.º 1
0
    def logout(self) -> ControllerResponse:
        if self.account.isAuthorized:
            message = "Account {} logged out.".format(self.account.accountId)
            self.account = Account(self.db)
        else:
            message = "No account is currently authorized."

        response = ControllerResponse()
        response.addResponseMsg(message)
        return response
Exemplo n.º 2
0
    def controller(self, userInput: str) -> ControllerResponse:
        self.stopInactiveTimer()

        # minimal error handling for simplicity
        # one try block, if error occurs log and return generic response
        try:
            # raise exception
            if len(userInput.strip()) == 0:
                raise Exception("No input detected.")
            # parse commands if valid, exception raised if not valid
            cmds = self.validateInput(userInput)
            initArg = cmds[0]

            # if the command is in the list of commands that need authorization
            # validate that the account is authorized
            if initArg in self.preauthCmds:
                # if the account is authorized then route and run command
                if self.account.isAuthorized:
                    if initArg == "withdraw":
                        response = self.withdraw(cmds[1])
                    elif initArg == "deposit":
                        response = self.deposit(cmds[1])
                    elif initArg == "balance":
                        response = self.getBalance()
                    elif initArg == "history":
                        response = self.getHistory()
                # if the account isn't authorized, update response
                else:
                    response = ControllerResponse()
                    response.addResponseMsg("Authorization required.")
            # if command doesn't need preauth then run them
            elif initArg == "authorize":
                response = self.authorize(cmds[1], cmds[2])
            elif initArg == "logout":
                response = self.logout()
            elif initArg == "end":
                response = self.endProgram()
            # otherwise command isn't recognized, throw error
            else:
                raise Exception(
                    "Invalid command detected: {}".format(userInput))

        # if there is an error, log to db and update controller message
        except Exception as e:
            self.logError(e)
            message = "Invalid command detected."
            response = ControllerResponse()
            response.addResponseMsg(message)
            response.updateErrorFlag(True)

        # start inactive timer unless if program is ending
        if not response.end:
            self.startInactiveTimer()

        return response
Exemplo n.º 3
0
    def deposit(self, amount: int) -> ControllerResponse:
        if amount > 0:
            self.updateBalances(amount, False)
            message = "Current balance: {}".format(
                round(self.account.balance, 2))
        else:
            message = "Deposit amount must be greater than 0."

        # update response and return
        response = ControllerResponse()
        response.addResponseMsg(message)
        return response
Exemplo n.º 4
0
    def withdraw(self, amount: int) -> ControllerResponse:
        response = ControllerResponse()

        # if the amount is not greater than 0 and not an increment of 20 update response message
        # exit immediately
        if amount <= 0 or amount % 20 != 0:
            message = "Withdrawal amount must be greater than 0 and in increments of 20."
            response.addResponseMsg(message)
            return response
        # if the account is overdrawn update the response message
        # and exit immediately
        elif self.account.balance < 0:
            message = "Your account is overdrawn! You may not make withdrawals at this time."
            response.addResponseMsg(message)
            return response
        # if the atm has no money update response message and return
        elif self.atmBalance == 0:
            message = "Unable to process your withdrawal at this time."
            response.addResponseMsg(message)
            return response

        message = ""

        # if there isn't enough in the atm, update amount requested to atm balance
        # prepend unable to dispense full amount message to return message
        if amount > self.atmBalance:
            message = "Unable to dispense full amount requested at this time. "
            amount = self.atmBalance

        # if there is enough in the account
        # update account and atm balance
        if amount <= self.account.balance:
            self.updateBalances(amount, True)
            message += "Amount dispensed: ${}\nCurrent balance: {}".format(
                amount, round(self.account.balance, 2))
        # if there is money in the account but not enough
        # add an extra 5 to withdrawal amount
        # update account and atm balance
        elif 0 <= self.account.balance < amount:
            self.updateBalances(amount, True, True)
            message += (
                "Amount dispensed: ${}\nYou have been charged an overdraft fee of "
                "$5. Current balance: {}").format(
                    amount, round(self.account.balance, 2))

        # update response message
        response.addResponseMsg(message)
        return response
Exemplo n.º 5
0
    def getHistory(self) -> ControllerResponse:
        message = ""
        sqlData = self.sql.getHistoryCmd(self.account.accountId)

        # if no history found update message
        # otherwise grab history
        if len(sqlData) == 0:
            message = "No history found"
        else:
            for row in sqlData:
                message += "{} {} {} {}\n".format(row[0], row[1],
                                                  round(row[2], 2),
                                                  round(row[3], 2))

        response = ControllerResponse()
        response.addResponseMsg(message)
        return response
Exemplo n.º 6
0
    def authorize(self, accountId: str, pin: str) -> ControllerResponse:
        response = ControllerResponse()

        # check if an account is already authorized, if one is then return
        if self.account.isAuthorized:
            message = "An account is already authorized. Logout before authorizing another account."
            response.addResponseMsg(message)
            return response

        # validate that account sql object is valid
        # it's possible that it could have been cleared
        # due to inactivity
        if self.account.sql is None:
            self.account.sql = SQLHelper(self.db)

        # search for account data
        actData = self.sql.accountSelectCmd(accountId)

        # if no account exists with the provided account id then return
        # failed authorization
        if not actData:
            message = "Authorization failed."
            response.addResponseMsg(message)
            return response

        # get pin number
        actPin = actData[2]

        # if the pin on file matches the provided pin then authorize account
        if actPin == pin:
            balance = actData[3]
            self.account.addAccountDetails(accountId, balance, True)
            message = "{} successfully authorized.".format(accountId)
        # otherwise fail authorization
        else:
            message = "Authorization failed."

        response.addResponseMsg(message)
        return response
Exemplo n.º 7
0
 def endProgram(self) -> ControllerResponse:
     response = ControllerResponse()
     response.setEndFlag()
     response.addResponseMsg("Goodbye!")
     return response
Exemplo n.º 8
0
 def getBalance(self) -> ControllerResponse:
     response = ControllerResponse()
     response.addResponseMsg("Current balance: {}".format(
         round(self.account.balance, 2)))
     return response