功能: 开户、查询、取款、存款、转账、改密、锁定、解锁、补卡、销户、退出
简述:用户需要用身份证号码开户注册一个银行卡号,可以实现查询、存款取款等功能。此系统主要采用面向对象的方法,信息存储方式采用json模块来进行存储。(数据存储也可以用pickle模块,彩票系统已采用,此系统就不用pickle模块,而采用json模块,下文会简单介绍一下json和pickle的优缺点,纯属个人见解)
系统主函数:
说明:
1、此系统获取文件绝对路径方法采用os模块(彩票系统采用指定的方式获取绝对路径)
2、数据存储:
在退出时,写数据方式采用"w"的方式,而不采用"a"(追加)的方式。
原因:如果退出时,写数据采用"a"(追加)的方式,则每运行一次程序,则会成倍增加数据量,内存容易爆满。
3、文件操作:
文中采用两种方式对文件进行操作,一种是读写完文件后会自动关闭文件(即用with方式),一种是读写完文件后得手动关闭文件
from amt import Amt
from admin import Admin
from card import Card
from uers import Uers
import os
import json
def Main():
admin = Admin()
allUserDict = {}
path = os.path.join(os.getcwd(), "allUers.txt")
with open(path,"r") as f:
for line in f.readlines():
dict1 = eval(line)
user = Uers.dict2obj(dict1)
allUserDict[user.bankCard.bankCardNum] = user
amt = Amt(allUserDict)
admin.printAdminView()
if admin.adminOption() == -1:
print("账号不存在或密码输入有误!系统自动结束...".center(48,"*"))
return -1
while True:
admin.printSysFunctionView()
operation = input("请选择您的操作:".center(42," "))
if operation == "1":
print("开户".center(48,"*"))
amt.createUers()
elif operation == "2":
print("查询".center(48,"*"))
amt.query()
elif operation == "3":
print("取款".center(48,"*"))
amt.getMoney()
elif operation == "4":
print("存款".center(48,"*"))
amt.saveMoney()
elif operation == "5":
print("转账".center(48,"*"))
amt.transferMoney()
elif operation == "6":
print("改密".center(48,"*"))
amt.changePassword()
elif operation == "7":
print("锁卡".center(48,"*"))
amt.lockCard()
elif operation == "8":
print("解卡".center(48,"*"))
amt.solutionCard()
elif operation == "9":
print("补卡".center(48,"*"))
amt.repairCard()
elif operation == "0":
print("销卡".center(48,"*"))
amt.destoryCard()
elif operation == "t":
print("退出".center(48,"*"))
close = admin.adminOption()
if close != -1:
print("系统关闭成功!".center(43,"*"))
f = open(path, "w", encoding="utf-8")
for key, value in allUserDict.items():
json.dump(value, f, default=Uers.obj2dict)
f.write("\n")
f.close()
return -1
else:
print("管理员账号或密码输入有误!关闭失败...".center(33,"*"))
continue
else:
print("输入有误!系统将自动结束...".center(38,"*"))
return -1
if __name__ == "__main__":
Main()
系统主界面模块:
主要实现系统界面的显示。
import time
class Admin(object):
def __init__(self):
self.admin = "1"
self.passwd = "1"
def printAdminView(self):
print("**************************************************")
print("* *")
print("* *")
print("* 欢迎登录木子李银行系统 *")
print("* *")
print("* *")
print("* *")
print("**************************************************")
def printSysFunctionView(self):
print("**************************************************")
print("* 开户(1) 查询(2) *")
print("* 取款(3) 存款(4) *")
print("* 转账(5) 改密(6) *")
print("* 锁定(7) 解锁(8) *")
print("* 补卡(9) 销户(0) *")
print("* 退出(t) *")
print("**************************************************")
def adminOption(self):
inputAdmin = input("请输入管理员账号:")
inputPasswd = input("请输入管理员密码:")
if self.admin != inputAdmin or self.passwd != inputPasswd:
return -1
time.sleep(1)
return True
if __name__ == "__main__":
pass
系统功能模块:
主要实现系统开户、查询、取款等功能。
from card import Card
from uers import Uers
import check
class Amt(object):
def __init__(self, allUers):
self.allUers = allUers
def createUers(self):
name = input("请输入您的名字:").strip()
if not check.checkName(name):
return -1
identityCardNum = input("请输入您的身份证号码:").strip()
if not check.checkIdentity(identityCardNum):
return -1
phoneNum = input("请输入您的电话号码:").strip()
if not check.checkPhone(phoneNum):
return -1
bankCardNum = check.getBankCardNum()
# 判断卡号是否已存在字典中,若存在则重新生成
while True:
if self.allUers.get(bankCardNum) != None:
bankCardNum = check.getBankCardNum()
else:
break
prestoredMoney = int(input("请输入预存款金额:"))
if not check.checkPrestoredMoney(prestoredMoney):
return -1
onePassword = input("请设置您的密码:").strip()
for x in range(2):
if not check.checkPassword(onePassword):
print("密码输入不正确!")
else:
break
else:
print("密码输入不正确!开户失败...")
return -1
# 创建一个卡对象,并存储卡的信息
card = Card(bankCardNum, onePassword, prestoredMoney)
# 创建一个用户对象,并存储用户信息
uers = Uers(name, identityCardNum, phoneNum, card)
self.allUers[bankCardNum] = uers
print("恭喜您!开户成功,请牢记您的卡号:", bankCardNum)
def query(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
print("卡号为:%s\n余额为:%d" % (uers.bankCard.bankCardNum, uers.bankCard.bankCardMoney))
def getMoney(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
money = int(input("请输入取款金额:"))
if uers.bankCard.bankCardMoney > money:
uers.bankCard.bankCardMoney -= money
else:
print("余额不足!")
return -1
print("卡号为:%s\n余额为:%d" % (uers.bankCard.bankCardNum, uers.bankCard.bankCardMoney))
def saveMoney(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
money = int(input("请输入存款金额:"))
uers.bankCard.bankCardMoney += money
print("卡号为:%s\n余额为:%d" % (uers.bankCard.bankCardNum, uers.bankCard.bankCardMoney))
def transferMoney(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
receiveBankCardNum = input("请输入接收者的卡号:").strip()
receiveUers = self.allUers.get(receiveBankCardNum)
if receiveUers == None:
print("该卡不存在!!!")
return -1
transferMoney = int(input("请输入转账金额:"))
if transferMoney > uers.bankCard.bankCardMoney:
print("余额不足! 转账失败...")
return -1
uers.bankCard.bankCardMoney -= transferMoney
receiveUers.bankCard.bankCardMoney += transferMoney
print("卡号为:%s\n余额为:%d" % (uers.bankCard.bankCardNum, uers.bankCard.bankCardMoney))
def changePassword(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
onePassword = input("请设置您的新密码:").strip()
if not check.checkPassword(onePassword):
print("密码输入不正确!密码重置失败...")
return -1
uers.bankCard.bankCardPassword = onePassword
print("密码修改成功!请牢记您的新密码!")
def lockCard(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
uers.bankCard.bankCardLock = 1
print("您卡号为:%s已成功上锁!" % uers.bankCard.bankCardNum)
def solutionCard(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if not uers.bankCard.bankCardLock:
print("改卡已解锁!无须再解!")
return -1
uers.bankCard.bankCardLock = 0
print("您卡号为:%s已解锁成功!" % uers.bankCard.bankCardNum)
def repairCard(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
# 产生新卡号
newBankCardNum = check.getBankCardNum()
while True:
if self.allUers.get(newBankCardNum) != None:
newBankCardNum = check.getBankCardNum()
else:
break
print("您的新卡号为:%s" % newBankCardNum)
# 设置新密码
onePassword = input("请设置您的密码:").strip()
if not check.checkPassword(onePassword):
return -1
card = Card(newBankCardNum, onePassword, uers.bankCard.bankCardMoney)
newUser = Uers(uers.name, uers.identityCardNum, uers.phoneNum, card)
self.allUers[newBankCardNum] = newUser
print("请牢记您的新卡号:%s 余额:%d" % (newBankCardNum, newUser.bankCard.bankCardMoney))#newUser.bankCardNum.bankCardMoney))
self.allUers.pop(bankCardNum)
def destoryCard(self):
bankCardNum = input("请输入您的卡号:").strip()
cardPassword = input("请输入您的密码:").strip()
uers = self.allUers.get(bankCardNum)
if uers == None or uers.bankCard.bankCardPassword != cardPassword:
print("该卡不存在或密码不正确!")
return -1
if uers.bankCard.bankCardLock:
print("改卡已经锁定!请解锁后再使用该功能...")
return -1
destory = input("是否确认注销该卡?【yes/no】").strip()
if destory == 'yes':
self.allUers.pop(bankCardNum)
print("注销成功!")
return -1
else:
print("操作已取消!")
return -1
if __name__ == "__main__":
pass
系统辅助功能模块:
主要用于验证用户名、密码、身份证等信息是否正确。
import random
# 检查用户名输入是否合法
def checkName(user):
if len(user) != 1:
print("用户名长度不合法!")
return False
for x in user:
if x >= '0' and x <= '9' or x >= 'a' and x <= 'z' or x >= 'A' and x <= 'Z' or x == '_':
pass
else:
print("用户名输入不合法!")
return False
return True
# 检查身份证号码是否合法
def checkIdentity(identity):
if len(identity) != 1:
print("身份证长度不合法!")
return False
for x in identity:
if x >= '0' and x <= '9':
pass
else:
print("身份证输入不合法!")
return False
return True
# 检查电话号码是否合法
def checkPhone(identity):
if len(identity) != 1:
print("电话号码长度不合法!")
return False
for x in identity:
if x >= '0' and x <= '9':
pass
else:
print("电话号码输入不合法!")
return False
return True
# 检查预存款金额是否合法
def checkPrestoredMoney(money):
if money > 0:
return True
else:
print("预存款金额输入有误!")
return False
# 检查密码是否正确
def checkPassword(password):
AgainPassword = input("请确认您的密码:").strip()
if password == AgainPassword:
return True
else:
return False
# 产生一个卡号
def getBankCardNum():
bankCardNum = ""
for i in range(6):
bankCardNum += chr(random.randrange(10) + 48)
return bankCardNum
if __name__ == "__main__":
pass
用户信息:
此模块主要用来存储用户的信息。
由于用json模块将信息写入磁盘,而且写入方式是以str的方式写入,故用户模块和银行卡模块不需要采用"__str__"对用户信息以及银行卡信息进行重加载。查看用户信息或银行卡信息可以直接打开存储文件进行查阅。无须进行重加载打印查阅。(上一遍彩票系统采用的是pickle模块进行存储数据,写入方式是以二进制的方式,故查阅信息时,需要对信息进行重加载以及打印。)
json模块与pickle模块的比较
json模块优点:写入信息是以str的形式写入,可以查看信息写入是否正确
json模块缺点:读写数据比较麻烦。无法直接将对象信息写入文件,需要自定义方法将对象信息写入文件
(下面方法采用:将对象转为字典的方式写入;读取对象信息时,再将字典转为对象)
pickle模块优点:读写数据方便
pickle模块缺点:写入信息时,无法查看信息是否写入正确。只能对数据进行重加载后进行打印才能查看信息。
from card import Card
class Uers(object):
def __init__(self, name, identityCardNum, phoneNum, bankCard):
self.name = name
self.identityCardNum = identityCardNum
self.phoneNum = phoneNum
self.bankCard = bankCard
# def __str__(self):
# return "%s-%s-%s-%s-%s-%d-%s" % (self.name, self.identityCardNum, self.phoneNum,
# self.bankCard.bankCardNum, self.bankCard.bankCardPassword,
# self.bankCard.bankCardMoney, self.bankCard.bankCardLock)
@staticmethod
def obj2dict(uers):
return {"name":uers.name, "identityCardNum":uers.identityCardNum,
"phoneNum":uers.phoneNum, "bankCard":{"bankCardNum": uers.bankCard.bankCardNum,
"bankCardPassword": uers.bankCard.bankCardPassword,"bankCardMoney": uers.bankCard.bankCardMoney,
"bankCardLock": uers.bankCard.bankCardLock}}
@staticmethod
def dict2obj(dict1):
return Uers(dict1.get("name"), dict1.get("identityCardNum"), dict1.get("phoneNum"),
Card(dict1["bankCard"]["bankCardNum"], dict1["bankCard"]["bankCardPassword"],
dict1["bankCard"]["bankCardMoney"], dict1["bankCard"]["bankCardLock"]))
if __name__ == "__main__":
pass
银行卡信息:
此模块主要用于存储用户银行卡的信息
# 卡默认处于解锁状态
class Card(object):
def __init__(self, bankCardNum, bankCardPassword, bankCardMoney, bankCardLock = 0):
self.bankCardNum = bankCardNum
self.bankCardPassword = bankCardPassword
self.bankCardMoney = bankCardMoney
self.bankCardLock = bankCardLock
# def __str__(self):
# return "%s-%s-%d-%s" % (self.bankCardNum, self.bankCardPassword, self.bankCardMoney, self.bankCardLock)
if __name__ == "__main__":
pass