4
\$\begingroup\$

I'm creating a Banking system program using Python, and the requirements of this program are very basic:

  1. Register and maintain Customer Details
  2. Update Account balance through Deposits and Withdrawals

I've managed to program the basic functionality, and now I'm in need of any feedback on how to improve my code, in order to create a program with CLEAR, CONCISE AND STRUCTURED CODE. I'd very much appreciate any kind of feedback, be it code specific feedback or feedback on my general coding, as I'm a beginner to programming in general.

PS: Im aware that the program needs error handling, and Im already working on it.

import pyodbc

cnxn = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};Server=GLOBALGROWTH\SQLEXPRESS;Database=IDLBANKSYS;Trusted_Connection=yes;')
cursor = cnxn.cursor()

class CLIMenu(object):

    def __init__(self, options: list):
        self.options=options

    def PrintCLIMenu(self):
        print('=' * 30, '\tIDL BANKING SYSTEM', '=' * 30, sep='\n')
        print()
        print("PLEASE ENTER THE NUMBER CORRESPONDING TO YOUR DESIRED COMMAND IN THE PROMPT BELOW :\n ", *self.options,
              sep='\n')
        print()
    def GetUserInput(self):
        return input(">>>: ")

def RegisterCustomer():
    RegisterCustomerForm = [input("NATIONAL IDENTITY CARD NUMBER : "), input("FULL NAME IN CAPITALS : "),
                                input("DATE OF BIRTH AS DD/MM/YYYY : "),
                                input("ADDRESS : "), input("CONTACT NUMBER : "), input("EMAIL ADDRESS : ")]
    cursor.execute("INSERT into customer VALUES(?,?,?,?,?,?)", *RegisterCustomerForm)
    cnxn.commit()
    print("REGISTRATION SUCCESSFUL")
    cursor.execute("INSERT into account VALUES(?,?,0)", RegisterCustomerForm[0], RegisterCustomerForm[1])
    cnxn.commit()

def ViewCustomer():
        CustToView=input("PLEASE ENTER THE NATIONAL IDENTITY CARD NUMBER OF THE CUSTOMER YOU WISH TO VIEW : ")
        cursor.execute("SELECT * FROM customer WHERE Cust_NIC=?",CustToView)
        columns = [column[0] for column in cursor.description]
        print('\t'.join(str(i) for i in columns),end="")
        print('\n')
        for row in cursor.fetchall():
            print ('\t'.join(str(j)for j in row))
        input("\n==========PRESS ENTER KEY TO RETURN TO THE PREVIOUS MENU==========")

def ViewAllCustomers():
    cursor.execute("SELECT * FROM customer")
    columns = [column[0] for column in cursor.description]
    print('\t'.join(str(i) for i in columns), end="")
    print('\n')
    for row in cursor.fetchall():
        print('\t'.join(str(j) for j in row))
    input("\n==========PRESS ENTER KEY TO RETURN TO THE PREVIOUS MENU==========")

class Transaction:

    def __init__(self,acct,amt):
        self.acct=acct
        self.amt=amt

    def Deposit(self):
        cursor.execute("UPDATE account SET Acct_Bal=Acct_Bal+? WHERE Acct_NO=?",self.amt,self.acct)
        cnxn.commit()
        print("YOU'VE DEPOSITED %s TO ACCOUNT NUMBER %s" % (self.amt,self.acct))
    def Withdrawal(self):
        cursor.execute("UPDATE account SET Acct_Bal=Acct_Bal-? WHERE Acct_NO=?",self.amt,self.acct)
        cnxn.commit()
        print("YOU'VE  WITHDRAWN  %s FROM ACCOUNT NUMBER %s" % (self.amt, self.acct))


if __name__=='__main__':
    while True:
        MainMenu=CLIMenu(['\t1.ACCESS CUSTOMER DETAILS','\t2.ACCESS TRANSACTION PORTAL','\t3.EXIT'])
        MainMenu.PrintCLIMenu()
        UserInput=MainMenu.GetUserInput()
        if UserInput =='1':
            while True:
                CustomerMenu = CLIMenu(['\t1.REGISTER CUSTOMER', '\t2.VIEW CUSTOMER',
                                              '\t3.VIEW ALL CUSTOMERS','\t4.GO TO PREVIOUS MENU'])
                CustomerMenu.PrintCLIMenu()
                UserInput=CustomerMenu.GetUserInput()
                if UserInput == '1':
                    RegisterCustomer()
                    continue
                if UserInput == '2':
                    ViewCustomer()
                    continue
                if UserInput == '3':
                    ViewAllCustomers()
                    continue
                if UserInput == '4':
                    break
        elif UserInput == '2':
            AcctToTransact=input("PLEASE ENTER THE ACCOUNT NUMBER : ")
            AmtToTransact=input("PLEASE ENTER THE TRANSACTION AMOUNT : ")
            trnsct=Transaction(AcctToTransact,AmtToTransact)
            TransactionMenu = CLIMenu(['\t1.DEPOSIT MONEY',
                                             '\t2.WITHDRAW MONEY', '\t3.GO TO PREVIOUS MENU'])
            TransactionMenu.PrintCLIMenu()
            UserInput = TransactionMenu.GetUserInput()
            if UserInput == '1':
                trnsct.Deposit()
                continue
            if UserInput == '2':
                trnsct.Withdrawal()
                continue
            if UserInput == '3':
                continue
        elif UserInput == '3':
            break

cnxn.close()
quit()
\$\endgroup\$
  • \$\begingroup\$ I removed the FP tag. FP and OOP are two rather different approaches to programming. \$\endgroup\$ – Mathieu Guindon Apr 13 '17 at 16:06
3
\$\begingroup\$
  1. You are using the same For loop twice (and the print that follows it). You should extract it to a new function and call it twice.

  2. In the main function you should extract the code that's under the if condition, and the code that's under elif to new functions.

  3. The if statements for UserInput should be replaced with a dictionary. See an example here.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.