Python: simple banking system implementation

Keywords: Python Pycharm Attribute

1. admin.py defines administrator information and main interface display

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
@author:Cao Xin Jian
@contact: 617349013@qq.com
@software: PyCharm
@file: admin.py
@time: 2018/9/11 10:14
"""


import time
class Admin():
    def __init__(self,name,passwd):
        self.name = name
        self.__passwd = passwd
        self.__status = False

    def adminView(self):
        for i in range(4):
            print("".center(60,"*"))
        s1 = "Welcome to CaoShi bank"
        print(s1.center(60-len(s1),"*"))
        for i in range(4):
            print("".center(60,"*"))
        if self.__status:
            print("The administrator is locked. Please contact the God Cao Xinjian")
            return -1
        name = input("Please enter the administrator user name:")
        if name != self.name:
            print("User name input error")
            return -1
        if self.checkAdminPasswd() != 0:
            return  -1
        return 0



    def adminAction(self):
        print("""************************************************************
***************Open account(1)****************Sales Account(2)***************
***************query(3)****************Transfer accounts(4)***************
***************Withdraw money(5)****************deposit(6)***************
***************locking(7)****************Unlock(8)***************
***************Modify density(9)****************Patch card(0)***************
************************Exit the system(q)************************
************************************************************
        """)

    def checkAdminPasswd(self):
        n = 0
        while n <= 3:
            if n == 3:
                self.status = True
                print("Input more than 3 times, the administrator is locked, please contact the God Cao Xinjian")
                return -1
            passwd = input("Please input a password:")
            if passwd != self.__passwd:
                print("Wrong password, please re-enter")
                n += 1
            else:
                print("Password verification succeeded, please wait")
                time.sleep(2)
                return 0
    @property
    def passwd(self):
        return self.__passwd

    @passwd.setter
    def passwd(self,password):
        self.__passwd = password

    @property
    def status(self):
        return self.__status

    @status.setter
    def status(self, st):
        self.__status = st

if __name__ == "__main__":
    admin = Admin("cxj","1")
    while True:
        admin.adminView()

2. card.py defines bank card information

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
@author:Cao Xin Jian
@contact: 617349013@qq.com
@software: PyCharm
@file: card.py
@time: 2018/9/11 15:02
"""


import random

class Card():
    def __init__(self,id,balance):
        self.__id = id
        self.__balance = balance
        self.status = False

    @property
    def id(self):
        return self.__id

    @id.setter
    def id(self,id):
        self.__id = id

    @property
    def balance(self):
        return self.__balance

    @balance.setter
    def balance(self,balance):
        self.__balance = balance


if __name__ == "__main__":
    card = Card(1000)
    print(card.id)
    print(card.balance)

3. user.py defines bank account information

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
@author:Cao Xin Jian
@contact: 617349013@qq.com
@software: PyCharm
@file: user.py
@time: 2018/9/11 14:54
"""

class User():
    def __init__(self,name,idCard,phone,passwd,card):
        self.__name = name
        self.__idCard = idCard
        self.phone = phone
        self.__passwd = passwd
        self.card = card

    @property
    def name(self):
         return self.__name

    @name.setter
    def name(self,name):
        self.__name = name

    @property
    def idCard(self):
        return self.__idCard

    @idCard.setter
    def idCard(self, idCard):
        self.__idCard = idCard

    @property
    def passwd(self):
        return self.__passwd

    @passwd.setter
    def passwd(self, passwd):
        if self.__passwd == passwd:
            raise UsersException("The new password is the same as the old one")
        else:
            self.__passwd = passwd

class UsersException(Exception):
    pass

4. functions.py bank function logic implementation

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
@author:Cao Xin Jian
@contact: 617349013@qq.com
@software: PyCharm
@file: functions.py
@time: 2018/9/11 11:01
"""

import pickle,os,random
from admin import Admin
from card import Card
from user import User,UsersException

pathAdmin = os.path.join(os.getcwd(), "admin.txt")
pathUser = os.path.join(os.getcwd(), "users.txt")

def rpickle(path):
    if not os.path.exists(path):
        with open(path,"w") as temp:
            pass
    with open(path,'rb') as f:
        try:
            info =  pickle.load(f)
        except EOFError as e:
            info = ""
    return info

def wpickle(objname,path):
    if not os.path.exists(path):
        with open(path,"w") as temp:
            pass
    with open(path,'wb') as f:
        pickle.dump(objname,f)

def adminInit():
    # print(pathAdmin)
    adminInfo = rpickle(pathAdmin)
    if adminInfo:
        admin = adminInfo
        # print(admin.status)
    else:
        admin = Admin("cxj", "1")
    return admin

def adminClose(admin):
    wpickle(admin, pathAdmin)

def randomId(users):

    while True:
        str1 = ""
        for i in range(6):
            ch = str((random.randrange(0, 10)))
            str1 += ch
        if not users.get(str1,""):
            return str1

def openAccount(users):
    name = input("Please enter your name:")
    idCard = input("Please enter your ID number:")
    phone = input("Please enter your phone number:")
    passwd = input("Please enter the account password:")
    balance = int(input("Please enter your amount:"))
    id = randomId(users)
    card = Card(id,balance)
    user = User(name,idCard,phone,passwd,card)
    users[id] = user
    print("Please remember your bank card number%s" %(id))


def userInit():
    userInfo = rpickle(pathUser)
    if userInfo:
        users = userInfo
    else:
        users = {}
    return users

def userClose(users):
    wpickle(users, pathUser)

def getUser(users):
    id = input("Please enter your bank card number:")
    if not users.get(id, ""):
        print("The card number you entered does not exist")
        user = None
    else:
        user = users.get(id)
    return user

def transferUser(users):
    id = input("Please enter the bank card number of the transfer (opposite party):")
    if not users.get(id, ""):
        print("The card number you entered does not exist")
        user = None
    else:
        user = users.get(id)
    return user

def changeMoney(user,res):
    money = int(input("Please enter the transaction amount:"))
    if money <= 0:
        print("Wrong amount entered")
        return 0
    if res:
        if money > user.card.balance:
            print("Sorry, your credit is running low")
            return 0
    return money

def serchAccount(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    res = checkUserPasswd(user)
    if not res:
        print("Your account name is%s,Your balance is%s" % (user.name, user.card.balance))

def transferAccount(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    res = checkUserPasswd(user)
    if not res:
        transUser = transferUser(users)
        if not transUser:
            return -1
        money = changeMoney(user,1)
        if not money:
            return -1
        user.card.balance -= money
        transUser.card.balance += money
        print("Successful trade")

def withdrawal(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    res = checkUserPasswd(user)
    if not res:
        money = changeMoney(user,1)
        if not money:
            return -1
        user.card.balance -= money
        print("Successful trade")

def deposit(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    res = checkUserPasswd(user)
    if not res:
        money = changeMoney(user,0)
        if not money:
            return -1
        user.card.balance += money
        print("Successful trade")

def delAccount(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    res = checkUserPasswd(user)
    if not res:
        users.pop(user.card.id)
        print("Account deleted successfully")
        return 0

def lockAccount(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    checkUserPasswdLock(user)

def unlockAccount(users):
    user = getUser(users)
    if not user:
        return -1
    if not user.card.status:
        print("Account does not need to be unlocked")
        return -1
    res = checkUserPasswd(user)
    if not res:
        user.card.status = False
        print("Account unlocked successfully!")

def changePasswd(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    res = checkUserPasswd(user)
    if not res:
        newPasswd = input("Please enter a new password:")
        try:
            user.passwd = newPasswd
        except UsersException as e:
            print(e)
        else:
            print("Password modified successfully!")

def makeNewCard(users):
    user = getUser(users)
    if not user:
        return -1
    if user.card.status:
        print("The account is locked. Please unlock it before using other functions")
        return -1
    res = checkUserPasswd(user)
    if not res:
        id = randomId(users)
        userinfo = users[user.card.id]
        users.pop(user.card.id)
        users[id] = userinfo
        users[id].card.id = id


        print("Please remember your bank card number%s" % (id))

def checkUserPasswd(user):
    n = 0
    while n <= 3:
        if n == 3:
            user.card.status = True
            print("Input more than 3 times, the account is locked, please unlock and use other functions")
            return -1
        passwd = input("Please enter your account password:")
        if passwd != user.passwd:
            print("Wrong password, please re-enter")
            n += 1
        else:
            return 0

def checkUserPasswdLock(user):
    n = 0
    while n <= 3:
        if n == 3:
            print("Input more than 3 times, account locking failed!")
            return -1
        passwd = input("Please enter your account password:")
        if passwd != user.passwd:
            print("Wrong password, please re-enter")
            n += 1
        else:
            user.card.status = True
            print("Account locked successfully!")
            return 0

5. bankManage.py main program

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
 @author: Cao Xinjian
@contact: 617349013@qq.com
@software: PyCharm
@file: bankManage.py
@time: 2018/9/11 9:57
"""

'''
Administrator class:
Name: Admin
 Properties: name, passwd
 Method: display the welcome interface and function interface of the administrator

Bank card:
Name: Card
 Attribute: id, balance
 Method: generate card number

ATM:
Name: ATM
 Properties:
Methods: account opening, inquiry, withdrawal, transfer, deposit, password change, locking, unlocking, card supplement and account cancellation

User:
Name: user
 Property: name, ID number, phone number, bank card
 Method:
'''

import time,os
from admin import Admin
import functions


#users = {}
def run():
    admin = functions.adminInit()
    users = functions.userInit()
    #print(users)
    if admin.adminView():
        functions.adminClose(admin)
        functions.userClose(users)
        return -1
    while True:
        admin.adminAction()
        value = input("please select the business you want to handle:")
        if value == "1":
            functions.openAccount(users)
            functions.userClose(users)
        elif value == "2":
            functions.delAccount(users)
            functions.userClose(users)
        elif value == "3":
            functions.serchAccount(users)
        elif value == "4":
            functions.transferAccount(users)
            functions.userClose(users)
        elif value == "5":
            functions.withdrawal(users)
            functions.userClose(users)
        elif value == "6":
            functions.deposit(users)
            functions.userClose(users)
        elif value == "7":
            functions.lockAccount(users)
            functions.userClose(users)
        elif value == "8":
            functions.unlockAccount(users)
            functions.userClose(users)
        elif value == "9":
            functions.changePasswd(users)
            functions.userClose(users)
        elif value == "0":
            functions.makeNewCard(users)
            functions.userClose(users)
        elif value == "q":
            functions.adminClose(admin)
            functions.userClose(users)
            return -1
        elif value == "m":
            for user in users:
                print(user)
        else:
            print("Emma, your input editor can't understand it, please input again")



if __name__ == "__main__":
    run()

 

Posted by jimmyt1988 on Sun, 15 Dec 2019 11:26:12 -0800